Skip to content

Instantly share code, notes, and snippets.

@wplaunchify
Forked from ajmorris/functions.php
Created February 24, 2020 05:54
Show Gist options
  • Save wplaunchify/cd34341f68dad1144fcc6c3b7b8d77d5 to your computer and use it in GitHub Desktop.
Save wplaunchify/cd34341f68dad1144fcc6c3b7b8d77d5 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