WooCommerce - 如何检查特定产品是否在购物车中。任何钩子?

Kul*_*hli 0 wordpress woocommerce

是否有任何钩子可以检查是否将任何特定产品添加到购物车?

rne*_*ius 6

您可以使用 的find_product_in_cart()方法在购物车中查找商品WC_Cart。您需要生成一个$cart_idusing generate_cart_id(),以便执行此操作:

$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
}
Run Code Online (Sandbox Code Playgroud)