Wordpress - How to make shipping price zero in woocommerce? - php

I have a condition where I want to make shipping price zero. Is there any predefined function for that? like we have a predefined function for making the cart empty,
$woocommerce->cart->empty_cart();
Similarly is there any function to make shipping price zero?

As per the official documentation:
If free shipping is enabled customers will have access to free shipping on their orders. You can however specify a few conditions they must meet to be granted free shipping such as;
Minimum order amount
Coupon (Users will need to enter a valid free shipping coupon code to use this method. If a coupon is used, the minimum order amount will be ignored)
You can also choose specific countries to which free shipping is allowed.
Further, the free shipping method has an is_available function which can be hooked into:
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available );
This means you can use add_filter() on woocommerce_shipping_free_shipping_is_available and return true or false.

Related

Filter "woocommerce_cart_contents_weight" not applied for shipping weight

I'm trying to take account of the package weight in the total weight of the cart so I can bill the right shipping cost.
I tried adding a filter on 'woocommerce_cart_contents_weight' like explained there
So I added the following code snippet
function add_package_weight_to_cart_contents_weight( $weight ) {
$weight = $weight * 1.3; // add 30%
return $weight;
}
add_filter('woocommerce_cart_contents_weight', 'add_package_weight_to_cart_contents_weight');
I've also added a snippet to print cart weight on cart page, and I can see that the weight filter isn't applied : The total weight of the cart doesn't change wether the snippet is active or not.
I also tried putting exactly 900g articles in the cart so that, with the box weight, it goes over 1kg and change price, but the shipping price is still the one for under 1kg.
Any idea on why it isn't applied and how I could fix it ?
Your code works and only affects WC_Cart method get_cart_contents_weight() usage.
Now for shipping methods, as cart items can be divided into packages, the weight is calculated for each shipping package and doesn't use WC_Cart method get_cart_contents_weight(). Instead it will make a calculation on the cart items for each package.
A possible way should be to use the filter hook woocommerce_package_rates to alter cost based on the package weight calculation. But this is going to be much more complicated as it seems that you are using 3rd party plugins.

WooCommerce remove specific fee

I'm currently adding a feee on checkout;
add_action( 'woocommerce_cart_calculate_fees', 'custom_cart_total' );
function custom_cart_total($order_total) {
WC()->cart->add_fee( __('Military Discount', 'woocommerce'), -$totalDiscount );
}
I have some additional conditional logic to add where I may want to remove this specific fee Military Discount. I don't see an option of remove_fee. I could 0 out the discount but then it will show on the invoice which I don't want if it is 0.
How can I remove this discount based on some of my stores logic?
Further details -
I could only add the discount based on logic instead of removing it however, this WooCommerce store is unique in the fact the shopping is done in-store. We have issues where customers are getting the discount due to cache/cookies/ or sessions. So I'm looking to force clear all the discounts for the next user.

Without coupon = Shipping free, with coupon = with shipping - WooCommerce

I need help for my WooCommerce Shop.
Our customers get free shipping after a amount of 39€.
If our customers get a price under 39€ after using a coupon, shipping is no more free.
So for example:
Customers is on Cart, full cart amount is 42€ = Shipping Free.
Use -15% off Coupon = cart amount is 38,55 = Shipping costs <- but this still have to be free
In the plugin i use, i can set the setting "allow shipping free" but then the customer gets shipping free after the minimum amount for the coupon so this isnt usefull for us.
I use the Plugin = WooCommerce Extended Coupon Features FREE
https://de.wordpress.org/plugins/woocommerce-auto-added-coupons/
Hope you guys understand what I mean
You can achieve this by using a simple code snippet.
you need to config one Free shipping without any Minimum order amount and a Flat rate to display normal shipping.
Now copy and paste below snippet into functions.php. It will hide free shipping if the subtotal less than 39, otherwise hide flat rate
add_filter( 'woocommerce_package_rates', 'modify_shipping_rate', 15, 2 );
function modify_shipping_rate( $available_shipping_methods, $package ){
global $woocmmerce;
$total_coast = WC()->cart->get_subtotal();
if( $total_coast < 39 ){
unset($available_shipping_methods['free_shipping:4']); // Config the Free Shipping method id properly
}else{
unset($available_shipping_methods['flat_rate:1']); //// Config the Flatrate shipping method id properly
}
return $available_shipping_methods;
}

Get the order total shipping in Woocommerce 3

I'm trying to get the shipping method or cost of shipping for a WooCommerce order - I'm writing a custom email template that is different depending on free delivery vs paid delivery.
I found a function called get_total_shipping(), but this is now deprecated and I cannot find a replacement - does one exist?
I've noticed that the shipping amount is stored in a hidden meta field (_order_shipping), which I can access, but I worry that this might break on future WooCommerce updates.
Since Woocommerce 3 get_total_shipping() method is replaced by get_shipping_total() .
So there is actually 2 available CRUD getters methods for shipping totals in WC_Abstract_Order Class that can be used on the WC_Order instance object:
get_shipping_total() that is the shipping total excluding taxes
get_shipping_tax() that is the shipping taxes total
So you will use them with the $order variable object simply this way:
$shipping_total = $order->get_shipping_total();
$shipping_tax = $order->get_shipping_tax();
There is also get_shipping_to_display() method that will output the formatted shipping total.

How to get all available shipping methods programmatically in magento?

I'm using magento C.E 1.7. I use all shipping methods of magento and I'm viewing only one shipping method in the front end, which has higher priority. I'm using table rate shipping for some countries.
I wanted to add special fixed shipping price for some products when they shipped to that countries only. So that, I added new shipping method namely 'flat rate per product', by using a new module.
Based on this module, a text box is added to all the products in admin to specify the special shipping price. If I filled this field with some value, It considers it as a special shipping price. And if not filled it, it will be considered as special shipping price is $0.
So I have given least priority in the sort order for this shipping method to avoid this method from viewing on front end when products without special shipping price and products with special shipping price both are added into the cart.
So that, If only the special shipping priced product is added to the cart, It shows only table rate shipping method in front end.
I checked the code in availble.phtml. There is a foreach loop to proceed with available shipping methods. I need to check the availability of the 'flat rate per product' and it's price value for the product before start of loop execution.
I tried to get all the values of the array. But I could not. Can anyone help on this?
you can use this code:
<?php
$methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
$shipMethods = array();
foreach ($methods as $shippigCode=>$shippingModel)
{
$shippingTitle = Mage::getStoreConfig('carriers/'.$shippigCode.'/title');
$shipMethods[$shippigCode] = $shippingTitle;
}
return $shipMethods;
?>

Categories