Reputation: 41
I would like to add this feature of hiding shipping method but one to all products if a variation has been chosen. There are thousands of products and there will be more in the future. I used this code:
$_product = $values['data'];
// if product variation id @ Cart, disable all but Local Pickup
if( $_product->variation_id == 26408 || $_product->variation_id == 26409 ) {
$wc_pickup_store = $rates['wc_pickup_store'];
$rates = array();
$rates['wc_pickup_store'] = $wc_pickup_store;
}
}
return $rates;
}
What I am trying to achieve is to replace the variation ID by the variation name instead so that this can be applied to all of products that has the same variation name.
Thanks for the help!
Upvotes: 2
Views: 1743
Reputation: 254373
Your code is outdated since WooCommerce 3. For the cart item variation ID, you should use:
$variation_id = $values['variation_id'];
The variation name is the product variation title, that you can get using the WC_Product
method get_name()
.
If the variation id is an integer higher than zero, $_product->get_name()
will be the variation name. Otherwise it will be the product name (of a simple product for example).
So your code will be:
$_product = $values['data'];
$variation_id = $values['variation_id'];
$product_name = $_product->get_name(); // The variation name
// if product variation id @ Cart, disable all but Local Pickup
if( $variation_id == 26408 || $variation_id == 26409 ) {
$wc_pickup_store = $rates['wc_pickup_store'];
$rates = array();
$rates['wc_pickup_store'] = $wc_pickup_store;
}
}
return $rates;
}
Upvotes: 2