cart && ! WC()->cart->is_empty() ) {
foreach ( WC()->cart->get_cart() as $item ) {
$cart_product_ids[] = $item['product_id'];
$terms = wp_get_post_terms( $item['product_id'], 'product_cat', array( 'fields' => 'ids' ) );
$cart_categories = array_merge( $cart_categories, $terms );
}
$cart_categories = array_unique( $cart_categories );
}
$rec_args = array(
'post_type' => 'product',
'posts_per_page' => 8,
'post__not_in' => $cart_product_ids,
'ignore_sticky_posts' => 1,
'post_status' => array( 'publish', 'draft' ),
'orderby' => 'rand',
'meta_query' => array(
array(
'key' => '_is_recommended_product',
'value' => 'yes',
),
),
);
if ( ! empty( $cart_categories ) ) {
$rec_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $cart_categories,
'operator' => 'IN',
),
);
}
$rec_query = new WP_Query( $rec_args );
$rec_ids = wp_list_pluck( $rec_query->posts, 'ID' );
$remaining = 8 - count( $rec_ids );
$random_products = array();
if ( $remaining > 0 ) {
$exclude = array_merge( $cart_product_ids, $rec_ids );
$rand_args = array(
'post_type' => 'product',
'posts_per_page' => $remaining,
'post__not_in' => $exclude,
'ignore_sticky_posts' => 1,
'post_status' => array( 'publish', 'draft' ),
'orderby' => 'rand',
);
if ( ! empty( $cart_categories ) ) {
$rand_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $cart_categories,
'operator' => 'IN',
),
);
}
$rand_query = new WP_Query( $rand_args );
$random_products = $rand_query->posts;
}
$all_products = array_merge( $rec_query->posts, $random_products );
if ( ! empty( $all_products ) ) :
global $post;
?>