I am writing the function which should check up whether there is in the cart an item, which has a particular category.
My idea was:
add_filter( 'woocommerce_package_rates', 'remove_flat_rate_from_used_products', 10, 2 );
function remove_flat_rate_from_used_products($rates, $package) {
if( is_woocommerce() && ( is_checkout() || is_cart() ) ) {
if( check_whether_item_has_the_category() ) {
unset( $rates['flat_rate'] );
}
}
return $rates;
}
I guessed, the get_cart() function returns the contents of the cart, and I would be able to get information about items categories out there. I need to know the structure of the array get_cart() returns, so I wrote:
function check_whether_item_has_the_category() {
global $woocommerce;
var_dump(WC()->cart->get_cart());
}
And got
Warning: Invalid argument supplied for foreach() in ...wp-content\plugins\woocommerce\includes\class-wc-shipping.php on line 295
Then I tried find the category name in results of the get_cart() function:
function check_whether_item_has_the_category() {
global $woocommerce;
if( in_array('used', WC()->cart->get_cart()) ) {
echo 'do something';
}
}
And got the same error.
The use of $woocommerce instead of WC() give nothing, as well as the remove of global $woocommerce
What am I doing wrong and how do I get items categories (or check them for existing of the particular one)?