I am trying to create a functionality when a user tries to select COD as a payment option then set shipping to Free Shipping (Make this shipping option available).
Right now I have added this code.
add_action( 'woocommerce_checkout_update_order_review', __NAMESPACE__.'\\refresh_shipping_methods', 10, 1 );
function refresh_shipping_methods($post_data)
{
if (isset($post_data['payment_method']) && $post_data['payment_method'] === 'cod') {
// I am not sure how to add Free Shipping Method here.
}
}
I am unable to figure out how can I add free shipping method here when it isn't available in the available_shipping_method.
How can I add it programmatically ?
Related
I have woocommerce_shipping_package_name hook that makes API call after the customer fill his billing data to get the address and caculate shipping
`add_filter('woocommerce_shipping_package_name', 'customer_address_city_with_shipping', 10, 1);`
// this inside the customer_address_city_with_shipping function
$woocommerce->cart->add_fee('Shipping', $fee, true, '');
the shipping method added but without the fee (for sure I tried custom value)
In WooCommerce, I am hiding shipping methods based on different shipping classes in cart using "Hide shipping methods for specific shipping class in WooCommerce" answer code (the 2nd way), but the problem is that I use WPML plugin which manage 2 languages site, so looking for just one class won't do it.
So I need to handle 2 shipping classes instead of one. I tried addind 2 shipping classes this way:
// HERE define your shipping classes to find
$class = 3031, 3032;
But it breaks the website. So I would like to hide the defined flat rate not only for both shipping classes 3031 and 3032.
What I am doing wrong? How can I enable 2 shipping classes without breaking the web site?
To use multiple shipping classes, you should first defined them in an array and in the IF statement you will use in_array() conditional function this way:
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 classes to find
$classes = [3031, 3032];
// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:189');
// Checking in cart items
foreach( $package['contents'] as $item ) {
// If we find one of the shipping classes
if( in_array( $item['data']->get_shipping_class_id(), $classes ) ){
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]); // Remove the targeted methods
}
break; // Stop the loop
}
}
return $rates;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Sometimes, you should may be need to refresh shipping methods going to shipping areas, then disable / save and re-enable / save your "flat rates" shipping methods.
Related thread: Hide shipping methods for specific shipping class in WooCommerce
I have my own shipping method with some logic.
//some logic
$method->setPrice($price);
When in admin panel in promo rules i set free shipping for some condition, all other shipping method set to 0, but not may.
Help me please, what am i missing?
use
if ($request->getFreeShipping() === TRUE) {}
I have 2 shipping methods defined:
Table Rates - Priority 1
Local Pickup - Priority 2
Within Table Rates, I have 3 options:
Registered Australian Post (2 to 8 Business Days): $6.50
Tracking and Freight Insurance: $7.25
Nation-Wide Delivery (5 to 12 Business Days): $1.40
All orders that fall within the Local Table Rate area are presented with these 3 options and by default, Option 3 is selected ( I assume because it is the cheapest)
It defaults the priority to table rates but you can't define priority within the actual table rates. I want the default option to be Option 1: Registered Australian Post (2 to 8 Business Days)
I have discovered that the default shipping method is set here:
WC()->session->[chosen_shipping_methods] => a:1:{i:0;s:17:"table_rate-5 : 70";}
and apparently can be accessed and modified using the following two methods:
WC()->session->get('chosen_shipping_methods');
WC()->session->set('chosen_shipping_methods', $chosen_method);
BUT, I can get the current chosen_shipping_methods I just can't set a new one.
I'm trying to set it using the action woocommerce_shipping_method_chosen but it's not working
Can anyone guide me to what I should be looking at?
by only looking on your website and not looking at the code, I'm guessing this might be what you want...
add_action( 'template_redirect', 'reigel_chosen_shipping_methods' );
function reigel_chosen_shipping_methods(){
remove_action(current_filter(), __FUNCTION__);
WC()->session->set( 'chosen_shipping_methods', array('table_rate-7 : 72') );
}
You can reorder shipping methods via the admin dashboard. Goto Woocommerce > Shipping > Shipping Methods and change the order using drag and drop.
This is what I ended up using which worked as required:
/*=Use the shipping method filter to set the "selected" shipping method to
* the first (default) method in the list
**************************************************************************/
function oley_reset_default_shipping_method( $method, $available_methods ) {
$method = key($available_methods);
return $method;
}
add_filter('woocommerce_shipping_chosen_method', 'oley_reset_default_shipping_method', 10, 2);
(NOTE: This worked because the shipping rate I wanted was actually the first in the list but just wasn't being selected by default)
I have setup the free shipping for specific product categories in magento by refering this blog post http://www.regularjen.com/archives/2010/06/11/free-shipping-on-a-single-item-in-magento/ . And its working fine. Now the issue is , Magento showing both free shipping & other shipping charges in shiiping reuslt.
How can i remove the those result ?
One solution might be to override the getShippingRates() method which you can find in the Mage_Checkout_Block_Onepage_Shipping_Method_Available class.
public function getShippingRates()
{
parent::getShippingRates();
if (isset($this->_rates['freeshipping'])) {
$this->_rates = array('freeshipping' => $this->_rates['freeshipping']);
}
return $this->_rates;
}