gpt4 book ai didi

wordpress - 在 WooCommerce 的购物车页面上更新数量时拆分购物车项目

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

我想在单独的行中拆分相同产品的购物车项目。当我在单个产品页面上增加数量并添加到购物车时,它显示为单独的购物车项目。
我正在使用 WooCommerce - Treat cart items separate if quantity is more than 1答案代码。
我希望在购物车页面上更新数量并单击“更新购物车”时,项目必须在单独的行上拆分。
我怎样才能做到这一点?

最佳答案

当购物车页面上的数量更新时,下面的代码将在单独的行上拆分项目。

注释和解释添加到代码中。

function on_action_cart_updated( $cart_updated ) {
if ( $cart_updated ) {
// Get cart
$cart_items = WC()->cart->get_cart();

foreach ( $cart_items as $cart_item_key => $cart_item ) {
$quantity = $cart_item['quantity'];

// If product has more than 1 quantity
if ( $quantity > 1 ) {

// Keep the product but set its quantity to 1
WC()->cart->set_quantity( $cart_item_key, 1 );

// Run a loop 1 less than the total quantity
for ( $j = 1; $j <= $quantity -1; $j++ ) {
// Set a unique key.
$cart_item['unique_key'] = md5( microtime() . rand() . "Hi Mom!" );

// Get vars
$product_id = $cart_item['product_id'];
$variation_id = $cart_item['variation_id'];
$variation = $cart_item['variation'];

// Add the product as a new line item with the same variations that were passed
WC()->cart->add_to_cart( $product_id, 1, $variation_id, $variation, $cart_item );
}
}
}
}
}
add_action( 'woocommerce_update_cart_action_cart_updated', 'on_action_cart_updated', 20, 1 );

关于wordpress - 在 WooCommerce 的购物车页面上更新数量时拆分购物车项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59878382/

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