Cray
Cray

Reputation: 5503

WooCommerce: Remove add to cart buttons on archives but not in cart

I want to remove the "Add to cart" Button on every page except the cart itself.

I found a solution to remove the cart button with an hook:

remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );

Works fine but that removes the button everywhere.

I tried this code to remove the button everywhere but the cart:

if (!is_cart()) :
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
endif;

But that doesn't seem to work.

Is there any other way without touching a template file?

Upvotes: 0

Views: 779

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254378

The following will do the trick (based on woocommerce_template_loop_add_to_cart() original function code):

add_action('init', 'remove_add_to_cart_function_callback' );
function remove_add_to_cart_function_callback() {
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    add_action( 'woocommerce_after_shop_loop_item', 'custom_template_loop_add_to_cart', 10 );
}

if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {

    function custom_template_loop_add_to_cart( $args = array() ) {
        global $product;

        if ( $product && is_cart() ) {
            $defaults = array(
                'quantity'   => 1,
                'class'      => implode(
                    ' ',
                    array_filter(
                        array(
                            'button',
                            'product_type_' . $product->get_type(),
                            $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
                            $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
                        )
                    )
                ),
                'attributes' => array(
                    'data-product_id'  => $product->get_id(),
                    'data-product_sku' => $product->get_sku(),
                    'aria-label'       => $product->add_to_cart_description(),
                    'rel'              => 'nofollow',
                ),
            );

            $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );

            if ( isset( $args['attributes']['aria-label'] ) ) {
                $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
            }

            wc_get_template( 'loop/add-to-cart.php', $args );
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Related: Remove "Add To Card" only Home page in WooCommerce

Upvotes: 1

Howard E
Howard E

Reputation: 5669

Did you try if isn't cart?

if (!is_cart()) :
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
endif;

Upvotes: 1

Related Questions