【问题标题】:WooCommerce - Display total number of cart products in checkoutWooCommerce - 在结帐时显示购物车产品的总数
【发布时间】:2018-03-09 01:30:55
【问题描述】:

我正在尝试在结帐页面上显示客户购物车中的商品总数。

我在documentation 中读到我需要使用get_cart_contents_count,但是当我尝试这个时,无论我有多少物品,它都会输出 1。

注意: "get_cart_contents_count( ) 获取购物车中的商品数量。 退货 整数”

我的代码是:

add_action( 'woocommerce_before_checkout_billing_form', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {
    $_cartQty = count( WC()->cart->get_cart_contents_count( ) );
    echo $_cartQty;
} 

任何帮助都将不胜感激,因为我认为我已经很聪明地进入了这个阶段。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    你有一个计数使它不起作用 - 试试这个:

    function my_custom_checkout_field( $checkout ) {
        $_cartQty = WC()->cart->get_cart_contents_count( );
        echo $_cartQty;
    } 
    

    如果我是你,我会把它改成:

    function my_custom_checkout_field( $checkout ) {        
        return WC()->cart->get_cart_contents_count();
    } 
    

    我永远不会从这样的函数中回显——一般来说,像这样将 php 和 HTML 结合起来并不好。

    我会<?= $class->my_custom_checkout_field() ?>

    【讨论】:

    • 太棒了,我删除了“计数”并且它起作用了。谢谢你。顺便说一句,我不会重复这个数字。我想测试它是否超过 1,然后更改表单字段。 echo 只是为了测试。
    • 很好很好 - 尝试帮助自己使用像 xdebugger 这样的调试器 - 我学得很晚。
    • 顺便说一句。在您的回答中,您说“cout”。我尝试将其编辑为“计数”,但编辑必须为 6 个字符或更多。
    • 它现在已编辑 - 如果您喜欢答案,您可以投票并接受它作为答案
    猜你喜欢
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 2018-06-17
    相关资源
    最近更新 更多