Skip to content

Instantly share code, notes, and snippets.

@pramodjodhani
Created September 3, 2025 06:15
Show Gist options
  • Save pramodjodhani/dba63a86705443a3bb3a21ba548dda41 to your computer and use it in GitHub Desktop.
Save pramodjodhani/dba63a86705443a3bb3a21ba548dda41 to your computer and use it in GitHub Desktop.
<?php
/**
* Check cart for given product id product.
*
* @param array $categories Categories.
*
* @return bool
*/
function iconic_check_for_cart_item_in_cart( $product_ids = array() ) {
if ( empty( $product_ids ) || empty( WC()->cart ) ) {
return false;
}
// Set our flag to be false until we find a product with matching ID.
$has_item = false;
// Loop through cart items to check for matching product IDs.
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
if ( in_array( $product_id, $product_ids ) ) {
$has_item = true;
break;
}
}
return $has_item;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment