1
votes

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 );
}
1

1 Answers

2
votes

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