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

根據 Woocommerce 中的特定購物車商品數量自動應用

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

問題描述

限時送ChatGPT賬號..

我正在嘗試在購物車中有 4 件商品時自動觸發要在購物車中應用的優惠券.

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

優惠券是在網站的 Woocommerce 后端預先創建的tasterbox"

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

我正在使用此答案代碼的修正版本:
根據產品類別自動添加 WooCommerce 優惠券代碼

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 件商品時,我無法讓它自動應用優惠券.這似乎是一件很簡單的事情,但我被卡住了.

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

Code goes in function.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)
主站蜘蛛池模板: 一级片在线观看视频 | 日本激情在线 | 日韩激情视频 | 日韩免费成人 | 一色桃子av | 欧美激情免费 | 欧美jizz19性欧美 | 亚洲黄色网址 | 成人免费视频视频 | av影院在线 | 丰满岳乱妇一区二区 | 91看片淫黄大片 | 国产精品久久久久久久久久久久午夜片 | 神马久久久久久 | 国产视频黄色 | 国产免费一级 | 日本一区二区不卡视频 | 黄色高清网站 | 免费av片| 日韩在线一区二区三区 | 日本熟妇毛耸耸xxxxxx | 国产一区二区三区视频 | 中文字幕一区二 | 午夜影院在线观看视频 | 在线不欧美 | 国产日韩欧美日韩大片 | 精品 | 羞羞的视频网站 | 婷婷狠狠| 亚洲精品在线视频 | 亚洲成人av在线播放 | 欧美精品第一页 | 亚洲在线播放 | 国产在线欧美 | 黄色小视频在线播放 | 国产精品99精品久久免费 | 久久久久综合 | 国产福利视频在线观看 | 久久久夜色精品亚洲 | 久久九九精品 | 自拍偷拍福利视频 |