
[英]Conditional function that check if products are already in cart in Woocommerce 3
[英]Check if products are already in cart
我对 Woocommerce 很陌生,想问一个问题。 我做了一个功能(如下),将 10 个产品随机添加到用户的购物车中。 一切都很好,我唯一的问题是,在将产品添加到该链接之前,我需要检查产品是否在购物车中。 因为否则产品将不会添加到购物车。 有没有办法比较产品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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.