Hide Add To Cart Button in Single Product Page if Product already in cart

This topic has 4 replies, 2 voices, and was last updated 9 months, 1 weeks ago ago by Luca Rossi

  • Avatar: jojoexpress
    jojoexpress
    Participant
    February 9, 2024 at 07:48

    Hi Team,

    Can you guide me how to hide the “add to cart” button in single product page, if product already in cart ?

    I already hide “add to cart” button in shop page using this filter “woocommerce_loop_add_to_cart_link”, which i add extra class into a link tag if product already in cart, and it already suceeded, but in single product page, it still appear.

    3 Answers
    Avatar: Justin
    Luca Rossi
    Support staff
    February 9, 2024 at 09:25

    Dear @jojoexpress,

    We kindly request that you insert the following code snippet into the functions.php file located within your child theme directory:

    
    function check_product_already_in_cart() {
    	if ( is_product() ) {
    		global $woocommerce;
    		$product_id = get_the_ID();
    		foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    			$_product = $values['data'];
    			if ( $_product->get_id() == $product_id ) {
                    ?>
                    <style>.single-product form.cart{display: none !important;}</style>
                    <?php
    				break;
    			}
    		}
    	}
    }
    add_action( 'wp_footer', 'check_product_already_in_cart', 999 );
    

    We trust this code will be beneficial for your needs. Should you require any further assistance, please do not hesitate to reach out.

    Warm regards,
    The 8Theme Team

    Avatar: jojoexpress
    jojoexpress
    Participant
    February 9, 2024 at 12:35

    Wow, thank you for the fast response, the code work like a charm.

    Btw, i have additional question if you don’t mind, how if the button not “Hide” but change into just text “Already In Cart”, without a link button.

    can we do that too ?

    Avatar: Justin
    Luca Rossi
    Support staff
    February 9, 2024 at 13:12

    Dear @jojoexpress,

    We kindly suggest utilizing the following code as an alternative solution:

    
    function check_product_already_in_cart() {
    	if ( is_product() ) {
    		global $woocommerce;
    		$product_id = get_the_ID();
    		foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    			$_product = $values['data'];
    			if ( $_product->get_id() == $product_id ) {
    				?>
                    <script>
                        jQuery(document).ready(function($){
                            $('.single-product form.cart').find('button.single_add_to_cart_button').text('Already In Cart').prop('disabled', true);
                        });
                    </script>
    				<?php
    				break;
    			}
    		}
    	}
    }
    add_action( 'wp_footer', 'check_product_already_in_cart', 999 );
    

    Please note that any further customization falls outside the parameters of the support we are able to provide. However, if you wish our developers team can personally assist you via our customization service (paid). To get started, please follow this link to send us your requirements – https://www.8theme.com/account/#etheme_customization_panel , and we will prepare a detailed estimate for the customization work. Our goal is to create a design that aligns with your vision and enhances your website’s appearance and functionality.
    Customize your website to suit your needs and make it stand out with our expert dev team.

    We appreciate your understanding in this matter.

    Warm regards,
    The 8Theme Team

  • Viewing 4 results - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.Log in/Sign up

8theme customization service
We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.