Skip to content

Instantly share code, notes, and snippets.

@chrismademe
Created January 15, 2020 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismademe/c69d6af0fb29d98ffe7c9bf230843ba8 to your computer and use it in GitHub Desktop.
Save chrismademe/c69d6af0fb29d98ffe7c9bf230843ba8 to your computer and use it in GitHub Desktop.
WooCommerce - order_has_product
<?php
/**
* Order Has Product
*
* Checks a WooCommerce order to see if it contains
* a given product ID
*
* @param object $order
* @param int $product_id
* @return bool
*/
function order_has_product( $order, $product_id ) {
$items = $order->get_items();
$has_product = false;
foreach ( $items as $item ) {
if ( $item->get_product_id() === $product_id ) {
$has_product = true;
break;
}
}
return $has_product;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment