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

根據(jù) Woocommerce 中的特定購物車商品數(shù)量自動應(yīng)用

Apply automatically a coupon based on specific cart items count in Woocommerce(根據(jù) Woocommerce 中的特定購物車商品數(shù)量自動應(yīng)用優(yōu)惠券)
本文介紹了根據(jù) Woocommerce 中的特定購物車商品數(shù)量自動應(yīng)用優(yōu)惠券的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試在購物車中有 4 件商品時自動觸發(fā)要在購物車中應(yīng)用的優(yōu)惠券.

I am trying to automatically trigger a coupon to be applied in the cart specifically for when there are 4 items in the cart.

優(yōu)惠券是在網(wǎng)站的 Woocommerce 后端預(yù)先創(chuàng)建的tasterbox"

The coupon is pre-created in the Woocommerce back-end of the site "tasterbox"

我正在使用此答案代碼的修正版本:
根據(jù)產(chǎn)品類別自動添加 WooCommerce 優(yōu)惠券代碼

I am using an amended version from this answer code:
Add WooCommerce coupon code automatically based on product categories

這是我的代碼版本:

add_action( 'woocommerce_before_calculate_totals', 'wc_auto_add_coupons', 10, 1 );
function wc_auto_add_coupons( $cart_object ) {

    // Coupon code
    $coupon = 'tasterbox';

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Initialising variables
    $is_match = false;
    $taster_item_count = 4;

    //  Iterating through each cart item
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // If cart items match 4
        if( $cart->cart_contents_count == $taster_item_count ){
            $is_match = true; // Set to true
            break; // stop the loop
        }
    }

    // If conditions are matched add the coupon discount
    if( $is_match && ! $cart_object->has_discount( $coupon )){
        // Apply the coupon code
        $cart_object->add_discount( $coupon );

        // Optionally display a message 
        wc_add_notice( __('TASTER BOX ADDED'), 'notice');
    } 
    // If conditions are not matched and coupon has been appied
    elseif( ! $has_category && $cart_object->has_discount( $coupon )){
        // Remove the coupon code
        $cart_object->remove_coupon( $coupon );

        // Optionally display a message 
        wc_add_notice( __('SORRY, TASTERBOX NOT VALID'), 'alert');
    }
}

但是,當購物車中有 4 件商品時,我無法讓它自動應(yīng)用優(yōu)惠券.這似乎是一件很簡單的事情,但我被卡住了.

However I can not get it to auto apply the coupon when there are 4 items in the cart. It seems like something simple to do, but I'm stuck.

感謝任何幫助.

推薦答案

您的代碼中存在一些小錯誤和錯誤.請嘗試以下操作:

There is some little mistakes and errors in your code. Try the following instead:

add_action( 'woocommerce_before_calculate_totals', 'auto_add_coupon_based_on_cart_items_count', 25, 1 );
function auto_add_coupon_based_on_cart_items_count( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Setting and initialising variables
    $coupon = 'tasterbox'; // <===  Coupon code
    $item_count = 4; // <===  <===  Number of items
    $matched    = false;

    if( $cart->cart_contents_count >= $item_count ){
        $matched = true; // Set to true
    }

    // If conditions are matched add coupon is not applied
    if( $matched && ! $cart->has_discount( $coupon )){
        // Apply the coupon code
        $cart->add_discount( $coupon );

        // Optionally display a message
        wc_add_notice( __('TASTER BOX ADDED'), 'notice');
    }
    // If conditions are not matched and coupon has been appied
    elseif( ! $matched && $cart->has_discount( $coupon )){
        // Remove the coupon code
        $cart->remove_coupon( $coupon );

        // Optionally display a message
        wc_add_notice( __('SORRY, TASTERBOX NOT VALID'), 'error');
    }
}

代碼位于活動子主題(或活動主題)的 function.php 文件中.經(jīng)測試有效.

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

這篇關(guān)于根據(jù) Woocommerce 中的特定購物車商品數(shù)量自動應(yīng)用優(yōu)惠券的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(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 簡單產(chǎn)品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 激情免费视频 | 欧美性成人 | 午夜免费福利电影 | 国产精品一区网站 | 久久久国产一区二区三区四区小说 | 久久久久久av | 欧美性猛交一区二区三区精品 | 精品乱码一区二区 | 91久久久久久久久 | 超碰在线97国产 | 中文字幕亚洲精品 | 国产亚洲精品精品国产亚洲综合 | 日韩影音 | 午夜精品久久久久久久 | 亚洲一区二区av | 久久中文字幕电影 | 99视频在线 | 日韩一区二区三区在线视频 | 欧美激情综合色综合啪啪五月 | 一区二区精品在线 | 九色 在线 | 全免费a级毛片免费看视频免 | 天天操一操| 成人欧美一区二区三区黑人孕妇 | 欧美一区二区久久 | 国内精品免费久久久久软件老师 | 国产黄色av网站 | 久久久资源 | 中文在线一区二区 | 午夜久久久| 欧美精品一区二区三区四区五区 | 国产91丝袜在线播放 | 在线观看成人免费视频 | 欧美在线a| 中文字幕在线免费视频 | 色综合色综合色综合 | 福利视频大全 | 粉嫩国产精品一区二区在线观看 | 高清成人免费视频 | 国产剧情一区 | 日韩精品色网 |