WooCommerce - How to check particular product is i

2019-09-09 01:31发布

Is there any hook, to check any particular product is added to cart or not ?

1条回答
爷的心禁止访问
2楼-- · 2019-09-09 01:59

You can find an item in the cart with the find_product_in_cart() method of WC_Cart. You'll need to generate a $cart_id using generate_cart_id(), in order to do this:

$product_id = 123456;  // Some product id
$product_cart_id = WC()->cart->generate_cart_id( $product_id );

// Returns an empty string, if the cart item is not found
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );

if ( $in_cart ) {
    // Do something if the product is in the cart
} else {
    // Do something if the product is not in the cart
}
查看更多
登录 后发表回答