WooCommerce add_to_cart Shortcode - Product already in Cart

19,000

Try this,

When the users clicks on the button you can check the current cart item with this code.

global $woocommerce;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {

  if($cart_item['product_id'] == $your_product_id){
   //the item already added to the cart
 }
} 

This will iterate the all items in the cart also if you want to add a product forcefully to the cart just need this,

$woocommerce->cart->add_to_cart($product_id,$qty);

Hope its helps..

Share:
19,000

Related videos on Youtube

user1493044
Author by

user1493044

Updated on June 04, 2022

Comments

  • user1493044
    user1493044 almost 2 years

    I've added the WooCommerce shortcode "add_to_cart" successfully on a custom post type page.

    [add_to_cart id="510"]

    If there is nothing in the cart, it redirects to the shopping cart adding the product as intended. However, if the product is already in the cart and the user click the button on the custom post type page, it redirects back to itself.

    The user can't add more than one of a given item to the cart. How can I check to see what's in the cart, so that if the user clicks the button it will redirect to the shopping cart if that product is already in there? Don't need any error message to come up like on the standard WooCommerce product page.

    Thanks in advance for any ideas!

    Aaron