How to make shipping cost free in woocommerce? - wordpress

I have a woocommerce website in which there are 2 products, one product has free shipping but other has paid shipping if they are added separately in cart. Now I have a scenario that if some customer adds both free and paid shipping products together in shopping cart then shipping should become free for that whole order. How can I achieve this?
Thanks,
Mohammad

try this... just paste this in your theme's functions.php and replace the id in $products_to_look = array( 34 ); below.
add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates' );
function woocommerce_package_rates( $rates ) {
if ( ! WC()->cart->is_empty() ) {
$products_to_look = array( 34 ); // ids of products separated by comma.
$found = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( $found = in_array( $cart_item['product_id'], $products_to_look ) ) {
break;
}
}
}
if ( $found ) {
foreach($rates as $key => $rate ) {
$rates[$key]->label = 'Free shipping'; // change label to Free shipping...
$rates[$key]->cost = 0; // cost is set to 0.
}
}
return $rates;
}
Further readings: WooCommerce shipping fee that changes when condition is met

Related

Remove Woocommerce shipping option for one particular city

Need some help on this...Please :)
I have this site that is using the Woocommerce's shipping option for "Starken por pagar", that works globally for all cities
And right after that is "Envío Gratis" that is send it for my actual shipping plugin, that calculate free shipping "Envío Gratis" when one particular city is selected.
My idea is that I need to remove "Starken por pagar" when the user select one particular city (one only - the same that is used by my shipping plugin) and left only the "Envío Gratis" for that city.
Because now I dont need the users need to pay for some service that is free. And as you may know, some people barely read the things in front of ther eyes :P
I tried with this (Im not an expert, sorry)
function filter_woocommerce_package_rates( $rates, $package ) {
// Loop trough
foreach ( $rates as $rate_id => $rate ) {
// Checks if a value exists in an array, multiple can be added, separated by a comma
if ( in_array( $rate->method_id, array( 'local_pickup', 'free_shipping' ) ) ) {
unset( $rates['flat_rate:28'] );
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
Or
// Hide shipping in cart page.
add_filter( 'woocommerce_product_needs_shipping', 'disable_shipping_in_cart_page', 10, 2 );
function disable_shipping_in_cart_page( $needs_shipping, $product ){
if ( is_cart() )
$needs_shipping = false;
return $needs_shipping;
}
// Conditionally remove Flat Rate which qualifies for free shipping
add_filter( 'woocommerce_package_rates', 'hide_flat_rate_when_free_is_available', 10, 3 );
function hide_flat_rate_when_free_is_available( $rates, $package ) {
$free = array();
$free_available = false;
foreach ( $rates as $rate_key => $rate ) {
if ( 'free_shipping' === $rate->method_id ){
$free_available = true;
break;
}
}
foreach ( $rates as $rate_key => $rate ) {
if ( $free_available && 'flat_rate' === $rate->method_id )
unset($rates[$rate_key]);
}
return $rates;
}
Thank you :)

Show or hide checkout postcode field based on specfic product in WooCommerce

in WooCommerce, I am trying to hide the postal code field on checkout whenever specific product is selected. I've tried a bunch of different things.
This is my last attempt:
/**
* Conditionally remove a checkout field based on products in the cart
*/
function wc_ninja_product_is_in_the_cart() {
// Add your special product IDs here
$ids = array( '9531', '9072', '9035' );;
// Products currently in the cart
$cart_ids = array();
// Find each product in the cart and add it to the $cart_ids array
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = $values['data'];
$cart_ids[] = $cart_product->id;
}
// If one of the special products are in the cart, return true.
if ( ! empty( array_intersect( $ids, $cart_ids ) ) ) {
return true;
} else {
return false;
}
}
function wc_ninja_remove_checkout_field( $fields ) {
if ( ! wc_ninja_product_is_in_the_cart() ) {
unset( $fields['billing']['billing_postcode'] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field' );
<!-- end snippet -->

Woocommerce: I want to disable "free shipping" when the cart contains only products of the same category "B"

someone gives me a suggestion to understand how to correctly write the shipping function in woocommerce to get this result:
a) Shipping costs are € 10 and are free for orders over € 100
b) I have two categories A and B
c) if in the cart there are products of ONLY CATEGORY B, the rule of free shipping for orders over € 100 must not be applied. So the shipping must be 10 €.
For point c) I have entered this function in function.php, but there is something wrong:
// Disable Woocommerce shipping methods based on specific category
add_filter( 'woocommerce_package_rates', 'hide_shipping_for_categories', 10, 2 );
function hide_shipping_for_categories( $rates, $package ) {
// Add your own Woocommerce categories here (either category ID, slug or name)
$terms = array( 'I nostri sottoli' );
$taxonomy = 'product_cat'; // If you need to hide shipping based on tags, then change it to product_tag
// Add shipping methods to be removed (like "local_pickup:8")
$method_instances_ids = array('free_shipping:4');
$found = false;
// Loop through cart items checking for defined product IDs
foreach( $package['contents'] as $cart_item ) {
if ( has_term( $terms, $taxonomy, $cart_item['product_id'] ) ){
$found = true;
break;
}
}
if ( ! $found ) return $rates; // If not found we exit
// Loop through your active shipping methods
foreach( $rates as $rate_id => $rate ) {
// Remove all other shipping methods other than your defined shipping method
if ( in_array( $rate_id, $method_instances_ids ) ){
unset( $rates[$rate_id] );
}
}
return $rates;
}

Request - How to add Handling Fee based on Category of Product? [duplicate]

I already found this code here and it works for like 80/90% for me (see below).
This code adds 60 euro to my cart when there is a product from category ID 349 in the cart. When I add a product from that category to my cart when the cart is empty it works fine. But when there is already a product in my cart from a different category and then I add the product with category 349 it doesn't add the 60 euro extra fee. How is this possible?
function woo_add_cart_fee() {
$category_ID = '349';
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
// Get the terms, i.e. category list using the ID of the product
$terms = get_the_terms( $values['product_id'], 'product_cat' );
// Because a product can have multiple categories, we need to iterate through the list of the products category for a match
foreach ($terms as $term) {
// 349 is the ID of the category for which we want to remove the payment gateway
if($term->term_id == $category_ID){
$excost = 60;
}
}
$woocommerce->cart->add_fee('Extra bezorgkosten kunstgras', $excost, $taxable = false, $tax_class = '');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
The code you are using is a bit outdated and you should use has_term() Wordpress conditional function to target a product category this way:
add_action( 'woocommerce_cart_calculate_fees','custom_pcat_fee', 20, 1 );
function custom_pcat_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Set HERE your categories (can be term IDs, slugs or names) in a coma separated array
$categories = array('349');
$fee_amount = 0;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
if( has_term( $categories, 'product_cat', $cart_item['product_id']) )
$fee_amount = 60;
}
// Adding the fee
if ( $fee_amount > 0 ){
// Last argument is related to enable tax (true or false)
WC()->cart->add_fee( __( "Extra bezorgkosten kunstgras", "woocommerce" ), $fee_amount, false );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
A fee of 60 will always be added if there is in cart an item from 349 product category ID.
I found a solution like the following. Note that I kept adding a fee, because I started from a previous snippet. You can apply an existing discount or calculate a different total according to your needs.
In this case, I apply a negative fee - like a discount - according to a >= 300 total value of the cart, whose values would be -25% only for local_pickup shipping method, calculating taxes in addition.
function discount_to_cat(){
// thanks to LoicTheAztec's snippet
$cat_in_cart = false;
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( 'category_to_pick', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
$tot_category_price = $cart_item['data']->get_price();
$tot_category_qty = $cart_item['quantity'];
$tot_category = $tot_category_price * $tot_category_qty;
break;
}
} global $product;
$total = WC()->cart->subtotal;
$discount_label = "";
if($total >= 300){
$discount_label=15;
}
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = explode(':',$chosen_methods[0]);
if($chosen_shipping[0]=='local_pickup'){
$discount_label=25;
}
$discount_applied = ($total-$tot_category)*$discount_label/100;
if($discount_label!=""){
$discount_applied_net = ($discount_applied/1.1); //1.1 according taxes for shipping
WC()->cart->add_fee( "Discount applied: ($discount_label%)", -$discount_applied_net, false );
}
}
add_action( 'woocommerce_cart_calculate_fees','discount_to_cat' );

