php - How to hide / remove add to cart button in Woocommerce after adding the product to the cart

Good day,
Before that, I searched here for a solution to the query but failed.
I need to hide / remove add to cart button in Woocommerce after adding the product to the cart, without affecting prices, quantities and product types (simple, variables, grouped, etc...). In General the idea is add checkout form in single product page.
You can visit the site to test from HERE
I use the following theme and plugins:

  • Free version of Astra Theme
  • Snippets code Plugin
  • Custom CSS and JavaScript Plugin
  • Microthemer Plugin

The following changes have been made by me:

1 - Redirect Add to cart in shop page
I changed the text of "add to cart" button to "view product" and redirected this button to the single product page, and this is the used code

add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product  ) {
    $button_text = __("View product", "woocommerce");
    $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';

    return $button;
}

2 - Change add to cart text in single product page
I changed the text "Add to cart" to "Buy now" in the single product page and this is the used code

// Code Start
// To change add to cart text on single product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' ); 
function woocommerce_custom_single_add_to_cart_text() {
    return __( 'Buy Now', 'woocommerce' );  // Replace "Buy Now" text with your own text
}

// To change add to cart text on product archives(Collection) page
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );  
function woocommerce_custom_product_add_to_cart_text() {
    return __( 'Buy Now', 'woocommerce' );  // Replace "By Now" text with your own text
}
// Code End

3 - Short description of woocommerce by default
I have added the checkout page to the product page after pressing the buy now button, and this is the used code

add_filter('woocommerce_short_description','ts_add_text_short_descr');

function ts_add_text_short_descr($description){
  $text="[woocommerce_checkout]";
    
  return $description.$text;
}

4 - Empty cart if the user reaches the home page (shop page in my case)
I added a code to empty the cart when going to the home page (in my case the home page is the shop page) and this is the used code

/*empty cart if user come to homepage*/
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;

if ($_SERVER['REQUEST_URI'] === '/') { 
    $woocommerce->cart->empty_cart(); 
 }
}

Now what I want to do is that after pressing the buy now button (previously, add to cart) and the checkout page form fields appear, the buy now button is hidden or deleted (previously, add to cart) without affecting the product types or any thing else.

I hope to find help from you or any recommended suggestions. best greetings

Answer

Solution:

Ok, there are multiple ways to do it. first, you have to check whether the product is in the cart or not. and to do that you can loop cart items and compare them with product id. Create a function like this.

function is_product_in_cart( $product_id ) {

    $in_cart = false;
  
    foreach( WC()->cart->get_cart() as $cart_item ) {
        $product_in_cart = $cart_item['product_id'];
        if ( $product_in_cart === $product_id ) $in_cart = true;
    }
  
    return $in_cart;
  
}

SO the first way to do it is you have to override woocommerce template in your active theme. and in order to do that copy file from here

wp-content\plugins\woocommerce\templates\single-product\add-to-cart.php

and paste here

wp-content\themes\your-active-theme-name\woocommerce\single-product\add-to-cart.php

You will see code in add-to-cart.php like below.

<?php
/**
 * Simple product add to cart
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/add-to-cart/simple.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files, and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 3.4.0
 */

defined( 'ABSPATH' ) || exit;

global $product;

if ( ! $product->is_purchasable() ) {
    return;
}

echo wc_get_stock_html( $product ); // WPCS: XSS ok.

if ( $product->is_in_stock() ) : ?>

    <?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>

    <form class="cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data'>
        <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

        <?php
        do_action( 'woocommerce_before_add_to_cart_quantity' );

        woocommerce_quantity_input(
            array(
                'min_value'   => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
                'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
                'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
            )
        );

        do_action( 'woocommerce_after_add_to_cart_quantity' );
        ?>

        <button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>

        <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
    </form>

    <?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>

<?php endif; ?>

Now modify code with function is_product_in_cart like below

<?php
/**
 * Simple product add to cart
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/add-to-cart/simple.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files, and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 3.4.0
 */

defined( 'ABSPATH' ) || exit;

global $product;

if ( ! $product->is_purchasable() ) {
    return;
}

echo wc_get_stock_html( $product ); // WPCS: XSS ok.

if ( $product->is_in_stock() ) : ?>

    <?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>

    <form class="cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data'>
        <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

        <?php
        do_action( 'woocommerce_before_add_to_cart_quantity' );

        woocommerce_quantity_input(
            array(
                'min_value'   => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
                'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
                'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
            )
        );

        do_action( 'woocommerce_after_add_to_cart_quantity' );
        if( !is_product_in_cart( $product->get_id() ) ){ ?>
        <button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
        <?php } do_action( 'woocommerce_after_add_to_cart_button' ); ?>
    </form>

    <?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>

<?php endif; ?>

and that's it now once you have added a product into the cart add to cart button will hide.

You have to follow for same for the other type like variable, grouped, and external types. You will find all these files in same folder that I mentioned above and you have to follow the same steps as above.

Now is the second way.

On page load, you can hide the button with CSS and use the same function. check the below code. this will code go in your active theme functions.php file.

function hide_add_to_cart_button(){
    if( is_product() ){
        if( is_product_in_cart( get_the_ID() ) ){
            ?>
            <style type="text/css">
                .single_add_to_cart_button{display: none;}
            </style>
            <?php
        }
    }
}
add_action('wp_head','hide_add_to_cart_button');

Source