首页 > 解决方案 > 如何在 Woocommerce 中根据购物车总数显示消息

问题描述

我正在尝试使用函数根据购物车总数在自定义页面模板中显示一条消息,但它不显示我的消息。

请看下面的功能:

add_shortcode( 'custom_order, 'woocommerce_order_subtotal' );
function woocommerce_order_subtotal(){ 

    $sub = WC()->cart->total();

    // if for the subtotal:

    if ($sub > 100)) {
        echo "Hello world!";
    }
}

标签: phpwoocommerce

解决方案


调用购物车时应该得到小计而不是总数

   add_shortcode( 'custom_order, 'woocommerce_order_subtotal' );
    function woocommerce_order_subtotal(){ 

        $sub = WC()->cart->total(); //this is wrong here you gettin total not subtotal
         $sub = WC()->cart->subtotal
         //Or this following         
         $sub = WC()->cart->get_cart_subtotal()

        // if for the subtotal:

        if ($sub > 100)) {
            echo "Hello world!";
    }
   }

推荐阅读