WooCommerce: Check if items are already in cart

后端 未结 4 1869
无人共我
无人共我 2020-12-09 00:07

I found this great snippet from this website

The following is the function to check if a specific product exists in cart:

        function woo_in_ca         


        
4条回答
  •  没有蜡笔的小新
    2020-12-09 00:19

    Maybe something simpler, first we get product ids in cart :

    $product_ids = array_merge(
      wp_list_pluck(WC()->cart->get_cart_contents(), 'variation_id'),
      wp_list_pluck(WC()->cart->get_cart_contents(), 'product_id')
    );
    

    Now if you want to check one product just simply use in_array function :

    in_array(123, $product_ids);
    

    and for more than one product:

    array_intersect([123, 345, 567], $product_ids);
    

提交回复
热议问题