How to check selected product in previous customer orders or not


$seletedProduct = 25;
$exist = ‘no’;
if(is_user_logged_in()){      // Check for customer login
$orders = get_posts( array(
‘numberposts’ => -1,
‘meta_key’ => ‘_customer_user’,
‘meta_value’ => get_current_user_id(),
‘post_type’ => ‘shop_order’,
‘post_status’ => array(‘wc-completed’, ‘wc-processing’)
) );
foreach ($orders as $order) {
$WC_Order = new WC_Order($order->ID);
$items = $WC_Order->get_items();
foreach ( $items as $item ) {
if($item[‘product_id’] == $seletedProduct){
$exist = ‘yes’;
}
}
}
}
if($exist == ‘no’){
echo ‘Product not ordered’;
}else{
echo ‘Product already ordered’;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s