在 WooCommerce 中,我使用代码在将任何菜肴添加到购物车时自动添加包装。
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 = 5738; // "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'];
}
}
add_action( 'woocommerce_before_calculate_totals', 'add_delivery_charge_to_cart', 10, 1 );
默认情况下,包装会自动添加到所有产品中。但对我来说这是错误的。
我需要排除某些类别,例如“饮料”和“面包”,这样包装就不会与这些类别中的每个产品一起添加。
我知道可以添加以下代码:
// Excluded product categories in this array (Can be IDs, slugs or names)
$excl_cats = array( 'drink', 'bread' );
和条件:
if( ! has_term( $excl_cats, 'product_cat', $product_id ) )
只有我不知道如何在上面的代码中正确地做到这一点。
我很乐意为您提供帮助!
PIPIONE
RISEBY
随时随地看视频慕课网APP