Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MadCowWeb/ad727c0e622bd6c6116f848fd961fae6 to your computer and use it in GitHub Desktop.
Save MadCowWeb/ad727c0e622bd6c6116f848fd961fae6 to your computer and use it in GitHub Desktop.
Add custom content to cart and checkout pages (only if a certain product ID is in the cart) with an action hook
// CHECK FOR SPECIFIC PRODUCT ID IN CART
function madcow_custom_product_id_is_in_the_cart($ids) {
// 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;
}
}
// ADD BANNER TO CART PAGE BASED ON PRODUCT IDs IN CART
add_action('woocommerce_before_cart', 'madcow_above_cart_and_checkout_message');
add_action('woocommerce_after_order_notes', 'madcow_above_cart_and_checkout_message');
function madcow_above_cart_and_checkout_message() {
$ids = array(20);
if (madcow_custom_product_id_is_in_the_cart($ids)) :
echo '<div class="top-cart-message"><h2>This is our cool message</h2></div>';
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment