Set "flat rate" shipping method as default in woocommerce - php

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

Related

Hide all Flat rate Shipping methods for specific shipping classes in WooCommerce

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.

Change the default Shipping method in Woocommerce

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;
}

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

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

Update WooCommerce "flat rate" shipping cost using jQuery

I am trying to update the shipping cost after the address is entered. When the client starts typing the address, and autocomplete function helps the client find the street name, building, floor, and side.
Once the address is entered JavaScript calculates the distance and the price.
Then the price is via AJAX send to shipment.php
It is my goal to have shipment update the shipping cost accordingly.
I have tried:
include '../../../../wp-load.php';
add_action( 'woocommerce_flat_rate_shipping_add_rate','add_another_custom_flat_rate', 10, 2 );
function add_another_custom_flat_rate( $method, $rate ) {
$new_rate = $rate;
$new_rate['cost'] = 50; // Update the cost to 50
$method->add_rate( $new_rate );
}
But without luck.
I have also following this guide and added another shipping method: Tuts
https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce--cms-26098
But once again, I cannot update the price once the address is entered.
I think that You don't need Query ajax to update the price directly for this calculated shipping cost and you don't need a custom shipping method as in your code or linked tutorial.
THERE IS THREE STEPS:
1) In your existing Ajax driven PHP function, you will set the calculated price in a custom WC_Session attribute where $shipping_cost variable will be the collected value of the calculated shipping cost passed via JS Ajax:
WC()->session->set( 'shipping_calculated_cost', $shipping_cost );
Then in jQuery Ajax "on success" event you will update checkout with the following:
$('body').trigger('update_checkout');
That will refresh checkout data…
2) In the custom function hooked in woocommerce_package_rates filer hook, you will get the session value of the calculated shipping cost…
IMPORTANT: In WooCommerce settings > shipping, you will set the "flat rate" cost to 1…
In The function code you will set the default price for this flat rate (that will be used when the shipping cost calculation doesn't exist yet):
add_filter('woocommerce_package_rates', 'update_shipping_costs_based_on_cart_session_custom_data', 10, 2);
function update_shipping_costs_based_on_cart_session_custom_data( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// SET HERE the default cost (when "calculated cost" is not yet defined)
$cost = '50';
// Get Shipping rate calculated cost (if it exists)
$calculated_cost = WC()->session->get( 'shipping_calculated_cost');
// Iterating though Shipping Methods
foreach ( $rates as $rate_key => $rate_values ) {
$method_id = $rate_values->method_id;
$rate_id = $rate_values->id;
// For "Flat rate" Shipping" Method only
if ( 'flat_rate' === $method_id ) {
if( ! empty( $calculated_cost ) ) {
$cost = $calculated_cost;
}
// Set the rate cost
$rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cost, 2);
// Taxes rate cost (if enabled)
foreach ($rates[$rate_id]->taxes as $key => $tax){
if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
$taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cost, 2 );
$has_taxes = true;
} else {
$has_taxes = false;
}
}
if( $has_taxes )
$rates[$rate_id]->taxes = $taxes;
}
}
return $rates;
}
Code goes in function.php file of your active child theme (or active theme) or in any plugin file.
3) Refresh the shipping caches (needed sometimes):
First empty your cart.
This code is already saved on your function.php file.
Go in a shipping zone settings and disable one "flat rate" (for example) and "save". Then re-enable that "flat rate" and "save". You are done and you can test it.
This should work for you,
If you have many different "flat rates" by shipping zones, you will need to make some changes in my code, if the default cost is different for each…

Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3

In WooCommerce 3, I have these shipping options (settings):
Free Shipping: free_shipping:1 - Minimum order amount is set at $50.
Normal Shipping flat_rate:3 - Amount $5.
Express Shipping flat_rate:5 - Amount $10.
I would like Express Shipping option to be always available (shown).
But when Free shipping is available (meaning that the customer has more than $50 in the cart) I would like to hide Normal Shipping only.
So when Free shipping is NOT available (and hidden), the available shipping rates will be Normal Shipping and Express Shipping.
Is that possible? How can I get this on WooCommerce 3?
Based on the official WooCommerce snippet code, making some light changes, you will be able to hide only your first flat rate when free shippings is available:
add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 100, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {
// HERE yours 2nd flat rate "Express Shipping" (that you never hide) in the array:
$flat_rates_express = array( 'flat_rate:5', 'flat_rate:12', 'flat_rate:14' );
$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' === $rate->method_id )
$free[ $rate_key ] = $rate;
}
return ! empty( $free ) ? array_merge( $free, $flat2 ) : $rates;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested on WooCommerce 3 and works.
Refresh the shipping caches:
1) First empty your cart.
2) This code is already saved on your function.php file.
3) Go in a shipping zone settings and disable one "flat rate" (for example) and "save". Then re-enable that "flat rate" and "save". You are done and you can test it.

Categories