Woocommerce Change shipping method title on checkout based on shipping class selected

懵懂的女人 提交于 2021-02-19 06:52:04

问题


I want to change the shipping method title displayed in the checkout of my store based on the shipping class the product has.

e.g.

Shipping method title is currently Flat Rate and I have 2 products:

  • If product A is being purchased I need it to have "Fragile shipping"
  • If product B is being purchased I need it to have "Standard shipping"

Sadly I have to do my shipping using classes so alternative methods won't work.

Any help would be appreciated.


回答1:


The following code will rename your shipping flat rate based on your "Fragile" shipping class:

You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.

The code:

add_filter('woocommerce_package_rates', 'change_shipping_method_name_based_on_shipping_class', 50, 2);
function change_shipping_method_name_based_on_shipping_class($rates, $package){
    // HERE set the shipping class for "Fragile"
    $shipping_class_id = 64;
    $found = false;

    // Check for the "Fragile" shipping class in cart items
    foreach( $package['contents'] as $cart_item ) {
        if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
            $found = true;
            break;
        }
    }

    // Loop through shipping methods
    foreach ( $rates as $rate_key => $rate ) {
        // Change "Flat rate" Shipping method label name
        if ( 'flat_rate' === $rate->method_id ) {
            if( $found )
                $rates[$rate_key]->label = __( 'Fragile shipping', 'woocommerce' );
            else
                $rates[$rate_key]->label = __( 'Standard shipping', 'woocommerce' );
        }
    }
    return $rates;
}

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

Don't forget to re-enable "Enable debug mode" option in shipping settings.




回答2:


i think this plugin may help you and also check this one



来源:https://stackoverflow.com/questions/51026568/woocommerce-change-shipping-method-title-on-checkout-based-on-shipping-class-sel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!