Created
December 7, 2017 04:06
Star
You must be signed in to star a gist
Check for specific Product IDs in your cart to return true if they are there. WooCommerce/WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Check if a specific product ID is in the cart | |
*/ | |
function wc_ninja_product_is_in_the_cart() { | |
// Add your special product IDs here | |
$ids = array( '45', '70', '75' );; | |
// Products currently in the cart | |
$cart_ids = array(); | |
// Find each product in the cart and add it to the $cart_ids array | |
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
$cart_product = $values['data']; | |
$cart_ids[] = $cart_product->id; | |
} | |
// If one of the special products are in the cart, return true. | |
if ( ! empty( array_intersect( $ids, $cart_ids ) ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment