Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Woocommerce Hide Shipping Address and Shipping Method by Product Category WordPress

  • SOLVED

I have been searching everywhere for some php code or plugin that would allow me to hide all shipping information by product category. I have a situation where I don't require any shipping information or shipping charges for a particular category.

The item is not a virtual or a downloadable product and by selecting virtual it does other things that I do require.

Answers (4)

2018-10-31

Reigel Gallarde answers:

this can be critical...
what if you have multiple categories involved in the cart?

have you found a solution yet?


Steven Sinfield comments:

No solution as yet. :(


Reigel Gallarde comments:

The only thing I can think of that can make a product to have no shipping is to make it virtual...
In your case you can make a filter for the is_virtual function and it should work without hurting other products...

so this should do it, I put it on pastebin because of formatting problem on this site.
https://pastebin.com/raw/qk0GHVbG


Let me know if this is what you need.

reference:
https://github.com/woocommerce/woocommerce/blob/12af00e38f9eeb06cea52c53f4701230c9b62630/includes/abstracts/abstract-wc-product.php#L1419


Reigel Gallarde comments:

in the code, replace 'uncategorized' with your category..


Reigel Gallarde comments:

please replace the if with this, I have done it the opposite on that one..
also added is_checkout(), so it will work only on checkout page...

if( is_checkout() && has_term( 'uncategorized', 'product_cat', get_post($product->get_id())) )


Steven Sinfield comments:

In uncatergorized do I put the category description, slug or id?


Reigel Gallarde comments:

as per https://codex.wordpress.org/Function_Reference/has_term
it can be the term name/term_id/slug or array of them to check for.

also, if this is a variation, you need to change get_id to get_parent_id

2018-10-30

Farid answers:

Can you share any related screenshots or your site URL for reference?

Thanks


Steven Sinfield comments:

Here is one of the screenshots of the information I want not to display for a particular product category.


Steven Sinfield comments:

And the zero dollar shipping field here because everything in this particular category has free shipping.


Farid comments:

If you are searching for plugin solution, try https://woocommerce.com/products/conditional-shipping-and-payments/. By using this plugin, you could add restrictions on the certain product or product categories.


Steven Sinfield comments:

That is a paid plugin I just want some PHP code that I can put into the functions.php of my theme to do exactly what I need.


Farid comments:

Then 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.

For the different conditions code examples based on different WC plugin versions you can consult https://docs.woocommerce.com/document/hide-other-shipping-methods-when-free-shipping-is-available/

If you have the basic coding knowledge and can get the product IDs then an example would be something like this:

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


You can set your own condition where only allow shipping methods that start with "local" in the code.

Thanks

2018-10-30

Arnav Joy answers:

can you please check this link
https://www.liquidweb.com/kb/way-conditionally-show-hide-checkout-fields-specific-products-product-categories-store/

or this
https://businessbloomer.com/woocommerce-disable-free-shipping-if-cart-has-shipping-class/


Steven Sinfield comments:

Hi Arnav,

As I am not a programmer and I know what I want I have been to both of those sites and was a little overwhelmed by the tech talk.

2018-10-30

Mohamed Ahmed answers:

Could you send me your site url to my email.
It's easy case and I will fix it.
my email: [email protected]

Regards