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

購物車折扣基于購物車商品數量且僅適用于未銷

Cart discount based on cart item count and only for items that are not in sale(購物車折扣基于購物車商品數量且僅適用于未銷售的商品)
本文介紹了購物車折扣基于購物車商品數量且僅適用于未銷售的商品的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

在 WooCommerce 中,我想專門為那些未銷售的產品提供 10% 的折扣.如果購物車中的商品數量為 5 件或更多商品并且沒有打折,那么我會提供 10% 的折扣.

In WooCommerce, I would like to give a discount of 10% specifically for those products that are not on sale. If cart item count is 5 or more items and not on sale, then I give a discount of 10%.

我使用以下代碼根據此處的購物車商品數量限制獲得折扣:

I use the following code to get a discount based on cart item count restriction here:

add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');

/**
* Add custom fee if more than three article
* @param WC_Cart $cart
*/

function add_custom_fees( WC_Cart $cart ){
     if( $cart->cart_contents_count < 5 ){
         return;
     } 
    // Calculate the amount to reduce
    $discount = $cart->subtotal * 0.1;
    $cart->add_fee( '10% discount', -$discount);
} 

但我不知道如何僅對非銷售商品應用折扣.我怎樣才能實現它?

But I don't know how to apply the discount only for items that are not in sale. How can I achieve it?

謝謝.

推薦答案

這是一個自定義掛鉤函數,如果購物車中有 5 件或更多商品且沒有產品,則該函數將應用于購物車 折扣特價:

Here is a custom hooked function that will apply to cart a discount, if there is 5 or more items in cart and no products on sale:

add_action('woocommerce_cart_calculate_fees' , 'custom_discount', 10, 1);
function custom_discount( $cart ){

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

    // Only when there is 5 or more items in cart
    if( $cart->get_cart_contents_count() >= 5):

        // Initialising variable
        $is_on_sale = false;

        // Iterating through each item in cart
        foreach( $cart->get_cart() as $cart_item ){
            // Getting an instance of the product object
            $product =  $cart_item['data'];

            // If a cart item is on sale, $is_on_sale is true and we stop the loop
            if($product->is_on_sale()){
                $is_on_sale = true;
                break;
            }
        }

        ## Discount calculation ##
        $discount = $cart->subtotal * -0.1;

        ## Applied discount (no products on sale) ##
        if(!$is_on_sale )
            $cart->add_fee( '10% discount', $discount);

    endif;
}

此代碼位于活動子主題(或主題)的 function.php 文件或任何插件文件中.

此代碼已經過測試并且運行良好.

This code is tested and works perfectly.

這篇關于購物車折扣基于購物車商品數量且僅適用于未銷售的商品的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
主站蜘蛛池模板: 国产精品一区二区欧美 | 久久国产亚洲 | 伊人久久伊人 | 中文字幕亚洲欧美 | 夜色www国产精品资源站 | 综合网视频 | 91夜色在线观看 | 久久成人精品一区二区三区 | 99国产精品久久久 | 91在线影院 | 在线中文视频 | 亚洲视频中文 | 成人一区二区三区在线 | 97免费视频在线观看 | 日韩视频在线播放 | 久久美女网 | 中文字幕日韩在线 | 久久久国产一区二区三区 | 日韩电影免费观看中文字幕 | 国产高清在线精品一区二区三区 | jizz在线免费观看 | 日韩福利电影 | 四虎影院久久 | 久久夜夜| 精品一区二区三区在线视频 | 欧美成人久久 | aaa在线| 国产成人精品免费 | 99福利网| 视频一区二区三区中文字幕 | 欧美精品久久久 | 亚洲精品亚洲人成人网 | 国产欧美日韩综合精品一 | 久久午夜视频 | 一区二区三区视频免费看 | 日韩高清国产一区在线 | 国产精品一区二区久久久久 | 宅男噜噜噜66一区二区 | 91精品观看 | 亚洲免费久久久 | 一级做a爰片久久毛片 |