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

WooCommerce 折扣:買一送一 50%

WooCommerce discount: buy one get one 50% off(WooCommerce 折扣:買一送一 50%)
本文介紹了WooCommerce 折扣:買一送一 50%的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我希望為特定的可變產品設置特定折扣,如果客戶購買一種產品,他們會以 50% 的折扣獲得另一種(相同的)(買一個 50% 的折扣).我試過很多折扣插件,我發現最接近的是:

I wish to set up a specific discount on a particular variable products, if customer buys one product they get the another(same) on 50% discount(Buy one get another for 50% off). I've tried many discount plugins buy the closest that I have found are:

  • WooCommerce 的定價交易

  • Pricing Deals for WooCommerce

WooCommerce 所有折扣精簡版

WooCommerce All Discounts Lite

通過使用這些插件,我可以為每個產品設置小計折扣或折扣,但不完全是我想要的(買 1 送 1).還有其他專業插件我不想去.

By using these plugins I was able to setup discount on subtotal or discount on a each product but not exactly what I am looking for(Buy 1 get 1 off). There are other pro plugins I don't want to go for it.

不買插件可以實現嗎?

謝謝

發現類似的東西https://www.fldtrace.com/buy-3-get-1-free-coupon-woocommerce

推薦答案

更新 (與你的評論有關)

此版本將在此定義的可變產品的購物車中的所有產品變體上全局運行:

This version will work globally on all product variations in the cart for this defined variable product:

add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1 );
function add_custom_discount_2nd_at_50( $wc_cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $discount = 0;
    $items_prices = array();

    // Set HERE your targeted variable product ID
    $targeted_product_id = 40;

    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
        if( $cart_item['product_id'] == $targeted_product_id ){
            $qty = intval( $cart_item['quantity'] );
            for( $i = 0; $i < $qty; $i++ )
                $items_prices[] = floatval( $cart_item['data']->get_price());
        }
    }
    $count_items_prices = count($items_prices);
    if( $count_items_prices > 1 ) foreach( $items_prices as $key => $price )
        if( $key % 2 == 1 ) $discount -= number_format($price / 2, 2 );

    if( $discount != 0 ){
        // Displaying a custom notice (optional)
        wc_clear_notices();
        wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');

        // The discount
        $wc_cart->add_fee( 'Discount 2nd at 50%', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

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

此代碼已在 Woocommerce 3+ 上進行測試并有效.

This code is tested on Woocommerce 3+ and works.

原答案:

有多種方法可以為特定可變產品 ID 的第二件商品添加 50% 的自定義折扣.下面我使用 add_fee() 方法和一個負值 (所以它增加了折扣).
可選擇顯示自定義通知:

There is many ways to do add a custom discount of 50% on the 2nd item for a specific variable product ID. Below I am using add_fee() method with a negative value (so it adds a discount).
Optionally it will display a custom notice:

add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1 );
function add_custom_discount_2nd_at_50( $wc_cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $discount = 0;

    // Set HERE your targeted variable product ID
    $targeted_product_id = 40;

    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
        if( $cart_item['product_id'] == $targeted_product_id ){
            $price = $cart_item['data']->get_price();
            $quantity = intval( $cart_item['quantity'] );
            for( $i = 1, $j = 0; $i <= $quantity; $i++ ){
                if( $i % 2 == 0 &&  $quantity > 1 ) $j++;
            }
            if( $quantity > 1 ) number_format($discount -= $price * $j / 2, 2 );
        }
    }
    if( $discount != 0 ){
        // Displaying a custom notice (optional)
        wc_clear_notices();
        wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');
        
        $wc_cart->add_fee( 'Discount 2nd at 50%', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

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

此代碼已在 Woocommerce 3+ 上進行測試并有效.

This code is tested on Woocommerce 3+ and works.

相關:WooCommerce折扣:買一送一 50% 折扣

這篇關于WooCommerce 折扣:買一送一 50%的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
主站蜘蛛池模板: 在线观看国产视频 | 亚洲精品久久久久久久久久久久久 | 亚洲精品视频一区 | a国产视频 | 成年人网站免费视频 | 日本成人二区 | 古装人性做爰av网站 | 亚洲精品久久区二区三区蜜桃臀 | a级黄色毛片免费播放视频 国产精品视频在线观看 | 国产性生活一级片 | 91免费看片| 欧美一区二区三区在线免费观看 | 久久久久免费观看 | 一区二区三区中文字幕 | 久久99深爱久久99精品 | 国产一区二区在线播放 | 亚洲精品国产第一综合99久久 | 日韩成人免费中文字幕 | 日韩精品一区二区三区第95 | 欧美日韩亚洲一区 | 一区视频在线 | 亚洲人成人一区二区在线观看 | www.玖玖玖 | 日韩精品一区二区不卡 | 国产免费人成xvideos视频 | 91p在线观看 | 欧美日韩国产精品一区 | 国产探花在线精品一区二区 | 综合久久一区 | 亚洲高清免费视频 | 欧美一级免费 | 18gay男同69亚洲网站 | 日本高清aⅴ毛片免费 | 男女网站视频 | 最新中文字幕第一页视频 | 欧美中文一区 | 中文字幕精品一区 | 亚洲精选久久 | 午夜色婷婷| 国产一区二区三区精品久久久 | 999久久久久久久 |