pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

根據(jù)購物車物品重量和購物車數(shù)量計算運費

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

問題描述

限時送ChatGPT賬號..

在客戶端 WooCommerce 網(wǎng)站中,為最多 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;
}

但現(xiàn)在,我想設(shè)置一個可變的運費,它將以兩種方式計算:

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

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

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

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

我已經(jīng)嘗試了一些現(xiàn)有的相關(guān)插件,但它們不適合這種情況.

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

謝謝.

推薦答案

代碼改進 (2019 年 3 月):

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

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 文件或任何插件文件中.

此代碼已經(jīng)過測試并且可以運行

This code is tested and it works

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

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

這篇關(guān)于根據(jù)購物車物品重量和購物車數(shù)量計算運費的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產(chǎn)品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 真空包装机-诸城市坤泰食品机械有限公司| 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 针焰试验仪,灼热丝试验仪,漏电起痕试验仪,水平垂直燃烧试验仪 - 苏州亚诺天下仪器有限公司 | 昆山PCB加工_SMT贴片_PCB抄板_线路板焊接加工-昆山腾宸电子科技有限公司 | 云南标线|昆明划线|道路标线|交通标线-就选云南云路施工公司-云南云路科技有限公司 | 防爆电机-高压防爆电机-ybx4电动机厂家-河南省南洋防爆电机有限公司 | 苏商学院官网 - 江苏地区唯一一家企业家自办的前瞻型、实操型商学院 | 风电变桨伺服驱动器-风电偏航变桨系统-深圳众城卓越科技有限公司 | 佛山商标注册_商标注册代理|专利注册申请_商标注册公司_鸿邦知识产权 | 东莞市天进机械有限公司-钉箱机-粘箱机-糊箱机-打钉机认准东莞天进机械-厂家直供更放心! | 恒温槽_恒温水槽_恒温水浴槽-上海方瑞仪器有限公司 | 地源热泵一体机,地源热泵厂家-淄博汇能环保设备有限公司 | 泰兴市热钻机械有限公司-热熔钻孔机-数控热熔钻-热熔钻孔攻牙一体机 | China plate rolling machine manufacturer,cone rolling machine-Saint Fighter | 世纪豪门官网 世纪豪门集成吊顶加盟电话 世纪豪门售后电话 | 工控机,嵌入式主板,工业主板,arm主板,图像采集卡,poe网卡,朗锐智科 | 合肥角钢_合肥槽钢_安徽镀锌管厂家-昆瑟商贸有限公司 | 常州企业采购平台_常州MRO采购公司_常州米孚机电设备有限公司 | (中山|佛山|江门)环氧地坪漆,停车场地板漆,车库地板漆,聚氨酯地板漆-中山永旺地坪漆厂家 | 猎头招聘_深圳猎头公司_知名猎头公司 | 传动滚筒_厂家-淄博海恒机械制造厂 | 山东臭氧发生器,臭氧发生器厂家-山东瑞华环保设备 | 活性炭厂家-蜂窝活性炭-粉状/柱状/果壳/椰壳活性炭-大千净化-活性炭 | 连续密炼机_双转子连续密炼机_连续式密炼机-南京永睿机械制造有限公司 | 四川职高信息网-初高中、大专、职业技术学校招生信息网 | 搪瓷搅拌器,搪玻璃搅拌器,搪玻璃冷凝器_厂家-淄博越宏化工设备 | 电地暖-电采暖-发热膜-石墨烯电热膜品牌加盟-暖季地暖厂家 | 液压压力机,液压折弯机,液压剪板机,模锻液压机-鲁南新力机床有限公司 | 环氧乙烷灭菌器_压力蒸汽灭菌器_低温等离子过氧化氢灭菌器 _低温蒸汽甲醛灭菌器_清洗工作站_医用干燥柜_灭菌耗材-环氧乙烷灭菌器_脉动真空压力蒸汽灭菌器_低温等离子灭菌设备_河南省三强医疗器械有限责任公司 | 熔体泵_熔体出料泵_高温熔体泵-郑州海科熔体泵有限公司 | 冷水机,风冷冷水机,水冷冷水机,螺杆冷水机专业制造商-上海祝松机械有限公司 | 紧急泄压人孔_防爆阻火器_阻火呼吸阀[河北宏泽石化] | 闭端端子|弹簧螺式接线头|防水接线头|插线式接线头|端子台|电源线扣+护线套|印刷电路板型端子台|金笔电子代理商-上海拓胜电气有限公司 | 棉服定制/厂家/公司_棉袄订做/价格/费用-北京圣达信棉服 | WF2户外三防照明配电箱-BXD8050防爆防腐配电箱-浙江沃川防爆电气有限公司 | 钢绞线万能材料试验机-全自动恒应力两用机-混凝土恒应力压力试验机-北京科达京威科技发展有限公司 | 水压力传感器_数字压力传感器|佛山一众传感仪器有限公司|首页 | 转向助力泵/水泵/发电机皮带轮生产厂家-锦州华一精工有限公司 | 沈阳网站建设_沈阳网站制作_沈阳网页设计-做网站就找示剑新零售 沈阳缠绕膜价格_沈阳拉伸膜厂家_沈阳缠绕膜厂家直销 | 密封圈_泛塞封_格莱圈-[东莞市国昊密封圈科技有限公司]专注密封圈定制生产厂家 | 巩义市科瑞仪器有限公司|