Home Remove "add to cart" button without removing Woocommerce Product Add-ons
Reply: 1

Remove "add to cart" button without removing Woocommerce Product Add-ons

Luke Baumann
1#
Luke Baumann Published in 2017-12-07 17:42:35Z

I am usig WooCommerce with WooCommerce product add-ons plugin. I have seen many answers that remove the "add to cart" button on single product pages. They also remove WooCommerce product add-ons as well.

Can anyone provide insight how to add back "add-ons" while removing "add to cart" button?

Here is what I have to remove the "add to cart" button but removes add-ons as well:

add_action( 'woocommerce_single_product_summary', 'remove_add_to_cart_buttons', 1 );

function remove_add_to_cart_buttons() {
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
LoicTheAztec
2#
LoicTheAztec Reply to 2017-12-08 01:48:13Z

WooCommerce product Add-Ons needs woocommerce_before_add_to_cart_button action hook to display all Add-ons fields and data.

So in the code below will work for simple and variable products, just removing the add-to-cart and quantity fields, allowing product Add-ons fields and data to be displayed.

add_action( 'woocommerce_single_product_summary', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
    global $product;

    // For simple product types
    if( $product->is_type( 'simple' ) ) {
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        add_action( 'woocommerce_single_product_summary', 'custom_before_single_add_to_cart', 30 );
    }
    // For variable product types (keeping attribute select fields)
    elseif( $product->is_type( 'variable' ) ) {
        remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
    }
}
function custom_before_single_add_to_cart(){
    global $product;

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

    // Simple Products
    if ( $product->is_in_stock() ) {
        do_action( 'woocommerce_before_add_to_cart_form' ); // (Optional)

        echo '<form class="cart" method="post" enctype="multipart/form-data">'; // (Optiona

            ## @since 2.1.0.
            do_action( 'woocommerce_before_add_to_cart_button' ); // <== NEEDED by Add-ons

            ## @since 3.0.0.
            do_action( 'woocommerce_before_add_to_cart_quantity' ); // Optional

            ## @since 3.0.0.
            do_action( 'woocommerce_after_add_to_cart_quantity' ); // Optional

            ## @since 2.1.0.
            do_action( 'woocommerce_after_add_to_cart_button' ); // Optional

        echo '</form>'; // Optional

        do_action( 'woocommerce_after_add_to_cart_form' ); // Optional
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.

As the add to cart button is removed, you will not be able to submit the Add-ons data

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302914 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO