• Hi there,

    I’m wondering if anyone could help me with a small modification to Woocommerce. I’m hoping to display an Add to Basket button with products when seen only in product category pages.

    So far I have tried adding:

    add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 20 );

    to the functions.php file. This does add the button, see this page, BUT it also shows it on the main shop page, see this page.

    To target product category pages only, I’ve tried:

    if ( is_product_category() ){
    add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 20 );
    }

    but this stopped the button showing up on ALL pages.

    I’m obviously doing something a bit wrong, but please could you point me in the right direction?

    Many thanks in advance,

    Mark

    https://www.ads-software.com/plugins/woocommerce/

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    I’m not following why this is needed – wc shows these buttons by default. See demo.woothemes.com

    Thread Starter SYPOMark

    (@sypomark)

    Hi Mike,

    Thanks for your reply. I am always impressed when the author of a plugin/app takes the time to interact on these forums.

    Unfortunately, that is not what is happening. With the mysite theme I am using, the add to basket buttons do not show up by default. I have used a css workaround – bit clumsy – but I was wondering if there was a better way.

    With kind regards,

    Mark

    Plugin Contributor James Koster

    (@jameskoster)

    Hey,

    Try sticking this in your child themes functions.php file.

    add_action( 'init', 'jk_shop_archive_add_to_cart_button' );
    function jk_shop_archive_add_to_cart_button() {
    add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 11 );
    }

    That should do the trick.

    Thread Starter SYPOMark

    (@sypomark)

    Hi James,

    Thanks for your extremely rapid response.

    That added the ‘Add to Basket’ button to every page as well. They’re only VISIBLE on the product category pages, but that’s only because of CSS.

    I’m looking for a solution that’ll add it exclusively to product category pages WITHOUT having to hide the m in CSS. I know there must be a way! My lack of knowledge is frustrating me!!!

    With kind regards,

    Mark

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    You can add conditonals to the function James provided.

    https://codex.www.ads-software.com/Conditional_Tags

    Thread Starter SYPOMark

    (@sypomark)

    Hi Mike,

    Thanks for coming back to me again.

    I’m confused though: I thought I’d already tried that, if you see my original post:

    if ( is_product_category() ){
    add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 20 );
    }

    What was wrong with this particular conditional tag?

    With kind regards,

    Mark

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    I think its too early. Put it inside the function James gave instead, rather than wrapping the entire thing.

    Thread Starter SYPOMark

    (@sypomark)

    Hello again.

    I think I’m going to give up on this one. Thank-you both very much for your help. I have placed conditional tags in the function as you suggested, Mike, but it stops the button appearing at all, as it did to start with.

    I even pasted in a variation on the code found here and that didn’t work either, like so:

    if ( is_product_category() ) {
    
      if ( is_product_category(array('Aerosol Testing', 'Microscopy', 'Mixing Media', 'Sample Preparation', 'Spare Parts', 'Weighing' ) ) ) {
        echo 'Testing';
      } else {
        echo 'Rhubarb';
      }
    
    }

    This is not the first thing that seems to not be working for me with the Mysite theme installed. I don’t know what is going on with it, though I expect it is probably down to user error!

    I’ll just have to go back to using the code without placing it in conditional tags and hide the instances I don’t want with CSS. Not the most elegant solution, but at least the user’ll be none the wiser.

    Thanks both again.

    With kind regards,

    Mark

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Did this not work?

    add_action( 'init', 'jk_shop_archive_add_to_cart_button' );
    function jk_shop_archive_add_to_cart_button() {
    if ( is_product_category() ) {
        add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 11 );
    }
    }
    Thread Starter SYPOMark

    (@sypomark)

    Hi Mike,

    Thanks for your message.

    No, sadly, that didn’t work. The Add to Basket button didn’t show up on any page of any type using that code.

    With kind regards,

    Mark

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Right but are they **also** hidden with CSS? Not sure. This code should add them to category archives.

    Thread Starter SYPOMark

    (@sypomark)

    Hi Mike,

    Thanks for your message.

    What I’ve got in functions.php at the moment is:

    add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 20 );

    making the Add to Basket button show up everywhere. To keep the button from showing up on anything other than product category pages, I’m using CSS.

    Adding the jk_shop function, with the conditional tag included, just stops the buttons showing up at all.

    With kind regards,

    Mark

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Hmm not sure then without debugging myself. At least you found an alternative.

    hi
    i have this problem
    how do you fix it?
    thanks

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Adding add to cart button to product category pages’ is closed to new replies.