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

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

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

問題描述

限時送ChatGPT賬號..

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

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.

我想先檢查數量,如果是 1,那么我想顯示增加該產品數量的通知.我自己從互聯網上想出了一些東西,但我認為我的解決方案不正確:

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 月

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

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');
        }
    }
}

代碼進入您的活動子主題(或活動主題)的functions.php文件經過測試并有效.

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

這篇關于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)
主站蜘蛛池模板: 亚洲九色 | 国产乱码久久久久久一区二区 | 在线视频成人 | 精品国产一区探花在线观看 | 四虎影视免费观看 | 亚洲天堂av在线 | 精品三区 | 日韩一区二区在线视频 | 91在线精品视频 | 欧美日韩三区 | 国产视频中文字幕 | 综合久久久 | 在线区 | 精品一区二区三区中文字幕 | 国产免费一区二区三区网站免费 | 精品日韩欧美一区二区 | 一区二区亚洲 | 国产亚洲欧美日韩精品一区二区三区 | 久久亚洲国产 | av黄色在线观看 | 国产乱码久久久久久 | 国产一区二区影院 | 久久精品成人 | 欧美理论片在线 | 成人视屏在线观看 | 国产高潮好爽受不了了夜夜做 | 91精品一区二区三区久久久久久 | 久久狠狠 | 欧美日韩在线一区 | 毛色毛片免费看 | 亚洲一级淫片 | 国产精品久久久久久久久图文区 | 国产一区二区免费 | 中文字幕三区 | 高清av在线| 欧美欧美欧美 | 午夜国产一区 | 欧美一级在线 | 日韩小视频在线 | 亚洲国产成人精品久久 | 久久久久久久一级 |