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

WooCommerce 折扣:買一送一 50% 的折扣通知

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

問題描述

限時(shí)送ChatGPT賬號..

在我之前的問題之后WooCommerce 折扣:買一送一個(gè) 50% 的折扣我想在購物車中添加自定義通知,每當(dāng)特定產(chǎn)品(不是所有產(chǎn)品)被添加到購物車時(shí).

After my previous question WooCommerce discount: buy one get one 50% off I want to add custom notice to the cart, whenever a particular product(not all the products) gets added to the cart.

我想先檢查數(shù)量,如果是 1,那么我想顯示增加該產(chǎn)品數(shù)量的通知.我自己從互聯(lián)網(wǎng)上想出了一些東西,但我認(rèn)為我的解決方案不正確:

I want to check the quantity first, if it's 1 then I want to display a notice to increase the quantity of that product. I have figured something by myself from the internet and I don't think my solution is right:

add_action( 'wp', 'sp_custom_notice' );
function sp_custom_notice() {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    
    //to display notice only on cart page
    if ( ! is_cart() ) {
        return;
    }

    global $product;

    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    if($product_id == 15730){
        //check for quantify if equal to 1 in cart


        wc_clear_notices();
        wc_add_notice( __("Add one more to get 50% off on 2nd product"), 'notice');
    }
}

如果有人能幫助我,那就太好了.

It will be great if anyone can help me on this.

推薦答案

更新 2020 年 7 月

如果您仍在使用我的回答中的代碼 對于您之前的問題,您也可以對其進(jìn)行一些更改以使其正常工作:

If you are still using the code from my answer to your previous question, you can change it a bit to get this working too:

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

    // YOUR SETTINGS:
    $targeted_product_id = 40; // Set HERE your targeted product ID

    // Initializing variables
    $discount = $qty_notice = 0;
    $items_prices = array();

    // Loop through cart items
    foreach ( $cart->get_cart() as $key => $cart_item ) {
        if( in_array( $targeted_product_id, [$cart_item['product_id'], $cart_item['variation_id']] ) ){
            $quantity = (int) $cart_item['quantity'];
            $qty_notice += $quantity;
            for( $i = 0; $i < $quantity; $i++ ) {
                $items_prices[] = floatval( $cart_item['data']->get_price());
            }
        }
    }

    $count_items = count($items_prices); // Count items

    rsort($items_prices); // Sorting prices descending order

    if( $count_items > 1 ) {
        foreach( $items_prices as $key => $price ) {
            if( $key % 2 == 1 )
                $discount -= number_format( $price / 2, 2 );
        }
    }

    // Applying the discount
    if( $discount != 0 ){
        $cart->add_fee('Buy one get one 50% off', $discount );

        // Displaying a custom notice (optional)
        wc_clear_notices(); // clear other notices on checkout page.
        if( ! is_checkout() ){
            wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');
        }
    }
    //  Display a custom notice on cart page when quantity is equal to 1.
    elseif( $qty_notice == 1 ){
        wc_clear_notices(); // clear other notices on checkout page.
        if( ! is_checkout() ){
            wc_add_notice( __( "Add one more to get 50% off on 2nd item" ), 'notice');
        }
    }
}

代碼進(jìn)入您的活動(dòng)子主題(或活動(dòng)主題)的functions.php文件經(jīng)過測試并有效.

注意:wc_add_notice()函數(shù)絕對不需要回顯

這篇關(guān)于WooCommerce 折扣:買一送一 50% 的折扣通知的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 和電話字段驗(yàn)證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產(chǎn)品中添加一個(gè)將更改價(jià)格的選擇字段)
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 | 精品国产区 | 亚洲午夜久久久 | av官网在线 | 国产精品久久久久无码av | 91视频免费在观看 | 国产丝袜一区二区三区免费视频 | www.性色| 免费毛片网站在线观看 | 精品成人佐山爱一区二区 | jizz18国产| 欧美一级免费黄色片 | 日韩精品在线观看网站 | 国产精品久久久久久久久久久新郎 | 国产在线一区二区 | 看片国产 | av一级| 在线欧美| 亚洲激情视频在线 | 国产露脸国语对白在线 | 日本久久精品视频 | 成人在线精品视频 | 九九热这里只有精品在线观看 | 犬夜叉在线观看 | 日韩欧美综合 | 国产一区在线免费观看视频 | 久久9久 | 久久久久久国产 | 日本中文字幕一区 | 天天插天天操 | 亚洲天堂免费在线 | 亚洲精品久久久久久久久久久久久 | 99tv| 亚洲一区二区免费看 | 99精品视频一区二区三区 | 欧美另类视频 | www网站在线观看 | 一区二区三区在线 | 欧 |