亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據 WooCommerce 購物車總數添加或刪除特定購物車項目

根據 WooCommerce 購物車總數添加或刪除特定購物車項目

PHP
不負相思意 2022-10-28 09:38:48
如果訂單總額高于 199.99 美元,我正在嘗試將免費產品添加到購物車我已經實現了這一點并且它正在發揮作用。問題是,如果用戶隨后從購物車中刪除了一個商品并再次低于 199.99 美元(以防止游戲系統),我需要刪除該產品。我所擁有的似乎正在工作。問題是,在 REMOVE FROM CART 操作似乎起作用(或刷新頁面)之前,我似乎需要單擊 2 個鏈接。這是什么原因造成的?是否可以通過 AJAX 完成刪除操作?// -------------------------------------------// ADD PRODUCT IF ORDER MINIMUM ABOVE 200/** Automatically adding the product to the cart when cart total amount reach to $199.99.*/function aapc_add_product_to_cart() {    global $woocommerce;    $cart_total = 199.99;       if ( $woocommerce->cart->total >= $cart_total ) {        if ( is_user_logged_in() ) {            $free_product_id = 339;  // Product Id of the free product which will get added to cart            $found      = false;            //check if product already in cart            if ( sizeof( WC()->cart->get_cart() ) > 0 ) {                foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {                    $_product = $values['data'];                    if ( $_product->get_id() == $free_product_id )                        $found = true;                                  }                // if product not found, add it                if ( ! $found )                    WC()->cart->add_to_cart( $free_product_id );            } else {                // if no products in cart, add it                WC()->cart->add_to_cart( $free_product_id );            }                }    }    if ( $woocommerce->cart->total <= $cart_total && $found ) {                WC()->cart->remove_cart_item( $free_product_id );            }       }add_action( 'template_redirect', 'aapc_add_product_to_cart' );add_action( 'template_redirect', 'remove_product_from_cart_programmatically' );
查看完整描述

1 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

您不應該使用template_redirect鉤子根據購物車的總閾值添加或刪除免費產品……此外,您的代碼有點過時并存在一些錯誤。


而是使用woocommerce_before_calculate_totals啟用 Ajax 的鉤子,這樣:


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 = 200; // The threshold amount for cart total

    $free_product_id  = 339; // ID of the free product

    $cart_items_total = 0; // Initializing


    // 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() == $free_product_id ) {

            $free_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 up to the defined amount and if the free products is not in cart, we add it.

    if ( $cart_items_total >= $threshold_amount && ! isset($free_item_key) ) {

        $cart->add_to_cart( $free_product_id );

    }

    // If cart total is below the defined amount and free product is in cart, we remove it.

    elseif ( $cart_items_total < $threshold_amount && isset($free_item_key) ) {

        $cart->remove_cart_item( $free_item_key );

    }

}

代碼在您的活動子主題(或活動主題)的functions.php 文件中。測試和工作。


查看完整回答
反對 回復 2022-10-28
  • 1 回答
  • 0 關注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號