pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問題描述

限時送ChatGPT賬號..

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

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

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

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).

我所擁有的似乎正在發揮作用.問題是,在 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 鉤子來添加或刪除基于購物車總閾值數量的免費產品......而且你的代碼是一個有點過時,有一些錯誤.

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 文件中.經測試有效.

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

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

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

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

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
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)
主站蜘蛛池模板: 金蝶帐无忧|云代账软件|智能财税软件|会计代账公司专用软件 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | 济南ISO9000认证咨询代理公司,ISO9001认证,CMA实验室认证,ISO/TS16949认证,服务体系认证,资产管理体系认证,SC食品生产许可证- 济南创远企业管理咨询有限公司 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 地源热泵一体机,地源热泵厂家-淄博汇能环保设备有限公司 | Copeland/谷轮压缩机,谷轮半封闭压缩机,谷轮涡旋压缩机,型号规格,技术参数,尺寸图片,价格经销商 CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 | 快速门厂家批发_PVC快速卷帘门_高速门_高速卷帘门-广州万盛门业 快干水泥|桥梁伸缩缝止水胶|伸缩缝装置生产厂家-广东广航交通科技有限公司 | 纳米二氧化硅,白炭黑,阴离子乳化剂-臻丽拾科技 | 海鲜池-专注海鲜鱼缸、移动海鲜缸、饭店鱼缸设计定做-日晟水族厂家 | 求是网 - 思想建党 理论强党 | 办公室家具_板式办公家具定制厂家-FMARTS福玛仕办公家具 | 喷砂机厂家_自动喷砂机生产_新瑞自动化喷砂除锈设备 | led冷热冲击试验箱_LED高低温冲击试验箱_老化试验箱-爱佩百科 | 上海洗地机-洗地机厂家-全自动洗地机-手推式洗地机-上海滢皓洗地机 | 冷藏车-东风吸污车-纯电动环卫车-污水净化车-应急特勤保障车-程力专汽厂家-程力专用汽车股份有限公司销售二十一分公司 | 隐形纱窗|防护纱窗|金刚网防盗纱窗|韦柏纱窗|上海青木装潢制品有限公司|纱窗国标起草单位 | 安驭邦官网-双向万能直角铣头,加工中心侧铣头,角度头[厂家直销] 闸阀_截止阀_止回阀「生产厂家」-上海卡比阀门有限公司 | 蒸汽热收缩机_蒸汽发生器_塑封机_包膜机_封切收缩机_热收缩包装机_真空机_全自动打包机_捆扎机_封箱机-东莞市中堡智能科技有限公司 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | 焊缝跟踪系统_激光位移传感器_激光焊缝跟踪传感器-创想智控 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | 医养体检包_公卫随访箱_慢病随访包_家签随访包_随访一体机-济南易享医疗科技有限公司 | 特材真空腔体_哈氏合金/镍基合金/纯镍腔体-无锡国德机械制造有限公司 | SDG吸附剂,SDG酸气吸附剂,干式酸性气体吸收剂生产厂家,超过20年生产使用经验。 - 富莱尔环保设备公司(原名天津市武清县环保设备厂) | 伶俐嫂培训学校_月嫂培训班在哪里报名学费是多少_月嫂免费政府培训中心推荐 | 收录网| 全自动五线打端沾锡机,全自动裁线剥皮双头沾锡机,全自动尼龙扎带机-东莞市海文能机械设备有限公司 | 压接机|高精度压接机|手动压接机|昆明可耐特科技有限公司[官网] 胶泥瓷砖胶,轻质粉刷石膏,嵌缝石膏厂家,腻子粉批发,永康家德兴,永康市家德兴建材厂 | 石英砂矿石色选机_履带辣椒色选机_X光异物检测机-合肥幼狮光电科技 | 汽车润滑油厂家-机油/润滑油代理-高性能机油-领驰慧润滑科技(河北)有限公司 | 筛分机|振动筛分机|气流筛分机|筛分机厂家-新乡市大汉振动机械有限公司 | 防爆大气采样器-防爆粉尘采样器-金属粉尘及其化合物采样器-首页|盐城银河科技有限公司 | 钢结构-钢结构厂房-钢结构工程[江苏海逵钢构厂] | 双相钢_双相不锈钢_双相钢圆钢棒_双相不锈钢报价「海新双相钢」 双能x射线骨密度检测仪_dxa骨密度仪_双能x线骨密度仪_品牌厂家【品源医疗】 | 体视显微镜_荧光生物显微镜_显微镜报价-微仪光电生命科学显微镜有限公司 | 控显科技 - 工控一体机、工业显示器、工业平板电脑源头厂家 | 热熔胶网膜|pes热熔网膜价格|eva热熔胶膜|热熔胶膜|tpu热熔胶膜厂家-苏州惠洋胶粘制品有限公司 | 电机修理_二手电机专家-河北豫通机电设备有限公司(原石家庄冀华高压电机维修中心) | 道康宁消泡剂-瓦克-大川进口消泡剂供应商 | 北京租车公司_汽车/客车/班车/大巴车租赁_商务会议/展会用车/旅游大巴出租_北京桐顺创业租车公司 | 上海新光明泵业制造有限公司-电动隔膜泵,气动隔膜泵,卧式|立式离心泵厂家 | 旅游规划_旅游策划_乡村旅游规划_景区规划设计_旅游规划设计公司-北京绿道联合旅游规划设计有限公司 |