用可预订产品的人数替换 WooCommerce 购物车数量
Replace WooCommerce Cart Qty by persons count for Bookable Products
我正在使用以下代码更新预订产品人数的购物车页面:
// Add "Persons" to replace cart quantity for bookable products
add_filter( 'woocommerce_cart_item_quantity', 'replace_cart_quantity_for_bookings', 20, 3 );
function replace_cart_quantity_for_bookings( $product_quantity, $cart_item_key, $cart_item ){
// Check that is a bookable product
if( isset($cart_item['booking']) ){
$product_quantity = '<span style="text-align: center; display:inline-block;">'.$cart_item['booking']['Persons'].'<br>
<small>(' . __('persons','woocommerce') . ')</small><span>';
}
return $product_quantity;
}
但是此代码无效并显示该错误:
Notice: Undefined index: Persons in /home/www/wp-content/themes/my-child-theme/functions.php
我们将不胜感激。
获取可预订产品的购物车项目人数的正确方法是使用:
$cart_item['booking']['_qty']
所以在你的代码中:
add_filter( 'woocommerce_cart_item_quantity', 'replace_cart_quantity_for_bookings', 20, 3 );
function replace_cart_quantity_for_bookings( $quantity, $cart_item_key, $cart_item ){
// Only for bookable product items
if( isset($cart_item['booking']) && isset($cart_item['booking']['_qty']) ){
$quantity = '<span style="text-align:center; display:inline-block; line-height:10px">'.$cart_item['booking']['_qty'].'<br>
<small>(' . __('persons','woocommerce') . ')</small><span>';
}
return $quantity;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
现在要按可预订产品的人员类型获取购物车项目数是(给出一个数组):
$cart_item['booking']['_persons']
在下面的例子中,这将给出一个类似于 2 种不同类型人员的数组:
Array (
[872] => 2
[873] => 2
)
我正在使用以下代码更新预订产品人数的购物车页面:
// Add "Persons" to replace cart quantity for bookable products
add_filter( 'woocommerce_cart_item_quantity', 'replace_cart_quantity_for_bookings', 20, 3 );
function replace_cart_quantity_for_bookings( $product_quantity, $cart_item_key, $cart_item ){
// Check that is a bookable product
if( isset($cart_item['booking']) ){
$product_quantity = '<span style="text-align: center; display:inline-block;">'.$cart_item['booking']['Persons'].'<br>
<small>(' . __('persons','woocommerce') . ')</small><span>';
}
return $product_quantity;
}
但是此代码无效并显示该错误:
Notice: Undefined index: Persons in
/home/www/wp-content/themes/my-child-theme/functions.php
我们将不胜感激。
获取可预订产品的购物车项目人数的正确方法是使用:
$cart_item['booking']['_qty']
所以在你的代码中:
add_filter( 'woocommerce_cart_item_quantity', 'replace_cart_quantity_for_bookings', 20, 3 );
function replace_cart_quantity_for_bookings( $quantity, $cart_item_key, $cart_item ){
// Only for bookable product items
if( isset($cart_item['booking']) && isset($cart_item['booking']['_qty']) ){
$quantity = '<span style="text-align:center; display:inline-block; line-height:10px">'.$cart_item['booking']['_qty'].'<br>
<small>(' . __('persons','woocommerce') . ')</small><span>';
}
return $quantity;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
现在要按可预订产品的人员类型获取购物车项目数是(给出一个数组):
$cart_item['booking']['_persons']
在下面的例子中,这将给出一个类似于 2 种不同类型人员的数组:
Array (
[872] => 2
[873] => 2
)