久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

根據(jù) WooCommerce 購物車總數(shù)添加或刪除特定購物車

Add or remove specific cart Item based on WooCommerce cart total(根據(jù) WooCommerce 購物車總數(shù)添加或刪除特定購物車項目)
本文介紹了根據(jù) WooCommerce 購物車總數(shù)添加或刪除特定購物車項目的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

如果訂單總額超過 199.99 美元,我將嘗試將免費產品添加到購物車

I am trying to add a free product to the cart if the order total is above $199.99

我已經(jīng)實現(xiàn)了這一點,并且正在發(fā)揮作用.問題是,如果用戶隨后從購物車中刪除了一件商品并再次低于 199.99 美元(以防止玩弄系統(tǒng)),我需要刪除該產品.

I have achieved this and it is working. The issue is that I need to remove the product if the user then deletes an item from the cart and goes below $199.99 again (to prevent gaming the system).

我所擁有的似乎正在發(fā)揮作用.問題是,在 REMOVE FROM CART 操作似乎起作用(或刷新頁面)之前,我似乎需要單擊 2 個鏈接.

What I have seems to be working. The problem is that it seems I need to click 2 links before the REMOVE FROM CART action seems to be working (or refresh the page).

這是什么原因造成的?是否可以通過 AJAX 完成刪除操作?

What is causing this? Can the remove action be accomplished with AJAX by any chance?

// -------------------------------------------
// 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' );

function remove_product_from_cart_programmatically() {
    if ( is_admin() ) return;
    $product_id = 339; // product id
    $cart_total = 199.99;
    $in_cart = false;
    foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( $cart_item['product_id'] === $product_id ) {
            $in_cart = true;
            $key = $cart_item_key;
            break;
        }
    }
    if( WC()->cart->total < $cart_total ) {
        if ( $in_cart ) WC()->cart->remove_cart_item( $key );
    }
}

推薦答案

你不應該使用 template_redirect 鉤子來添加或刪除基于購物車總閾值數(shù)量的免費產品......而且你的代碼是一個有點過時,有一些錯誤.

You should not use template_redirect hook to add or remove a free product based on a cart total threshold amount… Also your code is a bit outdated with some mistakes.

改為使用啟用 Ajax 的 woocommerce_before_calculate_totals 鉤子,這樣:

Instead use woocommerce_before_calculate_totals hook that is Ajax enabled, this way:

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 文件中.經(jīng)測試有效.

Code goes on functions.php file of your active child theme (or active theme). Tested and works.

相關:其他類似的回答主題

這篇關于根據(jù) WooCommerce 購物車總數(shù)添加或刪除特定購物車項目的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 久久综合九九 | 亚洲不卡在线视频 | 国产美女黄色片 | 久久成人一区 | 中文字幕亚洲一区二区三区 | 四虎永久在线精品免费一区二 | 久久一区二区免费视频 | 亚洲国产免费 | 欧美一级高清片 | 超碰在线人人 | 老子午夜影院 | 久久久www成人免费无遮挡大片 | 91精品一区 | 狠狠操av| 天堂男人av| 国产三级一区二区 | 亚洲成人在线免费 | 欧美精品久久久久久久久老牛影院 | 精品一区二区三区四区 | 欧美日韩成人一区二区 | 极品粉嫩国产48尤物在线播放 | 午夜天堂精品久久久久 | 亚洲国产成人精品女人久久久 | 一区二区福利视频 | 欧美日韩精品 | 一区中文字幕 | 国户精品久久久久久久久久久不卡 | 成人综合视频在线 | 日韩精品一区二区三区中文在线 | 一级黄色录像毛片 | 在线视频一区二区三区 | 中文在线一区 | 久久伦理电影 | 欧美一区二区三区 | 中文字字幕在线中文乱码范文 | 日韩精品久久一区二区三区 | 91久久久久久久久久久久久 | 欧美一区二区三区四区视频 | 中文字幕av一区二区三区 | 国产成人在线一区二区 | 成人午夜精品 |