Php 按可预订产品的人员计数替换购物车数量
我正在使用以下代码与预订产品的人员一起更新购物车页面的数量:Php 按可预订产品的人员计数替换购物车数量,php,wordpress,woocommerce,cart,woocommerce-bookings,Php,Wordpress,Woocommerce,Cart,Woocommerce Bookings,我正在使用以下代码与预订产品的人员一起更新购物车页面的数量: // 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
// 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;
}
//添加“人员”以替换可预订产品的购物车数量
添加过滤器('woocommerce\u cart\u item\u quantity','replace\u cart\u quantity\u for\u bookings',20,3);
函数替换预订的购物车数量($product\U QUOTE,$cart\U item\U key,$cart\U item){
//检查是否为可预订产品
如果(isset($cart_item['booking'])){
$product\U QUOTE='.$cart\U item['booking']['Persons'].
("人","商业");;
}
返回$product\U数量;
}
但此代码不起作用,并显示该错误:
注意:未定义索引: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;
}
这将为以下情况下的2种不同人员类型提供类似的数组:
Array (
[872] => 2
[873] => 2
)
我们将非常感谢您对以下答案的反馈,谢谢……您的解决方案非常有效,解决了问题。我正在编写更多的代码来提取购物车的总数量,希望代码不会取代前端的数量,也不会取代后端的数量——应该是这样,因为我们正在取代数量元。将确认这一点,并为其他人的利益。不幸的是,它没有取代后端的数量。其他功能,如shipping和cart fee,仍然将数量视为1,这是可预订产品的默认值。不,它不会替换,这是另一个问题/答案:stackOverFlow中的规则是一次一个问题,以避免过于宽泛的问题,这是离题的。您在问题中使用的挂钩只会影响数量显示,主要问题是从购物车项目中获取一次预订的人员数量,该购物车项目是
$cart\u item['booking']['qty']
,而不是$cart\u item['booking']['persons']
。我已经就此提出了另一个问题。但我想为了社区利益,我也要结束这里的讨论。谢谢你的指导。干杯嗨,另一个问题的链接是,如果你能帮忙的话。
Array (
[872] => 2
[873] => 2
)