0

I try to unset Chash on delivery (unset( $available_gateways['cod'] );) in specific category.

So i need to get the category id of all product i select it in checkout page woocomerce.

How can do that!

0

Getting the ids of the items in your cart can be done somewhat like this:

global $woocommerce;
$cart = $woocommerce->cart->get_cart();
$cart_items_ids = array();
foreach ( $cart as $item_key => $item_value ) {
    $cart_items_ids[] = $item_value[ 'data' ]->id;
}

OR Plese check below Snippet: Check if Product Category is in the Cart – WooCommerce

add_action('woocommerce_before_cart', 'xyz_check_category_in_cart');

function xyz_check_category_in_cart() {
    // Set $cat_in_cart to false
    $cat_in_cart = false;

    // Loop through all products in the Cart        
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];

        // If Cart has category "download", set $cat_in_cart to true
        if ( has_term( 'download', 'product_cat', $product->get_id() ) ) {
            $cat_in_cart = true;
            break;
        }
    }

    // Do something if category "download" is in the Cart      
    if ( $cat_in_cart ) {

        // For example, print a notice
        wc_print_notice( 'Category Downloads is in the Cart!', 'notice' );

        // Or maybe run your own function...
    }
}

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it).

Please let me know in the comments if everything worked as expected.

Thank you!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.