Free shipping for the 100 first customers in Woocommerce - php

I am trying to find a way in Woocommerce to allow free shipping for the first 100 customers as a promotional event.
Once the limit of 100 first customers is reached, then standard shipping will be applied.
Is this possible? How can I do that?

Here is a simple the way to do it with the 2 hooked functions below that will:
Auto Add a coupon code to cart with "Free shipping" option enabled to 1st 100 customers.
To hide other shipping methods when "Free shipping" is available.
But you will have:
In WooCommerce > Settings > shipping, for each shipping zone set a "Free shipping" Method and chose one of this options:
A valid free shipping coupon
A minimum order amount OR a coupon
To set a special coupon code before in WooCommerce > Coupons with the following settings:
General > Discount type: Fixed cart
General > Amount: 0
General > Allow free shipping: enabled
Usage limit > Usage limit per coupon: 100
Usage limit > Usage limit per user: 1
Here is the code:
// Auto apply "free_shipping" coupon for first hundred
add_action( 'woocommerce_before_calculate_totals', 'auto_apply_free_shipping_coupon_first_hundred', 20, 1 );
function auto_apply_free_shipping_coupon_first_hundred( $cart ) {
// HERE define your free shipping coupon code
$coupon_code = 'summer';// 'freeship100';
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Get an instance of the WC_Coupon object
$coupon = new WC_Coupon( $coupon_code );
// After 100 usages exit
if( $coupon->get_usage_count() > 100 ) return;
// Auto-apply "free shipping" coupon code
if ( ! $cart->has_discount( $coupon_code ) && is_cart() ){
$cart->add_discount( $coupon_code );
wc_clear_notices();
wc_add_notice( __('You have win Free shipping for the first 100 customers'), 'notice');
}
}
// Hide Others Shipping methods when "Free shipping is available
add_filter( 'woocommerce_package_rates', 'hide_others_when_free_shipping_is_available', 100 );
function hide_others_when_free_shipping_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for WooCommerce version 3+

Related

How to change shipping method title based on cart amount?

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

If Shipping Cost is Greater Than X, Apply Coupon Code

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

Simple weight based taxable fee for a specific country in Woocommerce

