In Woocommerce I have the following situation: there are shipping methods before 100$, and after 100$ only one (free shipping) is available. So when a client buys a product 102$ and then apply the (10%) promo code, the price will be 91,80$. Because I unset the shipping methods after 100$ and the free shipping appears after 100$ only, for the client shows: "There are no shipping methods available..."
add_filter( 'woocommerce_package_rates', 'woocommerce_hide_shipping', 10, 2 );
function woocommerce_hide_shipping( $rates, $package ) {
$threshold = 100;
if ( WC()->cart->subtotal >= $threshold ) {
unset( $rates['flat_rate:45'] );
unset( $rates['flat_rate:75'] );
}
if ( WC()->cart->subtotal >= $threshold && !empty(WC()->cart->applied_coupons) ) {
//code here
}
return $rates;
}
Its possible to show the free shipping if the price was >100$ before promo code and a coupon was applied? I set up the free shipping to show a minimum order amount (100$) but there is a way to show, initialize? Other approaches are also welcomed.
The problem is.... Free Shipping that you have applied is bounded by min order and that is referring to order total (NOT SUBTOTAL).....
Whereas subtotal gives you the value without discount.
So,
Remove condition (min order total) from your Admin settings - Free shipping.... Now it will work for every order...
Then modify your code ---
add_filter( 'woocommerce_package_rates', 'woocommerce_hide_shipping', 10, 2 );
function woocommerce_hide_shipping( $rates, $package ) {
$threshold = 100;
if ( WC()->cart->subtotal >= $threshold ) {
unset( $rates['flat_rate:45'] );
unset( $rates['flat_rate:75'] );
}
if ( WC()->cart->subtotal < $threshold ) {
//code here
unset( $rates['free_shipping:45'] );
unset( $rates['free_shipping:75'] );
}
return $rates;
}
Related
I'm need hide only one woocommerce shipping method when subtotal cart is over R$ 130. i'm have been used this code below (found on web and modified by me), but what i need is not working, any effect was applied.
Here this code:
// Disable Woocommerce shipping methods based on cart subtot
add_filter( 'woocommerce_package_rates', 'wpsh_hide_shipping_based_on_subtotal', 10, 2 );
function wpsh_hide_shipping_based_on_subtotal( $rates, $package ) {
// Retrieve cart subtotal
$cart_subtotal = $package['contents_cost'];
// Shipping rate to be excluded
$shipping_id = 'correios-pac2';
if ( $cart_subtotal >= 130 )
unset( $rates[ $shipping_id ] );
return $rates;
}
Any help is apprecciate, tks.
I applied a 5% discount to the Woocommerce cart, based on cart amount and shipping method, which seems to be working fine. But I cannot manage to change the shipping method title when the rule applies, i need to add a message "5% discount" to all local-pickup methods.
I found a workarround using conditional shipping, but that won´t work in this website, I need to use the same method, but adding a message to the title.
Any ideas?
This is my code:
function local_pickup_discount( $cart ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$carrito_total = WC()->cart->get_cart_contents_total();
$chosen_shipping_no_ajax = $chosen_methods[0];
if ( 0 === strpos( $chosen_shipping_no_ajax, 'local_pickup' ) && $carrito_total < 60000) {
$discount = $cart->subtotal * 0.05; // 5% discount applied here
$cart->add_fee( __( 'Descuento por retiro en sucursal', 'yourtext-domain' ) , -$discount ); // Fee descripton
}
}
add_action( 'woocommerce_cart_calculate_fees', 'local_pickup_discount');
Thanks in advance, i know this must be a very simple thing to do, but i am not used to work with php.
You have to use this hook to rename your label woocommerce_cart_shipping_method_full_label. Inside you can apply any conditional logic you want and output label with custom one.
Note: This label is used in both cart and checkout. So if you want it only on checkout add condition is_checkout() or is_cart() if you want to limit it.
function wc_change_local_pickup_label( $label, $method ) {
$carrito_total = WC()->cart->get_cart_contents_total();
if ( $carrito_total < 60000 && 'local_pickup' === $method->method_id) {
$label = "Local pickup 5% discount";
}
return $label;
}
add_filter( 'woocommerce_cart_shipping_method_full_label', 'wc_change_local_pickup_label', 10, 2 );
Currently, our store offers "Free Shipping up to $350", after thought on how to incorporate this into eCom - I believe this will be the simplest method. Fetch Shipping Cost --> IF Shipping Costs > $350 --> Apply "350-shipping" coupon code.
We use 2 different plugins to calculate shipping:
1.) WooCommerce FedEx Shipping
2.) WooCommerce WWE LTL Quotes
Looking to incorporate this inside of functions.php. Note, I am also working on a solution, so will apply here if I get to it sooner
Notice here the correct code for the Shipping Page: https://i.imgur.com/TndLiHT.png
So far I have the following:
// $350 Shipping Coupon
add_filter( 'woocommerce_package_rates', 'custom_shipping_rate_label_based_on_cost', 100, 2 );
function custom_shipping_rate_label_based_on_cost( $rates, $package ){
// Loop through available shipping rates
foreach ( $rates as $rate_key => $rate ) {
$rate_cost = $rate->cost;
if ( $rate_cost > 350 ) {
echo "<script type='text/javascript'>alert('350 Detected');</script>";
}
}
return $rates;
}
What is missing here is I need to say:
IF ALL Items in Foreach are greater than $350. Right now, if I run this script, I get an endless loop of "350 detected"
add_action( 'woocommerce_after_calculate_totals', 'apply_coupon_depending_on_shipping_total', 999, 1 );
function apply_coupon_depending_on_shipping_total( $cart ) {
$coupon_code = 'coupon_code';
if ( $cart->has_discount( $coupon_code ) ) {
return;
}
$shipping_total = $cart->get_shipping_total();
if ( $shipping_total > 350 ) {
$cart->apply_coupon( $coupon_code );
}
}
In the Woocommerce store there are 3 shipping options
free shipping
flat rate
pickup
Inspired by Free shipping depending on weight and on minimal cart amount I would like to require a minimum total cart weight when choosing an shipping option.
When customers order for 24kg or less, they only should be able to pickup their order.
If the total cart weight is between 25kg and 49kg they should only may choose between the flat rate method or pickup.
And if the total cart weight is 50 kg and above they may choose between free shipping and pickup.
On the internet I found WooCommerce: How to Setup Tiered Shipping Rates by Order Amount, which I customized to work properly on the store because we have 2 shipping zones (so multiple flat rates, local pickup, etc).
Unfortunately it does not work for my needs, while I think the code is correct? Any help will be much appreciated.
add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 9999, 2 );
function bbloomer_woocommerce_tiered_shipping( $rates, $package ) {
if ( WC()->cart->get_cart_contents_weight() < 25 ) {
if ( isset( $rates['local_pickup:5'], $rates['local_pickup:9'] ) ) unset( $rates['flat_rate:3'], $rates['flat_rate:6'], $rates['free_shipping:1'], $rates['free_shipping:8'] );
} elseif ( WC()->cart->get_cart_contents_weight() < 50 ) {
if ( isset( $rates['flat_rate:3'], $rates['flat_rate:6'], $rates['local_pickup:5'], $rates['local_pickup:9'] ) ) unset( $rates['free_shipping:1'], $rates['free_shipping:8'] );
} else {
if ( isset( $rates['free_shipping:1'], $rates['free_shipping:8'], $rates['local_pickup:5'], $rates['local_pickup:9'] ) ) unset( $rates['flat_rate:3'], $rates['flat_rate:6'] );
}
return $rates;
}
Note: you cannot add multiple conditions to an if condition in the way you apply it.
Add the shipping options you want to keep under a certain condition to the $available_shipping array
When customers order for 24kg or less = local pickup
If the total cart weight is between 25kg and 49kg = local pickup and flat rate
If the total cart weight is 50 kg and above = local pickup and free shipping
function filter_woocommerce_package_rates( $rates, $package ) {
// Get cart contents weight
$weight = WC()->cart->get_cart_contents_weight();
// Conditions
if ( $weight <= 24 ) {
// Set available shipping options
$available_shipping = array( 'local_pickup' );
} elseif ( $weight > 24 || $weight < 50 ) {
$available_shipping = array( 'local_pickup', 'flat_rate' );
} else {
$available_shipping = array( 'local_pickup', 'free_shipping' );
}
foreach ( $rates as $rate_key => $rate ) {
// Targeting, NOT in array
if ( ! in_array( $rate->method_id, $available_shipping ) ) {
unset( $rates[$rate_key] );
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
I want to count shipping cost based on number of products add on cart like,
If I purchase one mobile then it will count shipping cost as 2.5 and after more than two or two mobile I purchased then shipping cost will be 5.0
<?php
$qty(1) * 2.5 = 2.5
$qty(2) * 2.5 = 5.0
$qty(3) * 2.5 = 5.0
?>
So is there any idea or suggestion how to count the shipping cost based on number of products ?
Updated:
As your question is a bit unclear, you could just need to add [qty]*2.5 in the Flat rate shipping method cost (for each shipping zone) in your wooCommerce shipping settings.
But it will not work if you have 2 different items in cart like: item1 (qty 1) + item2 (qty 1)
So this answer will do it in all cases:
1) First you will need to set a "Flat rate" shipping method for each Shipping Zones which cost will be set to 2.5 (in your WooCommerce shipping settings).
2) Adding this code that will calculate for each cart items (based on the total quantity of items) the new updated shipping cost:
add_filter( 'woocommerce_package_rates', 'custom_flat_rate_cost_calculation', 10, 2 );
function custom_flat_rate_cost_calculation( $rates, $package )
{
// The cart count (total items in cart)
$cart_count = WC()->cart->get_cart_contents_count();
$taxes = array();
// If there is more than 1 cart item
if( $cart_count > 1 ){
// Iterating through each shipping rate
foreach($rates as $rate_key => $rate_values){
// Targeting "Flat Rate" shipping method
if ( 'flat_rate' === $rate_values->method_id ) {
// Set the new calculated rate cost
$rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cumulated_active_quantity, 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] * $cumulated_active_quantity, 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 theme) or also in any plugin file.
This code is tested on WooCommerce 3+ and works
You will need to refresh shipping zones caches: disabling the Flat rate, then save. And enabling back this rate and save.
You can add a custom fee: Add to theme functions.php or use a plugin
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$price_per_mobile = 2.5;
$shipcharge = ( $woocommerce->cart->cart_contents_total * $price_per_mobile);
$woocommerce->cart->add_fee( 'Total Shipping Cost', $shipcharge, true, '' );
}
Go ahead with Woocommerce filter woocommerce_package_rates. Where you can customize all the shipping rates available in cart page.
Here is the code for adding extra cost to all items for both domestic and international shipement
add_filter('woocommerce_package_rates', 'wf_modify_rate', 10, 3);
function wf_modify_rate( $available_shipping_methods, $package ){
$origin_country = 'US';
$amount_to_add_domestic = 10;
$amount_to_add_inter_national = 20;
$amount_to_add = ($package['destination']['country'] == $origin_country) ?
$amount_to_add_domestic : $amount_to_add_inter_national;
$item_count = 0;
foreach ($package['contents'] as $key => $item) {
$item_count += $item['quantity'];
}
foreach ($available_shipping_methods as $methord_name => $methord) {
$available_shipping_methods[$methord_name]->cost += ($amount_to_add*$item_count);
}
return $available_shipping_methods;
}