Skip to content

Instantly share code, notes, and snippets.

@ajmorris
Created December 7, 2017 04:06
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ajmorris/4b7895c7ff18e82a817f6c00a9bbe080 to your computer and use it in GitHub Desktop.
Check for specific Product IDs in your cart to return true if they are there. WooCommerce/WordPress
<?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