【问题标题】:WooCommerce - How to check particular product is in cart or not. Any Hook?WooCommerce - 如何检查特定产品是否在购物车中。任何钩子?
【发布时间】:2015-11-05 18:09:56
【问题描述】:

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

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    您可以使用WC_Cartfind_product_in_cart() 方法在购物车中找到商品。您需要使用generate_cart_id() 生成$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
    }
    

    【讨论】:

    猜你喜欢
    • 2018-10-13
    • 2021-03-27
    • 2018-08-22
    • 2017-05-06
    • 1970-01-01
    • 2020-11-15
    • 2018-11-13
    • 2021-05-15
    • 1970-01-01
    相关资源
    最近更新 更多