php 免费送货取决于重量和最低购物车金额

eiee3dmh  于 2023-01-12  发布在  PHP
关注(0)|答案(2)|浏览(89)

与WooCommerce,我需要有免费送货超过一定数量的250除了在购物车中包括的重型产品。
有人知道我该怎么做吗?

r9f1avp5

r9f1avp51#

此自定义代码将保持免费送货方法,并将隐藏其他送货方式时,购物车金额高达250,如果产品不重(低于20公斤在这里)...不允许免费送货订单少于250,你可以在woocommerce (见末尾)。
首先,您必须确保在每个重产品中设置了重量(对于简单产品或可变产品(在每个变体中))。此处的购物车小计为不含税*(您可以轻松地将其更改为含税)*。

下面是**woocommerce_package_rates**filter钩子中的自定义钩子函数:

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文件中,或者也可以在任何插件文件中。
此代码经过测试,它适用于简单和可变的产品...
您还必须在WooCommerce设置〉运输,对于每个运输区域和**"Free Shipping"**方法,您的最低订单金额:

    • 您将需要刷新航运缓存数据:**禁用,保存和启用,保存当前航运区的相关航运方式,在woocommerce航运设置。
06odsfpq

06odsfpq2#

对于超过一定金额的免费送货,您可以使用内置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
        }

相关问题