I am quite new to Woocommerce and would like to ask a question. I have made a function (below), that adds randomly 10 products to user's cart. Everything works great, the only problem that I have is, that I need to check if the the product is in cart before adding it to that link. Because otherwise the product will not be added to cart. Is there any way to compare the product id?
function add_products(){
$args = array(
'post_type' => 'product',
'orderby' => 'rand',
'product_cat' => 'seeds',
'post_status' => 'publish',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_visibility',
'terms' => array( 'exclude-from-catalog', 'exclude-from-search' ),
'field' => 'name',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'vip',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'retailer',
'operator' => 'NOT IN',
)
),
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock',
),
)
);?>
<div class="add-products"><a href="/?fill_cart=<?
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$id = $product->get_id();
echo $id?>,<?php endwhile; ?>
<?php wp_reset_query(); ?>">
Add products to cart!
</a></div>
<?php
}