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.
Related
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.
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;
}
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.
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;
?>
Does someone know how to get the tax rate in Opencart 1.5.6.
It should be $string = $this->tax->getRate($tax_class_id); but that was depreciated. When I get the taxrate from config $this->config->get('config_tax'); I only get the tax ID
Is there a way to get the current tax rate build in opencart or has that to be done by using $this->config->get('config_tax') and a new function?
Hope some one can give me a clue
$this->config->get('config_tax') is used to determine if you want to apply tax rates to your items at all (simply an on-off switch if you like).
1.5.6 is no different to most of the 1.5.X series in that you get tax. Tax rates can also be multiple not just one, so getRates() will return an array of values. You call it using $this->tax->getRates($price_value, $tax_class_id) and it will return all of the tax associated with that. However, you really shouldn't be calling that directly, you should be calling the $this->tax->calculate($price, $tax_class_id, $this->config->get('config_tax')) to correctly determine tax values on a price.
For reference, you should check out the file /system/library/tax.php if you haven't already for the full tax methods.