• Resolved wertykid

    (@wertykid)


    Hello,

    I have added a snippet using this plugin, the only issue is, it runs throughout the entire website. Is there a way for me to use the snippets in specific Woocommerce product pages only?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hey,

    You can make a snippet run only on all WooCommerce product pages by adding this code to the beginning of the snippet code:

    if ( ! is_singular( [ 'product' ] ) ) return;

    Choosing which products to run code on is a little trickier, but still doable:

    $allowed_products = [ 'product-slug', 42, 'Product Name' ];
    
    if ( ! is_singular( [ 'product' ] ) || ! is_single( $allowed_products ) ) return;

    For this one, you need to set $allowed_products to be an array of the product IDs, slugs, or titles for the products you want to run the snippet on.

    Thread Starter wertykid

    (@wertykid)

    Thanks for your help!

    Hi there! What if I want to run the code only on woocommerce product catalogue (shop page) pages?

    To clarify, I’ve tried “is_shop()” and “is_product_category()”, but neither of them work.

    • This reply was modified 5 years, 6 months ago by rocketsam.

    use if-statements. I presume that if(is_woocommerce()) might be the correct conditon.

    WARNING. Newer use as condition function for wp_head, which has been defined with a plugin before wp_head function.

    This would crash your site:

    if(is_woocommerce()) {
    add_action( ‘wp_head’, function () { ?>…
    <?php } );}

    This would not crash your site:

    add_action( ‘wp_head’, function () {if(is_woocommerce()) { ?>…
    <?php }} );

    The same issue concern apparently also other core functions as wp_head.

    • This reply was modified 5 years, 6 months ago by tapiohuuhaa.

    Thanks for your response tapio!

    To clarify, I can’t use is_woocommerce because I do want the code to run on woocommerce shop archive/catalog pages. I just don’t want the code to run on the woocommerce product pages. It doesn’t matter if I exclude the product pages or if i only include the shop/catalog pages, these are the only 2 areas that the code affects.

    I simply tried running (seperately) in front of my code:
    – if ( ! is_shop() ) return;
    – if ( ! is_product_category() ) return;
    – if ( is_shop() ) { … }
    – if ( is_product_category() ) { … }

    You can collect them into one statement, where you define, where you want the code to run.

    if(is_woocommerce && (is_product_category() || is_shop() ))

    runs only on woocommerce product category pages

    if you want exclude
    if(is_woocommerce && ( ! is_shop() && !is_… && !is_…))

    You can use just one if-statement. If you have something else with some other pages you can use elseif(is_woocommerce && (…))

    the “if(is_woocommerce && (is_product_category() || is_shop() ))” makes a lot of sense, and I have no idea why its not working for me :/

    i’ll try again tomorrow after some sleep! thanks

    Plugin Author Shea Bunge

    (@bungeshea)

    WooCommerce has an is_product() function that I imagine would fit this purpose.

    Hi all, so with your help i figured out what’s wrong, which i feel very accomplished for ?? (thank you) however i have no idea how to fix it.

    in the end i decided to go with “if((!is_shop)) return;”

    the problem was that i kept clicking on the shop page, confirming that the code worked, and then clicking on the product page, and seeing that the code ran again, which i don’t want. i was VERY confused, since the code was so simple…. i realized that if i clicked on the product page first BEFORE clicking on the shop page, the code wouldn’t run, but as soon as i click on the shop page, the code has run for good. thus, what i was missing was that once the code runs once, it applies to the entire website.

    so what i need is either a way for the code to only apply to the shop page, or for the code to “unrun” once i click on the product page.

    is there any way of doing this? thank you so so much for your help!!

    so, to update this: i dug in a bit more, and realized that the code does “unrun” properly on my product pages that have less than 25 variations (which is less than half my products, which is why i didn’t notice originally).

    so for some reason, for my products with <25 variations, the code still works even though it shouldn’t, and for my products with <25 variations, the code doesn’t work, as expected.

    i’m assuming this is a woocommerce problem, and not a Code Snippets problem? not really sure how to tackle this one haha… please let me know if you have any thoughts!!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Place snippets only on certain Woocommerce product pages’ is closed to new replies.