在 WooCommerce 中将产品添加到购物车时自动添加包装
Automatically add packaging when adding product to cart in 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 );
}
}
我一盘=一盒,三盘(三盒)=一包。
我想实现这样的功能...
添加菜品时,会自动添加“饭盒”。此外,如果购物车中有 3 个饭盒,则会自动添加一个“套餐”。
如果名称“Lunchbox”和“Package”已经在购物车中,那么它们的数量会自动添加并计算包装总成本。
但是这里有两个问题:
一个。随着已经在购物车中的一道菜份数的增加,饭盒的数量并没有增加。
b。就是想不明白,如果饭盒数量变成“3”,如何自动添加另一个产品“套餐”。
我很乐意为您提供帮助!
为清楚起见:
除'lunch box'和'package'也是产品外,其他产品都是一道菜。客户不会select自己做任何事情,一切都会自动发生。
然后你得到:
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 );
在 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 );
}
}
我一盘=一盒,三盘(三盒)=一包。
我想实现这样的功能...
添加菜品时,会自动添加“饭盒”。此外,如果购物车中有 3 个饭盒,则会自动添加一个“套餐”。
如果名称“Lunchbox”和“Package”已经在购物车中,那么它们的数量会自动添加并计算包装总成本。
但是这里有两个问题:
一个。随着已经在购物车中的一道菜份数的增加,饭盒的数量并没有增加。
b。就是想不明白,如果饭盒数量变成“3”,如何自动添加另一个产品“套餐”。
我很乐意为您提供帮助!
为清楚起见:
除'lunch box'和'package'也是产品外,其他产品都是一道菜。客户不会select自己做任何事情,一切都会自动发生。
然后你得到:
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 );