Is there any hook, to check any particular product is added to cart or not ?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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
}