为了账号安全,请及时绑定邮箱和手机立即绑定

在 WooCommerce Checkout 上为每个产品添加数量输入字段

在 WooCommerce Checkout 上为每个产品添加数量输入字段

PHP
BIG阳 2023-05-26 16:25:08
我想了解如何在结帐页面上添加数量输入字段,这样,客户就可以更改每种产品的数量。这是我到目前为止得到的: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
查看完整描述

1 回答

?
守着一只汪

TA贡献1872条经验 获得超3个赞

您没有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 文件。测试和工作。


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


查看完整回答
反对 回复 2023-05-26
  • 1 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信