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

根據購物車物品重量和購物車數量計算運費

Shipping calculated on cart items weight and cart amount(根據購物車物品重量和購物車數量計算運費)
本文介紹了根據購物車物品重量和購物車數量計算運費的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

在客戶端 WooCommerce 網站中,為最多 250 個訂單啟用免費送貨方式.我使用下面的代碼(來自 這個答案),當訂單金額超過 250 時隱藏其他運費,除非有重購物車中的商品.

In a client WooCommerce web site, free shipping method is enabled for orders amount up to 250. I use the code below (from this answer), to hide other shipping rates when the order amount is over 250, except when there is heavy items in cart.

add_filter( 'woocommerce_package_rates', 'conditionally_hide_other_shipping_based_on_items_weight', 100, 1 );
function conditionally_hide_other_shipping_based_on_items_weight( $rates ) {
    // targeted weight
    $target_product_weight = 12;
    $target_cart_amount = 250;

    WC()->cart->subtotal_ex_tax >= $target_cart_amount ? $passed = true : $passed = false ;

    // Iterating trough cart items to get the weight for each item
    foreach(WC()->cart->get_cart() as $cart_item){
        if( $cart_item['variation_id'] > 0)
            $item_id = $cart_item['variation_id'];
        else
            $item_id = $cart_item['product_id'];

        // Getting the product weight
        $product_weight = get_post_meta( $item_id , '_weight', true);

        if( !empty($product_weight) && $product_weight >= $target_cart_amount ){
            $light_products_only = false;
            break;
        }
        else $light_products_only = true;
    }

    // If 'free_shipping' method is available and if products are not heavy
    // and cart amout up to the target limit, we hide other methods
    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id && $passed && $light_products_only ) {
            $free[ $rate_id ] = $rate;
            break;
        }
    }

    return ! empty( $free ) ? $free : $rates;
}

但現在,我想設置一個可變的運費,它將以兩種方式計算:

But now, I would like to set a variable shipping amount that will be calculated in 2 ways:

  • 當訂單金額低于 250 件時,將按訂單商品總重量計算 1 歐元/公斤.
  • 當訂單金額達到 250 件時,將僅計算重件重量(1 歐元/公斤).如果沒有重物,可享受免費送貨服務.

我怎樣才能做到這一點,因為它有點復雜?
有什么要跟蹤的嗎?

How can I achieve this, as it’s a bit complicated?
Any track to follow?

我已經嘗試了一些現有的相關插件,但它們不適合這種情況.

I have tried some existing related plugins, but they aren't convenient for this case.

謝謝.

推薦答案

代碼改進 (2019 年 3 月):

是的,如果沒有帶有自定義運費的插件,這是可能的,根據購物車物品重量和購物車數量計算......但您需要有一個'Flat rate' 運輸方式設置最低數量.這通常應該適用于您正在使用的代碼.

Yes this is possible without a plugin with a custom SHIPPING FEE, calculated on cart items weight and cart amount… But you will need to have a 'Flat rate' shipping method set with a minimal amount. This normally should work with the code you are using.

這是代碼(評論):

//Adding a custom Shipping Fee to cart based conditionaly on weight and cart amount
add_action('woocommerce_cart_calculate_fees', 'custom_conditional_shipping_fee', 10, 1);
function custom_conditional_shipping_fee( $cart ){

    ## --- YOUR SETTINGS --- ##
    $targeted_weight      = 20;  // Your targeted "heavy" product weight
    $targeted_cart_amount = 250; // Your targeted cart amount
    $price_per_kg         = 1;   // Price by Kg;
    $flat_rate_price      = 10;  // Set the cost like in 'flat rate' shipping method

    // Initializing variables
    $fee = $calculated_weight = 0;

    // For cart SUBTOTAL amount EXCLUDING TAXES
    $passed = $cart->subtotal_ex_tax >= $targeted_cart_amount ? true : false ;

    // For cart SUBTOTAL amount INCLUDING TAXES (replace by this):
    // $passed = $cart->subtotal >= $targeted_cart_amount ? true : false ;

    // Iterating through each cart items
    foreach( $cart->get_cart() as $cart_item ){
        // The Product weight
        $product_weight = $cart_item['data']->get_weight();

        // cart item weight
        $cart_item_weight = $cart_item['quantity'] * $cart_item['data']->get_weight();

        // When cart amount is up to 250, Adding weight of heavy items
        if($passed && $product_weight > $targeted_weight)
            $calculated_weight += $cart_item_weight;
    }

    #### Making the fee calculation ####

    // Cart is up to 250 with heavy items
    if ( $passed && $calculated_weight != 0 ) {
        // Fee is based on cumulated weight of heavy items
        $fee = ( $calculated_weight * $price_per_kg ) - $flat_rate_price;
    }
    // Cart is below 250
    elseif ( ! $passed ) {
        // Fee is based on cart total weight
        $fee = ( $cart->get_cart_contents_weight( ) * $price_per_kg ) - $flat_rate_price;
    }

    #### APPLYING THE CALCULATED FEE ####

    // When cart is below 250 or when there is heavy items
    if ($fee > 0){
        // Rounding the fee
        $fee = round( $fee );
        // This shipping fee is taxable (You can have it not taxable changing last argument to false)
        $cart->add_fee( __('Shipping weight fee', 'woocommerce'), $fee, true);
    }
}

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

此代碼已經過測試并且可以運行

This code is tested and it works

從費用計算中減去最小的統一費率"金額,這樣客戶支付正確的價格.

The minimal 'Flat rate' amount is subtracted from the fee calculation, this way the customer pay the right price.

這篇關于根據購物車物品重量和購物車數量計算運費的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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网址 | 久久香蕉网 | 久久免费精彩视频 | 中文字幕观看 | 亚洲第一福利视频 | h在线免费观看 | 欧美精品在线一区二区三区 | 欧美日韩国产传媒 | 欧美美乳 | 亚洲一区二区精品视频在线观看 | 国产一区二区三区视频 | 午夜视频免费 | 国产欧美一区二区三区日本久久久 | 色婷婷一区二区三区四区 | 欧美在线视频二区 | 欧美 视频 | 精品麻豆剧传媒av国产九九九 | 成人在线视频免费观看 | 成人在线视频网址 | 亚洲国产小视频 | 久久免费精品 | 亚洲国产中文字幕 | 午夜免费在线电影 | 国产日韩欧美一区二区在线播放 | 蜜桃传媒一区二区 | 国产精品成人一区二区 | 精品国产乱码久久久久久图片 | 人人干在线 | 精品久久电影 | 欧美日韩精品一区二区三区蜜桃 | 免费在线看a | 精品国产91乱码一区二区三区 | 亚洲h色| 一区二区三区视频在线 | 国产精品高清在线 | 麻豆视频在线免费观看 | 欧美一区视频在线 | 日韩视频免费在线 | 99热在线免费 | 精品一区二区视频 | 黄色成人免费看 |