![](/img/trans.png)
[英]Woocommerce: Item B is purchasable if item A added to the cart
[英]WooCommerce custom code deleting item as soon as it's added to cart
我正在嘗試設置自定義優惠券代碼,以便客戶可以將附加產品添加到他們的購物車中,前提是他們的購物車總額為 50 美元或更多。
我在 WooCommerce 答案代碼中使用允許根據購物車總數將特定產品添加到購物車,該回答代碼允許或拒絕根據購物車總數添加產品,這很好用。
我的問題是,如果插件項目在購物車中並且購物車總數不再在閾值內,我還有一段代碼應該刪除插件項目。
就我而言,我將閾值設置為 50 美元,附加產品的價格為 10 美元。
在測試中,我發現當購物車總額為 60 美元或更多時,一切正常。 我什至可以從購物車中刪除幾個項目,並且插件項目將一直保留到總數低於閾值,然后它也會被刪除。
但是,如果我在 go 添加插件項目時購物車總額在 50 美元到 60 美元之間,則插件項目一添加就會立即刪除。
我希望操作順序為 go
基於基於 WooCommerce 購物車總答案代碼添加或刪除特定購物車項目,這是我正在使用的代碼:
add_action( 'woocommerce_before_calculate_totals', 'add_or_remove_cart_items', 10, 1 );
function add_or_remove_cart_items( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// ONLY for logged users (and avoiding the hook repetition)
if ( ! is_user_logged_in() && did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$threshold_amount = 50; // The threshold amount for cart total
$addon_product_id = 7039; // ID of the addon item
$addon_product_cost = 10; // Cost of addon item
$cart_items_total = 0; // Initializing
$threshold_amount_with_addon = $threshold_amount + $addon_product_cost;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
// Check if the free product is in cart
if ( $cart_item['data']->get_id() == $addon_product_id ) {
$addon_item_key = $cart_item_key;
}
// Get cart subtotal incl. tax from items (with discounts if any)
$cart_items_total += $cart_item['line_total'] + $cart_item['line_tax'];
}
// If cart total is below the defined amount and addon product is in cart, we remove it.
if ( $cart_items_total < $threshold_amount_with_addon && isset($addon_item_key) ) {
$cart->remove_cart_item( $addon_item_key );
return;
}
}
我一生都無法弄清楚出了什么問題。 任何幫助將非常感激。
下面,我們從小計計算中排除了附加項目,因此我們不需要將附加價格添加到閾值金額(因此我們保留您的步驟 1、2 和 4)。
重新訪問的代碼:
add_action( 'woocommerce_before_calculate_totals', 'add_or_remove_cart_items', 10, 1 );
function add_or_remove_cart_items( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// ONLY for logged users, avoiding the hook repetition
if ( ! is_user_logged_in() || did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$addon_product_id = 7039; // ID of the addon item
$threshold_amount = 50; // The threshold amount
$cart_items_total = 0; // Initializing
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
// For Add-On product: Check if it is in cart
if ( in_array( $addon_product_id, [$cart_item['product_id'], $cart_item['variation_id']] ) ) {
$addon_item_key = $cart_item_key;
}
// For other items: add their line total including taxes to items subtotal
else {
// Sum dicounted line total incl. taxes to get other items subtotal
$cart_items_total += $cart_item['line_total'] + $cart_item['line_tax'];
}
}
// If cart total is below the defined amount and addon product is in cart, we remove it.
if ( $cart_items_total < $threshold_amount && isset($addon_item_key) ) {
$cart->remove_cart_item( $addon_item_key );
}
}
代碼進入活動子主題(或活動主題)的functions.php文件。 測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.