• Resolved banacan99

    (@banacan99)


    We sell only some products online, others will never be purchasable, so I need to remove the add to cart button and quantities on single product pages for certain subcategories. I’ve got my code working properly but when I:

    remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );

    it hides all variation information. How can I remove just the add to cart button and the quantity picker without removing all of the variation information that follows?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @banacan99

    Thanks for reaching out!

    I understand that you want to hide the Add to Cart button as well as the quantities of some of your products on the single product pages.

    What you want to achieve is a bit complex and would require customization to do it. Since custom coding is outside our scope of support, I am leaving this thread open for a bit to see if anyone can chime in to help you out.

    We have our WooCommerce Developer Resources Portal that can help you get going.

    You can also visit the WooCommerce Community Forum, the WooCommerce FB group, or the #developers channel of our Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, too.

    Another option is to check our customizations page to hire an expert that can create a custom solution for you: https://woocommerce.com/experts/

    Thread Starter banacan99

    (@banacan99)

    Well I found the answer in another, old post. The action I need is:

    remove_action( 'woocommerce_single_variation','woocommerce_single_variation_add_to_cart_button', 20 );

    This removes only the add to cart button and quantity.

    Plugin Support anastas10s (woo-hc)

    (@anastas10s)

    Hi there @banacan99 👋

    I found the answer in another, old post.

    This removes only the add to cart button and quantity.

    That’s great to read! Thanks for letting us know and posting it here.

    Thread Starter banacan99

    (@banacan99)

    As it turns out, while that worked as far as removing the quantity and add to cart button, I later discovered that it caused issues with my plugin WooThumbs. My final solution was adding a body class programmatically that I could use to apply display:none to hide these elements. I also made the select products unpurchasable:

    add_filter( 'woocommerce_is_purchasable', '__return_false');
     add_filter( 'body_class', function( $classes ) {
     return array_merge( $classes, array( 'hide-add-to-cart ' ) );

    Hi,

    Glad to hear that you figured this out. If you would like to share your experience with the community, you can leave a review here.

    Feel free to create a new thread if you have any other questions.

    Thread Starter banacan99

    (@banacan99)

    Sure, I’m happy to share my experience and solution.

    As I said earlier, because only some products are actually sold online, I needed to figure out a way to remove the quantity and add to cart button from a subset of products.

    The products are all categorized, and each category has sub-categories. So I needed to check all single product categories to see if its sub-categories are children of categories that are not sold online.

    I searched and searched for solutions based on sub-categories, but all of those solutions required manually creating an array of sub-categories and I needed a solution that would query the db for all current sub-categories because additional sub-categories would be created as time went on.

    This is what I came up with:

    add_action('wp', 'niba_remove_add_to_cart_from_category' );
     // allow querying of the WP database 
     global $wpdb;
     // Find all children of Rug category parents 19 & 20 (collection & style)
       $catproducts = $wpdb->get_results("
       SELECT t.slug, tt.term_id
       FROM wp_terms AS t
       INNER JOIN wp_term_taxonomy AS tt
       ON t.term_id = tt.term_id
       WHERE tt.parent IN (19)
       OR tt.parent IN (20)
       ", OBJECT );
       // create an array of category slugs of the children
       $catnames = array();
       foreach( $catproducts as $catproduct ) :
         $catnames[] = $catproduct->slug;
       endforeach;
    
     function niba_remove_add_to_cart_from_category() {
     // only do this if this is a product page
       if ( is_product() ) {
     // make the current product and the array of children available to this 
     // function     
      global $catnames;
      global $product;
    	// Get the categories of the current product
    	$terms = get_the_terms( get_the_ID(), 'product_cat' );
    	// if the product has categories create an array of those categories					
    	if ( $terms && ! is_wp_error( $terms ) ) : 
    		// make array of the product's categories
    		$prodcats = array();
    		foreach ( $terms as $term ) {
    			// make the array from the slugs
    			$prodcats[] = $term->slug;
    		}		
    	endif;
    	 // search for a match in the two arrays: $prodcats and $catnames
    	 // to determine if this is a rug collection or style
    	 $arr_common = array_intersect( $catnames, $prodcats );
    	 
    	 // check if $arr_common has any values
    	 // if it does then don't show the quantities or add to cart button and make this product umpurchasable
    	 if ( $arr_common ) {
         add_filter( 'woocommerce_is_purchasable', '__return_false');
         add_filter( 'body_class', function( $classes ) {
             return array_merge( $classes, array( 'hide-add-to-cart ' ) );
         } );
        }
      }
    }

    Here’s my associated css:
    body.hide-add-to-cart .woocommerce-variation-add-to-cart.variations_button { display:none !important; }

    Any suggestions for how this could be improved would be most welcome. I muddled through as best I could. Anyway, this code accomplishes what I needed.

    I hope this helps others seeking a similar solution.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Hide add to cart and quantity NOT variation info’ is closed to new replies.