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.
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.
i think this plugin may help you and also check this one
Related
I'm setting up a website with shipping but i have items that are collection on for all shipping zone and item that can be sent in all shipping zone.
So I have set up shipping classes for all the zones.
I am using "Hide shipping method for specific shipping classes in woocommerce" answer code and it is what I need.
But instead of putting in each flat_rate id is there a way I can target all the Flat Rate shipping methods, so when I add an other flat rate shipping setting, it will work for it, without having me making changes into the code.
I hope you understand what I am after. Any help is appreciated.
To use it for all "flat rate" Shipping methods and some other defined shipping methods, you will use the following instead:
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE define your shipping class to find
$class = 92;
// HERE define the shipping methods you want to hide (others than "flat rate")
$method_key_ids = array('local_pickup:3');
$found = false;
// Checking in cart items
foreach( $package['contents'] as $cart_item ){
// If we find the shipping class
if( $cart_item['data']->get_shipping_class_id() == $class ){
$found = true;
break; // Stop the loop
}
}
if( ! $found )
return $rates;
// Loop through shipping methods
foreach( $rates as $rate_key => $rate ) {
// Targetting "Flat rate" and other defined shipping mehods
if( 'flat_rate' === $rate->method_id || in_array($rate->id, $method_key_ids) ) {
unset($rates[$rate_key]);
}
}
return $rates;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Refresh the shipping caches: (required)
This code is already saved on your active theme's function.php file.
The cart is empty
In a shipping zone settings, disable / save any shipping method, then enable back / save.
I am using Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3 lightly changed answer code to hide all shipping methods except one. The only method I want showing is a rate from the "Woocommerce Advanced Shipping" plugin.
I am using the correct rate ID etc...
Everything works fine except when a customer tries to click that shipping method, it won't stay selected. It just jumps back to free shipping.
I have tried debugging and also tried the code with a native woocommerce flat rate ID and it showed up/able to select it just fine.
add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 100, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {
$flat_rates_express = array( '2588' );
$free = $flat2 = array();
foreach ( $rates as $rate_key => $rate ) {
// Updated Here To
if ( in_array( $rate->id, $flat_rates_express ) )
$flat2[ $rate_key ] = $rate;
if ( 'free_shipping:12' === $rate->id )
$free[ $rate_key ] = $rate;
}
return ! empty( $free ) ? array_merge( $free, $flat2 ) : $rates;
}
ID I want to Keep Shown: "2588" (Custom Shipping Rate From Plugin)
How can I disable the Flat rate shipping method when free shipping is available o and keep a custom shipping rate (from a plugin)?
As you have 3 shipping methods, 1 free shipping, 1 flat rate and 1 custom '2588', it's possible to hide the flat rate shipping method when free shipping is available instead:
add_filter( 'woocommerce_package_rates', 'free_shipping_disable_flat_rate', 1000, 2 );
function free_shipping_disable_flat_rate( $rates, $package ) {
// Here your free shipping rate Id
$free_shipping_rate_id = 'free_shipping:12';
// When your Free shipping method is available
if ( array_key_exists( $free_shipping_rate_id, $rates ) ) {
// Loop through shipping methods rates
foreach ( $rates as $rate_key => $rate ) {
// Removing "Flat rate" shipping method
if ( 'flat_rate' === $rate->method_id ){
unset($rates[$rate_key]);
}
}
}
return $rates;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Refresh the shipping caches:
This code is already saved on your function.php file.
In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.
I have two shipping methods. First is Free Shipping and second is Flat Rate Shipping for Express shipping for which i charge extra fee. By default Express Shipping is selected in the cart which lead to confusion among some buyers that I do not offer free shipping.
Is it possible to change default selected method to free shipping?
I think that you just need to reorder your shipping methods for each shipping zone, moving "free shipping" on first line.
If it doesn't work, you can add the following code:
add_action( 'woocommerce_before_cart', 'auto_select_free_shipping_by_default' );
function auto_select_free_shipping_by_default() {
if ( isset(WC()->session) && ! WC()->session->has_session() )
WC()->session->set_customer_session_cookie( true );
// Check if "free shipping" is already set
if ( strpos( WC()->session->get('chosen_shipping_methods')[0], 'free_shipping' ) !== false )
return;
// Loop through shipping methods
foreach( WC()->session->get('shipping_for_package_0')['rates'] as $key => $rate ){
if( $rate->method_id === 'free_shipping' ){
// Set "Free shipping" method
WC()->session->set( 'chosen_shipping_methods', array($rate->id) );
return;
}
}
}
Code goes in function.php file of your active child theme (or active theme). tested and works.
If you don't use a Cart page and there is a redirection to checkout, you will have to replace woocommerce_before_cart by woocommerce_before_checkout_form hook in the code.
function test_default_shipping_method($default,$available){
$default_method = 'wcv_pro_vendor_shipping'; //provide here the service name which will selected default
if( array_key_exists($method, $available_methods ) )
return $default_method;
else
return $default_method;
}
I have 3 shipping methods in my cart that should become zero prices as soon as your customer enters Free Shipping coupon.
I know how to add a filter in functions.php to detect the coupon but is someone know a snippet to set shipping methods visibles in cart (radio button) to ZERO for this order?
My deliveries methods are companies like UPS, FedEx...
I activated the free shipping option in order it can be managed with coupon.
The list of choice of deliveries methods for the customers is calculated according to my products shipping class and order total weight.
Shipping class are not calculated but set by product and i use TABLE RATE PLUGIN to calculate the weight.
First free shipping method has to be enabled with option "A valid free shipping coupon"…
Then you need to set the desired coupon codes with option "Allow free shipping" enabled.
The following code will set all shipping methods costs to zero when a valid coupon code (with option "Allow free shipping" enabled) will be applied.
Update: Hide "Free shipping" method and append shipping label titles with "(free)"
add_filter( 'woocommerce_package_rates', 'coupon_free_shipping_customization', 20, 2 );
function coupon_free_shipping_customization( $rates, $package ) {
$has_free_shipping = false;
$applied_coupons = WC()->cart->get_applied_coupons();
foreach( $applied_coupons as $coupon_code ){
$coupon = new WC_Coupon($coupon_code);
if($coupon->get_free_shipping()){
$has_free_shipping = true;
break;
}
}
foreach( $rates as $rate_key => $rate ){
if( $has_free_shipping ){
// For "free shipping" method (enabled), remove it
if( $rate->method_id == 'free_shipping'){
unset($rates[$rate_key]);
}
// For other shipping methods
else {
// Append rate label titles (free)
$rates[$rate_key]->label .= ' ' . __('(free)', 'woocommerce');
// Set rate cost
$rates[$rate_key]->cost = 0;
// Set taxes rate cost (if enabled)
$taxes = array();
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 )
$taxes[$key] = 0;
}
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works. It should works also for you.
Sometimes, you should may be need to refresh shipping methods:
1) Empty cart first.
2) Go to shipping Zones settings, then disable/save and re-enable/save the related shipping methods.
If you want to achieve the same but whenever there is a free shipping method available (not only applied coupon, but also cart total is above certain price), you can use this:
add_filter( 'woocommerce_package_rates', 'wc_apply_free_shipping_to_all_methods', 10, 2 );
function wc_apply_free_shipping_to_all_methods( $rates, $package ) {
if( isset( $rates['free_shipping:11'] ) ) {
unset( $rates['free_shipping:11'] );
foreach( $rates as $rate_key => $rate ) {
// Append rate label titles (free)
$rates[$rate_key]->label .= ' ' . __('(free)', 'woocommerce');
// Set rate cost
$rates[$rate_key]->cost = 0;
// Set taxes rate cost (if enabled)
$taxes = array();
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 )
$taxes[$key] = 0;
}
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
Notice, that after free_shipping there is a :11. Prior to WooCommerce 2.6, the details of shipping method "Free Shipping" were stored under array element $rates['free_shipping']. Now, however, it is stored as $rates['free_shipping:shipping_zone_instance_id'] where shipping_zone_instance_id is the shipping zone instance id of the shipping method. You can check the instance id of the shipping method by inspecting the "Free Shipping" in admin panel, or opening it in new tab and looking at the url http://prntscr.com/jd2zjy.
I have a woocommerce website and I have set 2 shipping methods:
Flat Rate
Local pickup
I would like to set the "Flat rate" shipping method as default (selected) in the cart or checkout page.
1) You can use the following code (to set "flat rate" shipping method as default) In cart page:
add_action( 'woocommerce_before_cart', 'set_default_chosen_shipping_method', 5 );
function set_default_chosen_shipping_method(){
//
if( count( WC()->session->get('shipping_for_package_0')['rates'] ) > 0 ){
foreach( WC()->session->get('shipping_for_package_0')['rates'] as $rate_id =>$rate)
if($rate->method_id == 'flat_rate'){
$default_rate_id = array( $rate_id );
break;
}
WC()->session->set('chosen_shipping_methods', $default_rate_id );
}
}
Code goes in function.php file of your active child theme (active theme or in any plugin file).
Tested and Works in WooCommerce 3+
2) You can also reorder the shipping rates in your shipping zones settings (but it doesn't really works as the last chosen shipping method take the hand).
You could use the following code to set 'any' shipping method as default.
function reset_default_shipping_method( $method, $available_methods ) {
$default_method = 'wf_fedex_woocommerce_shipping:FEDEX_GROUND'; //provide the service name here
if( array_key_exists($method, $available_methods ) )
return $default_method;
else
return $method;
}
Let's say, you're using a Carrier shipping plugin like WooCommerce FedEx Shipping Plugin. You can fetch the value Id (shown below) and paste it under the '$default_method' in the above code.
You will have to copy and paste the code in WordPress Dashboard->Appearance–>Editor–>functions.php of your theme.
Hope that helped. :)
copy the value Id from here