【问题标题】:Display total number of items in basket on cart and checkout, and email order reciept在购物车和结帐时显示篮子中的物品总数,并通过电子邮件发送订单收据
【发布时间】:2018-10-20 23:45:12
【问题描述】:

我正在尝试在不同位置显示购物车中的商品总数

1) 购物车页面 2) 结帐页面 3)通过电子邮件将订单收据发送给客户 4) 将订单收据通过电子邮件发送给管理员

我正在使用以下函数来计算购物车中的商品总数

 // function to calc total number items in basket
 function gh_custom_checkout_field( $checkout ) {        
 return WC()->cart->get_cart_contents_count();
 } 

有谁知道我如何在上面的位置显示值?

我尝试过使用 my_custom_checkout_field() ?> 但这只是给出了内部服务器错误。

【问题讨论】:

  • 大声抱歉是个错误

标签: php wordpress woocommerce cart orders


【解决方案1】:

1) 用于购物车和结帐

您可以使用您的函数(不带$checkout 变量,因为它不需要) 作为简码:

function get_cart_count() {        
    return WC()->cart->get_cart_contents_count();
}
add_shortcode( 'cart_count', 'get_cart_count');

代码进入您的活动子主题(或活动主题)的 function.php 文件中。

你会使用它:

  • 在 WordPress 文本编辑器中:[cart_count]
  • 在php代码中:echo do_shortcode( "[cart_count] ");
  • 在混合 php/html 代码中:<?php echo do_shortcode( "[cart_count] "); ?>

2) 订单和电子邮件通知

由于相应的购物车对象不再存在,您需要从WC_Order 对象(如果没有,则为订单ID)获取订单商品数。

您可以使用此自定义函数,并带有一个强制定义的参数,该参数可以是 WC_Order 对象或订单 ID。如果不是,该函数将不返回任何内容:

function get_order_items_count( $mixed ) {        
    if( is_object( $mixed ) ){
        // It's the WC_Order object
        $order = $order_mixed;
    } elseif ( ! is_object( $mixed ) && is_numeric( $mixed ) ) {
        // It's the order ID
        $order = wc_get_order( $mixed ); // We get an instance of the WC_order object
    } else {
        // It's not defined as an order ID or an order object: we exit
        return;
    }
    $count = 0
    foreach( $order->get_items() as $item ){
        // Count items
        $count += (int) $item->get_quantity()
    }
    return $count;
}

您将始终使用它作为函数的参数设置现有动态变量 $order_id$order 之类的

echo get_order_items_count( $order_id ); // Dynamic Order ID variable

或者

echo get_order_items_count( $order ); // Dynamic Order object variable

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多