Remove checkout fields for a defined product category in Woocommerce

In woocommerce I am trying to remove unwanted checkout shipping fields for a product category "house".
Here is my code:
function woo_custom_category_is_in_the_cart( $categories ) {
// Products currently in the cart
$cart_ids = array();
// Categories currently in the cart
$cart_categories = array('house');
// Find each product in the cart and add it to the $cart_ids array
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = $values['data'];
$cart_ids[] = $cart_product->id;
}
// Connect the products in the cart w/ their categories
foreach( $cart_ids as $id ) {
$products_categories = get_the_terms( $id, 'product_cat' );
// Loop through each product category and add it to our $cart_categories array
foreach ( $products_categories as $products_category ) {
$cart_categories[] = $products_category->slug;
}
}
// If one of the special categories are in the cart, return true.
if ( ! empty( array_intersect( $categories, $cart_categories ) ) ) {
return true;
} else {
return false;
}
}
/************************************************
* Remove unwanted checkout fields on condition *
************************************************/
function woo_custom_remove_checkout_field( $fields ) {
$categories = array( 'house' );
// If a special category is in the cart, hide the following billing fields
if ( woo_custom_category_is_in_the_cart( $categories ) ) {
// hide the billing fields
unset($fields['shipping']['shipping_first_name']);
unset($fields['shipping']['shipping_last_name']);
unset($fields['shipping']['shipping_company']);
unset($fields['shipping']['shipping_address_1']);
unset($fields['shipping']['shipping_address_2']);
unset($fields['shipping']['shipping_city']);
unset($fields['shipping']['shipping_postcode']);
unset($fields['shipping']['shipping_country']);
unset($fields['shipping']['shipping_state']);
unset($fields['shipping']['shipping_phone']);
// hide the additional information section
add_filter('woocommerce_enable_order_notes_field', '__return_false');
add_filter( 'woocommerce_ship_to_different_address_checked', '__return_false' );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'woo_custom_remove_checkout_field' );
But the code removes the fields on for other product categories too…
What I am doing wrong?
Any help is appreciated.
I have revisited and simplified your code:
add_filter( 'woocommerce_checkout_fields', 'conditionally_remove_checkout_fields', 25, 1 );
function conditionally_remove_checkout_fields( $fields ) {
// HERE the defined product Categories
$categories = array('house');
$found = false;
// CHECK CART ITEMS: search for items from our defined product category
foreach ( WC()->cart->get_cart() as $cart_item ){
if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$found = true;
break;
}
}
// If a special category is in the cart, remove some shipping fields
if ( $found ) {
// hide the billing fields
unset($fields['shipping']['shipping_first_name']);
unset($fields['shipping']['shipping_last_name']);
unset($fields['shipping']['shipping_company']);
unset($fields['shipping']['shipping_address_1']);
unset($fields['shipping']['shipping_address_2']);
unset($fields['shipping']['shipping_city']);
unset($fields['shipping']['shipping_postcode']);
unset($fields['shipping']['shipping_country']);
unset($fields['shipping']['shipping_state']);
unset($fields['shipping']['shipping_phone']);
// hide the additional information section
add_filter('woocommerce_enable_order_notes_field', '__return_false');
add_filter( 'woocommerce_ship_to_different_address_checked', '__return_false' );
}
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Resources