• Resolved cousinr

    (@cousinr)


    Hi,
    I need to remove the Add to cart button along with the quantity entry field for a certain category of products (e.g. legal services) from everywhere (from all pages). In doing so, leave those elements that add other plugins in the area where the add to cart button is located (such as support and live chat buttons from Dokan).

    I would use this code:

    // Disable products from categories (a, b, c) from being purchased no matter what
    add_filter('woocommerce_is_purchasable', 'set_catalog_mode_on_for_category', 10, 2 );
     
    function set_catalog_mode_on_for_category( $is_purchasable, $product ) {
     
     if( has_term( array( 'a', 'b', 'c' ), 'product_cat', $product->get_id() ) ) {
        return false;
      }
     
      return $is_purchasable;
    }
    
    // Hide 'Add To Cart' for variable products, but keep product variations visible
    function remove_add_to_cart(){
        if( has_term( array( 'a', 'b', 'c' ), 'product_cat', $product->get_id() ) ) {
        
    
           remove_action( 'woocommerce_single_variation','woocommerce_single_variation_add_to_cart_button', 20 );
    }
    } 
    
    add_action('woocommerce_single_variation','remove_add_to_cart');

    but then the other buttons that are added by other plugins to the ‘add to cart’ area disappear along with the button ‘add to cart’.

    Then I thought maybe I could use this code:

    add_action( 'woocommerce_single_product_summary', 'ywp_replace_add_to_cart_btn', 31 );
    function ywp_replace_add_to_cart_btn() {
        if( has_term( array( 'a', 'b', 'c' ), 'product_cat', $product->get_id() ) ) {
        
                    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
                    
    ?>
        <style>div.quantity,.single_add_to_cart_button{display:none}</style>
        <style>div.quantity,.qty{display:none}</style>
    
        <script>jQuery(document).ready(function($){$('div.quantity,.single_add_to_cart_button'),remove()})</script>
        <script>jQuery(document).ready(function($){$('div.quantity,.qty'),remove()})</script>
    <?php
    
    }
    }

    But in this case the cart button disappears only on the product page, while remaining on the archive product page (shop page).

    Could you help refine the last code or provide a better solution.

    I would not like to use plugins that turn the store into a catalog, as they have too much code and then the pages take 1-2 seconds longer to load. Please help with the right solution.

    • This topic was modified 3 years, 3 months ago by cousinr.
    • This topic was modified 3 years, 3 months ago by cousinr.
    • This topic was modified 3 years, 3 months ago by cousinr.
    • This topic was modified 3 years, 3 months ago by cousinr.
    • This topic was modified 3 years, 3 months ago by cousinr.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Hide Add to cart for certain categories’ is closed to new replies.