【问题标题】:Add Quantity Input Field on WooCommerce Checkout For Each Product在 WooCommerce 结帐上为每个产品添加数量输入字段
【发布时间】:2020-06-14 14:58:25
【问题描述】:

我正在尝试了解如何在结帐页面上添加数量输入字段,从而允许客户更改每种产品的数量。

这是我目前得到的:

add_filter( 'woocommerce_checkout_cart_item_quantity', 'qty_and_qty_change_on_checkout', 20, 3 );
function qty_and_qty_change_on_checkout( $quantity_html, $cart_item, $cart_item_key ) {

    return '<br>
    '.woocommerce_quantity_input().'
    <span class="product-quantity">
    ' . sprintf( '<b>Qty:</b> %s', $cart_item['quantity'] ) . '
    </span>';
}

但这给了我一个错误,即使woocommerce_quantity_input() 是它的官方功能? 错误是:

Notice: Undefined index: product in /wp-content/plugins/woocommerce/includes/wc-template-functions.php on line 1670

【问题讨论】:

    标签: php wordpress woocommerce checkout product-quantity


    【解决方案1】:

    您错过了在woocommerce_quantity_input() 函数中设置必要的参数,就像在cart/cart.php 模板文件中一样... 以下将显示一个输入字段,其中结帐页面中的当前数量替换了数量字符串:

    add_filter( 'woocommerce_checkout_cart_item_quantity', 'qty_input_field_on_checkout', 20, 3 );
    function qty_input_field_on_checkout( $quantity_html, $cart_item, $cart_item_key ) {
        $_product = $cart_item['data'];
    
        if ( $_product->is_sold_individually() ) {
            $product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key );
        } else {
            $product_quantity = woocommerce_quantity_input(
                array(
                    'input_name'   => "cart[{$cart_item_key}][qty]",
                    'input_value'  => $cart_item['quantity'],
                    'max_value'    => $_product->get_max_purchase_quantity(),
                    'min_value'    => '0',
                    'product_name' => $_product->get_name(),
                ),
                $_product,
                false
            );
        }
    
        return '<br><span class="product-quantity"><strong>' . __( 'Qty') . ': </strong></span>' . $product_quantity;
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    现在这将只允许显示数量字段,但不允许更新产品数量,因为它是其他更复杂的东西。

    【讨论】:

    • 谢谢您,非常感谢您的帮助。我设法让更改自动更新。
    • 当数量发生变化时,如何使用 AJAX 自动更新?用户提到能够实现这一点,但没有分享。非常沮丧,因为我不知所措。
    猜你喜欢
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多