I am building an eCommerce website by WordPress and stuck in one place. I want to set a function on my website. When buyer buys $30+ product then I want to make shipping cost zero. So I cannot understand how to do it. Please, someone, help me.
Something like this should do the trick.
add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates', 10, 2 );
function woocommerce_package_rates( $rates, $package ) {
if ( WC()->cart->subtotal > 30 ) {
foreach($rates as $key => $rate ) {
$rates[$key]->cost = 0;
}
}
return $rates;
}
You can find more examples about shipping fee here:
http://reigelgallarde.me/programming/woocommerce-shipping-fee-changes-condition-met/
Related
I am trying to hide a specific class when cart subtotal on Woocommerce is above a certain amount.
I have used the code below but have not achieved anything yet. Thank you in advance.
add_filter( 'woocommerce_package_rates', 'hide_specific_class', 10, 2 );
function hide_specific_class($classes, $package ) {
$cart_subtotal = WC()->cart->get_subtotal();
if ( $cart_subtotal > 20 ) {
unset($classes[array_search('inactive-shipping', $classes)]);
return $classes;
}
}
`
Hi all :) I understand that there are a lot of such topics, but, unfortunately, I cannot apply any code, and I do not know how to redo and apply it.
I added an additional shipping method for specific countries using the Innozilla Per Product Shipping WooCommerce plugin for a specific product
when selecting a specific country, I see old shipping methods like flexible_shipping_9_2, flat_rate: 12 and + new "per_product" shipping method
I would like to hide all delivery methods except per_product when the per_product delivery method is used.
How to do it? Thank you in advance.
add_filter( 'woocommerce_package_rates', 'businessbloomer_hide_free_shipping_for_shipping_class', 10, 2 );
function businessbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) {
$shipping_class_target = 15; // shipping class ID
$in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
if ( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
$in_cart = true;
break;
}
}
if ( $in_cart ) {
unset( $rates['free_shipping:8'] ); // shipping method with ID
}
return $rates;
}
pls how can I disable local pickup depending on a cart total weight? I want to disable possibility for local pickup when the cart total weight == 0. I used this snippet found elsewhere and modified, but it is not working and the local pickup is still offered
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() == 0 ) {
unset( $rates['local_pickup'] );
unset( $rates['local_delivery'] );
}
return $rates;
}
What am I doing wrong please?
Actually my code mentioned above is working. I did not realized two things:
ID of my local_pickup was wrong, I did not explore the page to find out, that the correct variable name is 'local_pickup:13' (code updated)
To see changes on the web I always have to open the site in anonymous window. Even Ctrl+F5, Ctrl+Shift+R in Chrome does not help.
You can count total weight by WC()->cart->cart_contents loop.
try this code
add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_weight', 10, 2 );
function hide_shipping_based_on_weight( $rates, $package ) {
$cart_total_weight = 0;
$shipping_id = 'local_pickup'; // your shipping id
// Calculate cart weight total.
foreach( WC()->cart->cart_contents as $key => $value ){
$cart_total_weight += $value['data']->weight * $value['quantity'];
}
// check cart total
if( $cart_total_weight <= 0 ){
unset( $rates[ $shipping_id ] );
}
return $rates;
}
For a client, I need to let backorders for products but they don't want to charge for those. So, I need to update the price dynamically if they stock is 0 or the stock is less than the quantity on the cart.
This is my code:
add_filter( 'woocommerce_cart_product_subtotal', 'modify_cart_product_subtotal', 10, 4 );
function modify_cart_product_subtotal( $product_subtotal, $product, $quantity, $cart ) {
if ( $product->get_stock_quantity() < $quantity ) {
return $product->get_stock_quantity() * $product->get_price();
}
return $product_subtotal;
}
In case of 0 stock, it works like a charm. In case of a quantity greater than the stock, it charges only for the available products, but the tax keeps increasing even for the rest. How can I exclude those from taxes calculations as well?
EDIT:
If I just update the price of the product, it does it for all of them, not only those above the available stock.
function zero_price_for_backorders( $cart_object ) {
global $isProcessed;
if( !WC()->session->__isset( "reload_checkout" )) {
foreach ( $cart_object->get_cart() as $key => $value ) {
if ( $value['data']->get_stock_quantity() < $value['quantity']) {
$value['data']->set_price(0);
}
}
$isProcessed = true;
}
}
add_action( 'woocommerce_before_calculate_totals', 'zero_price_for_backorders', 99 );
You should use before_calculate_total and change product price using set_price function so that you don't have to modify subtotal separately. The rest of the flow will fall in place automatically.
You can have a look at this link on how to change it dynamically:
Change product price dynamically woocommerce
I have a WooCommerce online shop that offers shipping to most products. Some of the products are for local pickup. I've tried setting a class on shipping zones with cost equal to zero and assigning the class on the products. But so far, the checkout still displays the shipping cost. Is there any way where some products will not have a shipping cost?
If you are searching for plugin solution, try WooCommerce Conditional Shipping and Payments. By using this plugin, you could add restrictions on certain product or product categories.
You've might want to look into the woocommerce_package_rates filter, which allows you to filter the set of shipping options that are available to the customer. An example would be something like this:
<?php
// add this snippet to functions.php:
add_filter( 'woocommerce_package_rates', function ( $rates, $package ) {
// examine $package for products. this could be a whitelist of specific
// products that you wish to be treated in a special manner...
$special_ids = array( 1, 2, 3, 4, 5 );
$special_product_present = false;
foreach ( $package['contents'] as $line_item ) {
if ( in_array( $line_item['product_id'], $special_ids ) ) {
$special_product_present = true;
}
}
$rates = array_filter( $rates, function ( $r ) use ( $special_product_present ) {
// do some logic here to return true (for rates that you wish to be displayed), or false.
// example: only allow shipping methods that start with "local"
if ( $special_product_present ) {
return preg_match( '/^local/', strtolower( $r->label ) );
} else {
return true;
}
} );
return $rates;
}, 10, 2 );
This blog post here shows some variations on that idea using this hook, including how to customize the available rates based on shopping cart value, customer's country, number of items in the cart, etc. And here's the source code: https://github.com/woocommerce/woocommerce/blob/v2.2.3/includes/class-wc-shipping.php#L366