免运费取决于重量和最小的购物车数量

时间:2017-02-17 13:34:45

标签: php wordpress woocommerce checkout shipping

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

有谁知道我应该做什么?

感谢。

2 个答案:

答案 0 :(得分:3)

此自定义代码将保留免费送货方法,并且当购物车金额高达 250 且产品不重时隐藏其他送货方式(此处少于20公斤)...如果订单少于250,则不允许免费送货,您可以在woocommerce 中设置此项(见最后)。

首先,您必须确保在每个重型产品中设置重量(对于简单或变量产品(在每个变体中)。此处的购物车小计不含税 (和您一样)可以将其改为容易纳税)

enter image description here

然后是 woocommerce_package_rates 过滤器钩子中的自定义钩子函数:

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文件中,或者放在任何插件文件中。

此代码经过测试,适用于简单和可变的产品......

  

你还需要在WooCommerce设置&gt;运送,为每个送货区域和 "Free Shipping" 方法您的最低订单金额:

enter image description here

  

您需要刷新送货缓存数据:禁用,保存并启用,保存当前送货区域的相关送货方式,在woocommerce送货设置中。

答案 1 :(得分:0)

对于超过一定金额的免费送货,您可以使用内置的WooCommerce选项。

对于某些特定产品,如果要免运费,可以使用以下代码段。

 add_filter('woocommerce_package_rates', 'hide_shipping_method_if_particular_product_available_in_cart', 10, 2);

        function hide_shipping_method_if_particular_product_available_in_cart($available_shipping_methods)
        {
            global $woocommerce;

            // products_array should be filled with all the products ids
            // for which shipping method (stamps) to be restricted.

            $products_array = array(
                101,
                102,
                103,
                104
            );

            // You can find the shipping service codes by doing inspect element using
            // developer tools of chrome. Code for each shipping service can be obtained by
            // checking 'value' of shipping option.

            $shipping_services_to_hide = array(
                'free_shipping',

            );

            // Get all products from the cart.

            $products = $woocommerce->cart->get_cart();

            // Crawl through each items in the cart.

            foreach($products as $key => $item) {

                // If any product id from the array is present in the cart,
                // unset all shipping method services part of shipping_services_to_hide array.

                if (in_array($item['product_id'], $products_array)) {
                    foreach($shipping_services_to_hide as & $value) {
                        unset($available_shipping_methods[$value]);
                    }

                    break;
                }
            }

            // return updated available_shipping_methods;
    return
        }