Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 944 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hide shipping methods for specific shipping class in WooCommerce

#1
Essentially I'm trying to make the flat rate method Id `flat_rate:7` **disabled** when there is cart items that have the shipping class "Roller" (ID `92`).

**This is the code I tried:**

add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 10, 2);

function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package)
{
$hide_when_shipping_class_exist = array(
92 => array(
'flat_rate:7'
)
);

$shipping_class_in_cart = array();
foreach(WC()->cart->cart_contents as $key => $values) {
$shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
}

foreach($hide_when_shipping_class_exist as $class_id => $methods) {
if(in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}

return $available_shipping_methods;
}

Shipping class ID **`92`** is the shipping class and I want to hide `flat_rate:7` for it.

My Site is this:

[To see links please register here]

WordPress: 4.8.4
WooCommerce: 3.1.1

Any help will be greatly appreciated.
Reply

#2
By tweaking LoicTheAztec's code (cheers), I was able to unset a shipping method for ***each package*** based on the shipping class of its contents, rather than the cart as a whole. Perhaps it will help someone else too:

// UNSET A SHIPPING METHOD FOR PACKAGE BASED ON THE SHIPPING CLASS(es) OF ITS CONTENTS
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}

foreach( $package['contents'] as $package_item ){ // Look at the shipping class of each item in package

$product_id = $package_item['product_id']; // Grab product_id
$_product = wc_get_product( $product_id ); // Get product info using that id

if( $_product->get_shipping_class_id() != 371 ){ // If we DON'T find this shipping class ID
unset($rates['wbs:9:dae98e94_free_ups_ground']); // Then remove this shipping method
break; // Stop the loop, since we've already removed the shipping method from this package
}
}
return $rates;
}

This code allows me to unset my 'Free UPS Ground' shipping if the package contains anything but 'Standard' items (shipping_class_id 371 in my case).

The scenario from the original post (disable method ***x*** if shipping class ***y***) would work like this:

// UNSET A SHIPPING METHOD FOR PACKAGE BASED ON THE SHIPPING CLASS(es) OF ITS CONTENTS
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}

foreach( $package['contents'] as $package_item ){ // Look at the shipping class of each item in package

$product_id = $package_item['product_id']; // Grab product_id
$_product = wc_get_product( $product_id ); // Get product info using that id

if( $_product->get_shipping_class_id() == 92 ){ // If we DO find this shipping class ID
unset($rates['flat_rate:7']); // Then remove this shipping method
break; // Stop the loop, since we've already removed the shipping method from this package
}
}
return $rates;
}
Reply

#3
**Update 2019:** You should try instead this shorter, compact and effective way:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

// HERE define your shipping class to find
$class = 92;

// HERE define the shipping method to hide
$method_key_id = 'flat_rate:7';

// Checking in cart items
foreach( $package['contents'] as $item ){
// If we find the shipping class
if( $item['data']->get_shipping_class_id() == $class ){
unset($rates[$method_key_id]); // Remove the targeted method
break; // Stop the loop
}
}
return $rates;
}

*Code goes in function.php file of your active child theme (or theme) or also in any plugin file.*

Tested and works.

>Sometimes, you should may be need to refresh shipping methods going to shipping areas, then disable / save and re-enable / save your "flat rates" shipping methods.

**Related:**

[To see links please register here]


> **To find the shipping methods IDs and the shipping classes IDs see below…**

-----

Update for many different shipping methods (related to your comments):

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

// HERE define your shipping class to find
$class = 92;

// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:7', 'local_pickup:3');

// Checking in cart items
foreach( $package['contents'] as $item ) {
// If we find the shipping class
if( $item['data']->get_shipping_class_id() == $class ){
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]); // Remove the targeted methods
}
break; // Stop the loop
}
}
return $rates;
}

Tested and works…

-------

**Finding the shipping class ID.**

1) In the database under `wp_terms` table:

Search for a term name or a term slug and you will get the term ID (the shipping class ID).

2) On Woocommerce shipping settings editing a "Flat rate", with your browser html inspector tool, inspect a shipping Class rate field like:

[![enter image description here][1]][1]

In the imput name attribute you have `woocommerce_flat_rate_class_cost_64`. So 64 is the ID for the shipping class.

------

**Get the shipping method rate ID:**

>To get the related **shipping methods rate IDs**, something like **`flat_rate:12`**, inspect with your browser code inspector each related radio button attribute **`value`** like:
>
>[![enter image description here][1]][1]

[1]:


[2]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through