根据重量和最少的购物车数量免费送货 [英] Free shipping depending on weight and on minimal cart amount

查看:90
本文介绍了根据重量和最少的购物车数量免费送货的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用WooCommerce,我需要在一定数量的 250 内免费送货,但购物车中包含的重型产品除外。

With WooCommerce, I need to have Free Shipping over certain amount of 250 except the heavy products that are included in the cart.

有人知道我应该怎么做吗?

Does anyone know what i should do?

谢谢。

推荐答案

此自定义代码将保留免费送货方法,并且当购物车金额达到 250 并且产品不很重时,将隐藏其他运输方法(小于20公斤)…要不允许小于250的订单免费送货,您可以在woocommerce (请参阅末尾)中进行设置。

This custom code will keep free shipping method and will hide other shipping methods when the cart amount is up to 250 and if products are not heavy (less than 20 kg here)… To not allow Free shipping for orders less than 250, you can set this in woocommerce (see at the end).

首先,您必须确保在每个重磅产品中设置了权重(对于简单或可变产品(在每个变体中)。此处的购物车小计是不含税 (可以轻松地将其更改为含税)。

First you will have to sure that the weight is set in each heavy product (for simple or variables products (in each variations). The cart subtotal here is Excluding taxes (and you can change it to Including taxes easily).

然后,此处是 woocommerce_package_rates 过滤器挂钩:

Then here is that custom hooked function in woocommerce_package_rates filter hook:

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 ) {

    // Set HERE your targeted weight (here is 20 kg) <== <== <== <== <==
    $target_product_weight = 20;

    // Set HERE your targeted cart amount (here is 250)  <== <== <== <== <==
    $target_cart_amount = 250;
    // For cart subtotal amount EXCLUDING TAXES
    WC()->cart->subtotal_ex_tax >= $target_cart_amount ? $passed = true : $passed = false ;
    // For cart subtotal amount INCLUDING TAXES (replace by this):
    // WC()->cart->subtotal >= $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;
}

代码进入活动子主题(或主题)的function.php文件

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

此代码已经过测试,适用于简单易变的产品……

This code is tested and it works for simple and variable products…


您还必须在WooCommerce设置>送货中,针对每个送货区域以及 免费送货 方法您的最低订单金额:

You Will also have to in WooCommerce settings > Shipping, for each shipping zone and for the "Free Shipping" method your minimum order amount:


您将需要刷新运送的缓存数据:在woocommerce运送设置中禁用,保存并启用,保存当前运送区域的相关运送方法。 / p>

You will need to refresh shipping cached data: disable, save and enable, save related shipping methods for the current shipping zone, in woocommerce shipping settings.

这篇关于根据重量和最少的购物车数量免费送货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