亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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條經驗 獲得超4個贊

您沒有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 關注
  • 163 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號