Adding a 'in cart' tag in woocommerce

Viewed 51

I am trying to add a tag that the product is in the cart after adding a product in WooCommerce, but it only shows this after refreshing the page. I would like it to me shown right after clicking Add to Cart.

add_action( 'woocommerce_after_shop_loop_item_title', 'display_in_cart_loop_woocommerce' );

function display_in_cart_loop_woocommerce() {
    global $product;
   if ( $product->get_type() == 'simple' && $product->is_purchasable()) {
         foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
         $_product = $values['data'];
           if( get_the_ID() == $_product->get_id() ) { 
       echo '<span class="insal">In Cart!</span>';
         }else {
      echo '<span class="notincart"> &nbsp;</span>';
         }
}
}
}

Should I use another hook?

Your help will very much appreciated. Thank you!

0 Answers
Related