且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

覆盖Woocommerce中特定运输类别的所有运输成本

更新时间:2023-11-30 11:50:22

当在购物车中找到特定的定义运输方式时,以下代码会将所有运输成本设置为零,并显示自定义通知。

The following code will set all shipping cost to zero, when a specific defined shipping method is found in cart items and will display a custom notice.

第二个挂钩函数是可选的,它将在结帐页面上显示自定义通知。

The second hooked function is optional and will display the custom notice in checkout page.


您将不得不暂时 启用调试模式在发运选项(tab)下的发运设置中以禁用/清除缓存。

You will have to temporarily "Enable debug mode" in Shipping settings under Shipping options (tab) to disable / clear caching.

在下面的代码中,请在每个函数中定义您的装运类别的子弹和您的自定义通知:

In the code bellow define in each function your shipping class slug and your custom notice:

// Null shipping costs for a specific shipping class and display a message
add_filter('woocommerce_package_rates', 'shipping_class_null_shipping_costs', 10, 2);
function shipping_class_null_shipping_costs( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    // HERE set your shipping class slug
    $shipping_class_slug = 'extra';

    $found = false;

    // Loop through cart items and checking for the specific defined shipping class
    foreach( $package['contents'] as $cart_item ) {
        if( $cart_item['data']->get_shipping_class() == $shipping_class_slug )
            $found = true;
    }

    // Set shipping costs to zero if shipping class is found
    if( $found ){
        foreach ( $rates as $rate_key => $rate ){
            $has_taxes = false;
            // Targetting "flat rate" and local pickup
            if( 'flat_rate' === $rate->method_id || 'local_pickup' === $rate->method_id  ){
                // Set the cost to zero
                $rates[$rate_key]->cost = 0;

                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $rates[$rate_key]->taxes[$key] > 0 ){
                        $has_taxes = true;
                        // Set taxes cost to zero
                        $taxes[$key] = 0;
                    }
                }
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
        }
        // Clear duplicated notices
        wc_clear_notices();
        // Add a custom notice to be displayed
        wc_add_notice( __('My custom shipping message here.', 'woocommerce'), 'notice' );
    }
    return $rates;
}


add_action( 'woocommerce_before_checkout_form', 'display_custom_shipping_message_in_checkout' );
function display_custom_shipping_message_in_checkout(){
    // HERE set your shipping class slug
    $shipping_class_slug = 'extra';

    $found = false;

    // Loop through cart items and checking for the specific defined shipping class
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if( $cart_item['data']->get_shipping_class() == $shipping_class_slug )
            $found = true;
    }

    if( $found ){
        // Display a custom notice
        wc_print_notice( __('My custom shipping message here.', 'woocommerce'), 'notice' );
    }
}

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

Code goes in function.php file of your active child theme (or active theme). It should works.


一旦测试一次,别忘了在运输设置中禁用调试模式