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

Woocommerce 3 中基于數量的購物車項目折扣

Cart item discount based on quantity in Woocommerce 3(Woocommerce 3 中基于數量的購物車項目折扣)
本文介紹了Woocommerce 3 中基于數量的購物車項目折扣的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我的 WP 網站銷售定制 T 恤.定制插件使每件定制襯衫成為 woocommerce 購物車中的一個行項目.如果訂購了 2 件一件設計的襯衫(該訂單項的數量為 2 件),我想打折.但如果每行只有 1 件商品,我不想打折.

我找到了這個解決方案

<小時><塊引用>

要顯示折扣產品價格而不是原始產品價格,只需刪除woocommerce_cart_item_price()函數(和鉤子)...

最新類似: Woocommerce 3 中的購物車項目數量累進百分比折扣

My WP site sells customized t-shirts. The customization plugin makes each customized shirt a line item in the woocommerce cart. If there are 2 shirts ordered of one design (quantity of 2 on that line item) I want to discount. But if there is just 1 item per line I dont want to discount.

I found this solution

Adding a discount by cart items conditionally based on the item quantity

But this seems to change the product price in the db, so after the discount kicks in for say 2 blue shirts because there were 2 ordered on 1 line, if I add a 3rd shirt on a separate line it also gets the discount, which I dont want.

解決方案

Since woocommerce version 3+ the linked answer code doesn't work. It needs something different and can even be done in a better way.

The code will apply a cart item discount based on the cart item quantity. In this code example, it will apply a discount of 5% on the cart item, when the quatity is equal or more than 2 (two).

The cart item unit price displayed is always the real product price. The discount will be effective and displayed on the cart item subtotal.

Additionally the product name will be appended with a discount mention.

The code:

add_filter('woocommerce_add_cart_item_data', 'add_items_default_price_as_custom_data', 20, 3 );
function add_items_default_price_as_custom_data( $cart_item_data, $product_id, $variation_id ){
    $product_id = $variation_id > 0 ? $variation_id : $product_id;

    ## ----- YOUR SETTING ----- ##
    $discount_percentage = 5; // Discount (5%)

    // The WC_Product Object
    $product = wc_get_product($product_id);

    // Only for non on sale products
    if( ! $product->is_on_sale() ){
        $price = (float) $product->get_price();

        // Set the Product default base price as custom cart item data
        $cart_item_data['base_price'] = $price;

        // Set the Product discounted price as custom cart item data
        $cart_item_data['new_price'] = $price * (100 - $discount_percentage) / 100;

        // Set the percentage as custom cart item data
        $cart_item_data['percentage'] = $discount_percentage;
    }

    return $cart_item_data;
}

// Display the product original price
add_filter('woocommerce_cart_item_price', 'display_cart_items_default_price', 20, 3 );
function display_cart_items_default_price( $product_price, $cart_item, $cart_item_key ){
    if( isset($cart_item['base_price']) ) {
        $product        = $cart_item['data'];
        $product_price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['base_price'] ) ) );
    }
    return $product_price;
}

// Display the product name with the discount percentage
add_filter( 'woocommerce_cart_item_name', 'append_percetage_to_item_name', 20, 3 );
function append_percetage_to_item_name( $product_name, $cart_item, $cart_item_key ){
    if( isset($cart_item['percentage']) && isset($cart_item['base_price']) ) {
        if( $cart_item['data']->get_price() != $cart_item['base_price'] )
            $product_name .= ' <em>(' . $cart_item['percentage'] . '% discounted)</em>';
    }
    return $product_name;
}

add_action( 'woocommerce_before_calculate_totals', 'custom_discounted_cart_item_price', 20, 1 );
function custom_discounted_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    ## ----- YOUR SETTING ----- ##
    $targeted_qty = 2; // Targeted quantity

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {

        // For item quantity of 2 or more
        if( $cart_item['quantity'] >= $targeted_qty && isset($cart_item['new_price']) ){

            // Set cart item discounted price
            $cart_item['data']->set_price($cart_item['new_price']);
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


To display the discounted product price instead of the original product price, just remove woocommerce_cart_item_price() function (and hook)

Newest similar: Cart item quantity progressive percentage discount in Woocommerce 3

這篇關于Woocommerce 3 中基于數量的購物車項目折扣的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節(jié)已用盡(嘗試分配 87 字節(jié)))
主站蜘蛛池模板: 91免费看片 | 亚洲精品电影网在线观看 | 日韩免费高清视频 | 成年男女免费视频网站 | 日韩成人免费视频 | 韩国成人在线视频 | 国产精品久久久久久久久久久久久久 | 色一情一乱一伦一区二区三区 | 91精品国产欧美一区二区成人 | 97超碰人人草 | 久久婷婷av | 国产精品亚洲综合 | 亚洲影音 | 影音先锋中文字幕在线观看 | 日韩在线免费看 | 色综合一区二区 | 久久久99国产精品免费 | 久久免费国产 | 国产一区二区三区在线看 | 久草免费在线视频 | 国产在线一区二区三区 | 亚州一区二区三区 | 国产一区二区三区在线免费观看 | 久久精品一区二区三区四区 | 蜜桃视频一区二区三区 | 欧美精品a∨在线观看不卡 欧美日韩中文字幕在线播放 | 免费在线观看一区二区 | 欧美最猛黑人xxxⅹ 粉嫩一区二区三区四区公司1 | 国产影音先锋 | 日韩av一区二区在线观看 | 91 久久| 国产激情精品 | 日日摸夜夜添夜夜添特色大片 | 亚洲国产成人精品女人久久久 | 国产成都精品91一区二区三 | 视频一区二区三区四区五区 | 日本网站在线看 | 精品国产一区二区三区av片 | 欧美精品一区在线 | 正在播放国产精品 | 日本欧美视频 |