根据重量和最小购物车数量免费送货

Free shipping depending on weight and on minimal cart amount(根据重量和最小购物车数量免费送货)
本文介绍了根据重量和最小购物车数量免费送货的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

    // Iterating trough cart items to get the weight for each item
    foreach( $package['contents'] as $cart_item ){
        // Getting the product weight
        $product_weight = $cart_item['data']->get_weight();

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

    // 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运输设置中禁用、保存和启用、保存当前运输区域的相关运输方式.

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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)