在WooCommerce中将产品添加到购物车时自动添加包装



在WooCommerce中,我使用一个代码,在将任何菜肴添加到购物车时自动添加包装。

add_action( 'woocommerce_before_calculate_totals', 'add_delivery_charge_to_cart', 10, 1 );
function add_delivery_charge_to_cart( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$dcharge_id  = 5737; // "LunchBox" to be added to cart
$items_count = 0;  // Initializing
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Check if "LunchBox" product is already in cart
if( $cart_item['data']->get_id() == $dcharge_id ) {
$dcharge_key = $cart_item_key;
$dcharge_qty = $cart_item['quantity'];
}
// Counting other items than "LunchBox"
else {
$items_count++;
}
}
// If product "LunchBox" is in cart, we check the quantity to update it if needed
if ( isset($dcharge_key) && $dcharge_qty != $items_count ) {
$cart->set_quantity( $dcharge_key, $items_count );
}
// If product "LunchBox" is not in cart, we add it
elseif ( ! isset($dcharge_key) && $items_count > 0 ) {
$cart->add_to_cart( $dcharge_id, $items_count );
}
}

我有一个菜=一个午餐盒,三个菜(三个午餐盒(=一包。

我想实现这样的功能。。。

  1. 添加菜肴时,会自动添加"午餐盒"。此外,如果推车中有3个午餐盒;包装";自动添加。

  2. "午餐盒"one_answers"包裹"的名称已经在购物车中,然后它们的数量会自动添加,并计算包装的总成本。

但这里有两个问题:

a。随着一道菜的份量增加,午餐盒的数量并没有增加。

b。我只是不明白如何自动添加另一个产品"包装";如果午餐盒的数量变为"3"。

我会为你的帮助而高兴的!

为了清晰起见:

每个产品都是一道菜,除了"午餐盒"one_answers"包装"也是产品。客户不会自己选择任何东西,一切都是自动发生的。

然后你得到:

function add_delivery_charge_to_cart( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$lunchbox_id  = 5737; // "LunchBox" to be added to cart
$pakket_id = 218; // "Pakket" to be added to cart
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Check if "LunchBox" product is already in cart
if( $cart_item['data']->get_id() == $lunchbox_id ) {
$lunchbox_key = $cart_item_key;
$lunchbox_qty = $cart_item['quantity'];
}

// Check if "Pakket" product is already in cart
if( $cart_item['data']->get_id() == $pakket_id ) {
$pakket_key = $cart_item_key;
$pakket_qty = $cart_item['quantity'];
}       
}

// Get total items in cart, counts number of products and quantity per product
$total_items_in_cart = $cart->get_cart_contents_count();

// If product "LunchBox" is in cart, we check the quantity to update it if needed
if ( isset($lunchbox_key) && $lunchbox_qty != $total_items_in_cart ) {
// Lunchbox total = total_items_in_cart 
$lunchbox_total = $total_items_in_cart;

// Isset lunchbox qty, lunchbox total - lunchbox qty
if ( isset($lunchbox_qty) ) {
$lunchbox_total = $lunchbox_total - $lunchbox_qty;
}
// Isset pakket qty, lunchbox total - pakket qty        
if ( isset($pakket_qty) ) {
$lunchbox_total = $lunchbox_total - $pakket_qty;
} 

// Set quantity, lunchbox
$cart->set_quantity( $lunchbox_key, $lunchbox_total );

} elseif ( !isset($lunchbox_key) && $total_items_in_cart > 0 ) {
// Product "LunchBox" is not in cart, we add it
$cart->add_to_cart( $lunchbox_id, $total_items_in_cart );
}

// Total items in cart greater than or equal to 3
if ( $total_items_in_cart >= 3 ) {
// Pakket total = total_items_in_cart 
$pakket_total = $total_items_in_cart;

// Isset lunchbox qty, pakket total - lunchbox qty
if ( isset($lunchbox_qty) ) {
$pakket_total = $pakket_total - $lunchbox_qty;
}
// Isset pakket qty, pakket total - pakket qty      
if ( isset($pakket_qty) ) {
$pakket_total = $pakket_total - $pakket_qty;
}       

// Pakket total = pakket_total / 3 = floor(result)
// Floor = round fractions down, rounding result down
$pakket_total = floor( $pakket_total / 3 );

// If product "Pakket" is in cart
if ( isset($pakket_key) ) {         
$cart->set_quantity( $pakket_key, $pakket_total );
} elseif ( !isset($pakket_key) ) {
// Product "Pakket" is not in cart, we add it
$cart->add_to_cart( $pakket_id, $pakket_total );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'add_delivery_charge_to_cart', 10, 1 );

相关内容

  • 没有找到相关文章

最新更新