In Woocommerce, I use following block of code that adds a custom fee in cart and checkout, based on total weight for a specific country:
function weight_add_cart_fee() {
// Set here your percentage
$percentage = 0.17;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Get weight of all items in the cart
$cart_weight = WC()->cart->get_cart_contents_weight();
// calculate the fee amount
$fee = $cart_weight * $percentage;
// If weight amount is not null, adds the fee calcualtion to cart
global $woocommerce;
$country = $woocommerce->customer->get_country();
if ( !empty( $cart_weight ) && $country == 'SK' ) {
WC()->cart->add_fee( __('Recyklačný poplatok (podľa váhy): ', 'my_theme_slug'), $fee, false );
}
}
add_action( 'woocommerce_cart_calculate_fees','weight_add_cart_fee' );
But I need to make this fee taxable. How to make it taxable?.
There is some mistakes and your code is outadated. Try the following instead (with a taxable fee):
add_action( 'woocommerce_cart_calculate_fees','add_fee_weight_based', 10 , 1 );
function add_fee_weight_based( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.17; // Percentage
$targeted_country = 'SK'; // Country
$cart_weight = $cart->get_cart_contents_weight(); // Total weight
if ( $cart_weight > 0 && WC()->customer->get_shipping_country() == $targeted_country ) {
$cart->add_fee( __('Recyklačný poplatok (podľa váhy): ', 'my_theme_slug'), ($cart_weight * $percentage), true );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Make a fee taxable
To make a fee taxable, in the WC_Cart add_fee() method, you need to set the 3rd argument to true (taxable)…
The 4th optional argument is related to the tax class that you can specify if you need to set a specific tax class

Disable only flat rate shipping method when free shipping is available in Woocommerce

I am using Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3 lightly changed answer code to hide all shipping methods except one. The only method I want showing is a rate from the "Woocommerce Advanced Shipping" plugin.
I am using the correct rate ID etc...
Everything works fine except when a customer tries to click that shipping method, it won't stay selected. It just jumps back to free shipping.
I have tried debugging and also tried the code with a native woocommerce flat rate ID and it showed up/able to select it just fine.
add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 100, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {
$flat_rates_express = array( '2588' );
$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:12' === $rate->id )
$free[ $rate_key ] = $rate;
}
return ! empty( $free ) ? array_merge( $free, $flat2 ) : $rates;
}
ID I want to Keep Shown: "2588" (Custom Shipping Rate From Plugin)
How can I disable the Flat rate shipping method when free shipping is available o and keep a custom shipping rate (from a plugin)?
As you have 3 shipping methods, 1 free shipping, 1 flat rate and 1 custom '2588', it's possible to hide the flat rate shipping method when free shipping is available instead:
add_filter( 'woocommerce_package_rates', 'free_shipping_disable_flat_rate', 1000, 2 );
function free_shipping_disable_flat_rate( $rates, $package ) {
// Here your free shipping rate Id
$free_shipping_rate_id = 'free_shipping:12';
// When your Free shipping method is available
if ( array_key_exists( $free_shipping_rate_id, $rates ) ) {
// Loop through shipping methods rates
foreach ( $rates as $rate_key => $rate ) {
// Removing "Flat rate" shipping method
if ( 'flat_rate' === $rate->method_id ){
unset($rates[$rate_key]);
}
}
}
return $rates;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Refresh the shipping caches:
This code is already saved on your function.php file.
In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.

Deposit based on a percentage of total cart amount

I've taken this code from another post and basically from my understanding, this code is trying to force the cart price to change to a fixed amount of $40 and charge it as a booking fee.
What I want to do is force the cart amount to be 20% of what the total would be based on adding up all the products in the cart. My site is for reservations, so I only want to charge a deposit and then have them pay when they use their reservation.
Here is the code from this post: Woocommerce cart deposit
add_action( 'woocommerce_cart_calculate_fees', 'booking_fee' );
function booking_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$bookingfee_array = array( '2434' );
$fixed = 40.00;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if( in_array( $values['product_id'], $bookingfee_array ) ) {
$surcharge = $fixed;
$woocommerce->cart->add_fee( 'Broneeringutasu', $surcharge, true, '' );
}
}
}
And here is how I would change it:
add_action( 'woocommerce_cart_calculate_fees', 'booking_fee' );
function booking_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$bookingfee_array = array( '2434' );
$percent = .20;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if( in_array( $values['product_id'], $bookingfee_array ) ) {
$surcharge = $percent;
$woocommerce->cart->add_fee( 'Booking Fee', $surcharge, true, '' );
}
}
}
But this is not working as expected.
Any help on this?
Thanks
Your code is not really going to do what you expect, as you are adding a fee of o.2 to total cart amount. So if the cart amount is 100, the grand total is going to be 100.2…
What you want to do instead, is to remove 80% of total cart amount to get a cart amount of 20%. This is possible using a negative fee of 80% from total cart amount. If you don't need to set targeted products IDs as in your original code, use the 2nd function below.
Here is the first function that will remove 80% of total cart amount when a cart item match with the targeted product IDs set in the function:
add_action( 'woocommerce_cart_calculate_fees', 'booking_deposit_calculation' );
function booking_deposit_calculation( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
## Set HERE your targeted products IDs
$target_product_ids = array( 2434 );
## Set HERE your negative percentage (to remove an amount from cart total)
$percent = -.80; // 80% off (negative)
$matching = false;
// Iterating through each cart items
foreach ( $cart_object->get_cart() as $item_values )
{
if( in_array( $item_values['product_id'], $target_product_ids ) )
{ // If a cart item match with a targeted product ID
// Get cart subtotal excluding taxes
$cart_subtotal = $cart_object->subtotal_ex_tax;
// or for subtotal including taxes use instead:
// $cart_subtotal = $cart_object->subtotal;
## ## CALCULATION ## ##
$calculated_amount = $cart_subtotal * $percent;
// Adding a negative fee to cart amount (Including taxes)
$cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, true );
break; // We stop the loop
}
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
But may be you don't need to have some targeted products as in the original code.
If it's the case this simplify the code:
add_action( 'woocommerce_cart_calculate_fees', 'booking_deposit_calculation' );
function booking_deposit_calculation( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
## Set HERE your negative percentage (to remove an amount from cart total)
$percent = -.80; // 80% off (negative)
// Get cart subtotal excluding taxes
$cart_subtotal = $cart_object->subtotal_ex_tax;
// or for subtotal including taxes use instead:
// $cart_subtotal = $cart_object->subtotal;
## ## CALCULATION ## ##
$calculated_amount = $cart_subtotal * $percent;
// Adding a negative fee to cart amount (Including taxes)
$cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, true );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Both functions are tested and works

Categories