gpt4 book ai didi

php - 自动将产品添加到购物车时排除某些类别 WooCommerce

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

在 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'];
}
}

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

// If product "LunchBox" is in the 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 );

默认情况下,包装会自动添加到所有产品。但对我来说这是错误的。

我需要排除某些类别,例如“饮料”和“面包”,以便包装不会与这些类别中的每个产品一起添加。

我知道可以添加以下代码:
// 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 ) )

只有我不知道如何在上面的代码中正确地做到这一点。

我会很高兴为您提供帮助!

最佳答案

每当产品包含特定类别时,该产品的数量就会添加到 $category_qty_total多变的。此总数随后从购物车中的商品总数中减去

/**
* Calculate the number of lunchboxes and package, based on the number of products in cart.
*/
function add_delivery_charge_to_cart( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;

/********** SETTINGS **********/

$lunchbox_id = 5737; // "LunchBox ID" to be added to cart
$pakket_id = 5738; // "Pakket ID" to be added to cart
$exclude_categories = array( 'drink', 'bread' ); // Exclude these categories

$category_qty_total = 0; // Total of category quantity items, Don't edit!!

/********** LOOP THROUGH CART ITEMS **********/

foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get product id
$product_id = $cart_item['data']->get_id();

// Get product quantity
$product_qty = $cart_item['quantity'];

// Check if "LunchBox" product is already in cart
if( $product_id == $lunchbox_id ) {
$lunchbox_key = $cart_item_key;
$lunchbox_qty = $product_qty;
}

// Check if "Pakket" product is already in cart
if( $product_id == $pakket_id ) {
$pakket_key = $cart_item_key;
$pakket_qty = $product_qty;
}

// Check if product belongs to a certain category
if( has_term( $exclude_categories, 'product_cat', $product_id ) ) {
$category_qty_total += $product_qty;
}
}

/********** CALCULATE THE TOTALS, SO "LUNCHBOX", "PAKKET" & CATEGORIES ARE NOT USED IN THE TOTALS **********/

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

// Total items in cart - category quantity total
$total_items_in_cart -= $category_qty_total;

// Lunchbox total = total_items_in_cart & pakket total = total_items_in_cart
$lunchbox_total = $total_items_in_cart;
$pakket_total = $total_items_in_cart;

// Isset lunchbox qty -> lunchbox total - lunchbox qty & pakket total - lunchbox qty
if ( isset($lunchbox_qty) ) {
$lunchbox_total -= $lunchbox_qty;
$pakket_total -= $lunchbox_qty;
}

// Isset pakket qty -> lunchbox total - pakket qty & pakket total - pakket qty
if ( isset($pakket_qty) ) {
$lunchbox_total -= $pakket_qty;
$pakket_total = $pakket_total - $pakket_qty;
}

/********** APPLY NEW TOTALS TO LUNCHBOX & PAKKET **********/

// 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 ) {
// 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 = 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) ) {
// Set quantity, pakket
$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 );

关于php - 自动将产品添加到购物车时排除某些类别 WooCommerce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60811983/

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