【问题标题】:WooCommerce: Only show product quantity in checkout if it is higher than 1WooCommerce:仅在结帐时显示大于 1 的产品数量
【发布时间】:2019-11-20 14:45:34
【问题描述】:

我想在 WooCommerce Checkout 中隐藏产品数量,并将标题旁边的“x 1”替换为产品标题下方的额外行。

但仅当订单中的产品超过 1 个时,它才应显示产品的数量。

我发现以下代码为数量添加了额外的行。但我不知道如何仅在订单中有 > 1 个产品时显示数量。

add_filter( 'woocommerce_checkout_cart_item_quantity', 'customizing_checkout_item_quantity', 10, 3);
function customizing_checkout_item_quantity( $quantity_html, $cart_item, $cart_item_key ) {
    $quantity_html = ' <br>
            <span class="product-quantity">' . __('Quantity', 'woocommerce') . ': <strong>' . $cart_item['quantity'] . '</strong></span>';

    return $quantity_html;
}

代码来自这个答案:https://stackoverflow.com/a/48233426/1788961

有没有办法检查数量并仅在订单中的产品超过 1 件时才显示该行?

【问题讨论】:

    标签: php wordpress woocommerce woocommerce-theming


    【解决方案1】:

    要检查每个订单是否有多个产品,我会选择以下条件:

    if ( WC()->cart->get_cart_contents_count() > 1 ) {
         // more than 1 product per order
    }
    

    【讨论】:

    • 谢谢,但我需要检查购物车中是否有多个单品(sku)。不是每个购物车的产品。
    【解决方案2】:

    我想通了。函数里已经有数量了:

    这是我的解决方案:

    add_filter( 'woocommerce_checkout_cart_item_quantity', 'customizing_checkout_item_quantity', 10, 3);
    function customizing_checkout_item_quantity( $quantity_html, $cart_item, $cart_item_key ) {
    
        $cart_item_quantity_count = $cart_item['quantity'];
        if ( $cart_item_quantity_count > 1 ) :
            $quantity_html = '<br> <span class="product-quantity">' . __('Quantity', 'woocommerce') . ': <strong>' . $cart_item['quantity'] . '</strong></span>';
        else:
            $quantity_html = '';
        endif;
    
        return $quantity_html;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-24
      • 2015-11-15
      • 1970-01-01
      • 2021-01-24
      • 2018-06-22
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多