gpt4 book ai didi

php - 用可预订产品的人数替换 WooCommerce 购物车数量

转载 作者:行者123 更新时间:2023-12-04 10:04:58 25 4
gpt4 key购买 nike

我正在使用以下代码更新购物车页面的 Qty with Persons for Booking 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
)

关于php - 用可预订产品的人数替换 WooCommerce 购物车数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61632753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com