• Resolved aldenzuck

    (@aldenzuck)


    Hello, i’m trying to make my (internal) woocommerce product pages open in a new tab when clicking on the image or title on the shop page.

    I’ve tried the PHP directly in my theme files before, which worked initially, but I’ve noticed that it doesn’t work anymore. I tried to remove it from my direct PHP theme file and add it as a snippet, however I got the error “Could not create snippet. Request failed with status code 403”

    remove_action( 'woocommerce_before_shop_loop_item','woocommerce_template_loop_product_link_open', 10 );
    add_action ( 'woocommerce_before_shop_loop_item', 'ami_function_open_new_tab', 10 );
    function ami_function_open_new_tab() {
    echo '';
    }
    • This topic was modified 1 year, 3 months ago by aldenzuck.
    • This topic was modified 1 year, 3 months ago by aldenzuck.
Viewing 11 replies - 1 through 11 (of 11 total)
  •   remove_action( 'woocommerce_before_shop_loop_item','woocommerce_template_loop_product_link_open', 10 );
      add_action ( 'woocommerce_before_shop_loop_item', 'ami_function_open_new_tab', 10 );
      function ami_function_open_new_tab() {
        global $product;
        $link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
        echo '<a href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link" target="_blank">';
      }
    Thread Starter aldenzuck

    (@aldenzuck)

    Thanks @lorro, however this is the error i get when i try to add a PHP snippet with that:

    Snippet automatically deactivated due to an error on line 4:

    Cannot redeclare function ami_function_open_new_tab.

    This means you have left in one of your earlier tries. Make sure the function doesn’t exist is any plugin files (these should not in case be edited), in functions.php or in any other snippets.

    Thread Starter aldenzuck

    (@aldenzuck)

    Submitted but doesn’t work. Thank you, I removed the entry in my php file and re-added the one you sent here into the php snippet. However the product page still does opens in the same tab

    • This reply was modified 1 year, 3 months ago by aldenzuck.

    Is the site the one in your profile? The shop links there do have target=”_blank” in the <a tags so the snippet is working. Are you using the Astra theme? I tried the snippet with Astra and the links do open in a new tab on my site. When I disable Javascript in my browser, the links in the site in your profile do open in a new tab.

    So it looks like one of your other plugins is using Javascript to prevent new tabs. Temporarily deactivate all plugins except WooCommerce and try again. Hopefully your products will now open in a new tab. Reactivate your other plugins one-by-one and test to try to identify which plugin you are using which is breaking the new tab function, then see if it has a setting which controls this.

    I’m making assumptions along the way. Please correct me if any are incorrect.

    Thread Starter aldenzuck

    (@aldenzuck)

    THANK you for such a detailed look into this issue.
    I am using Astra theme as well and yes that is the correct website1

    Turns out it was the “Head, Footer and Post Injections” plugin (which houses my Google tag and hotjar code only).

    I tried using “Header Footer Code Manager” plugin instead and it also disabled the open in a new tab feature.
    I tried inserting the Google tag in my current snippets plugin and got an error: Could not create snippet. Request failed with status code 403
    I made sure to deactivate the other header/footer plugins before trying to add that snippet.
    I also tried to remove the hotjar code and that also wasn’t it

    • This reply was modified 1 year, 3 months ago by aldenzuck.

    Hi,

    The error message you’re encountering (“Could not create snippet. Request failed with status code 403”) indicates that you don’t have the necessary permissions to create a snippet. This could be due to the settings of the plugin or theme you are using to manage snippets.

    If you’re unable to create a snippet directly, you might consider adding the code via a child theme’s functions.php file. Here’s how you can do it:

    1. Child Theme Setup (if not already done):
      Set up a child theme if you haven’t already. This is a safer way to make customizations without affecting the parent theme’s files.
    2. Locate or Create functions.php:
      Inside your child theme’s folder, locate the functions.php file. If it doesn’t exist, you can create one.
    3. Add the Code:
      Open the functions.php file in a code editor and add the following code:
       // Remove the default action that opens the product link
       remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
    
       // Add custom action to open the product link in a new tab
       add_action( 'woocommerce_before_shop_loop_item', 'custom_function_open_new_tab', 10 );
    
       function custom_function_open_new_tab() {
           global $product;
           echo '<a href="' . get_permalink( $product->get_id() ) . '" target="_blank">';
       }

    This code removes the default action that opens the product link and replaces it with a custom action that opens the link in a new tab.

    1. Save and Test:
      Save the functions.php file and then check your shop page to see if the product links are opening in a new tab when clicking on the image or title.

    Remember that the exact implementation might depend on your theme and WooCommerce version, and it’s always a good idea to have a backup of your website before making any changes to the code. If you’re not comfortable with code modifications, consider reaching out to a developer for assistance.

    Thread Starter aldenzuck

    (@aldenzuck)

    Thank you @owadud655 , if you don’t mind reading the whole thread to get the full context of the situation

    • This reply was modified 1 year, 3 months ago by aldenzuck.

    I loaded the “Head, Footer and Post Injections” plugin to my Astra / WooCommerce dev site, with the new tab snippet. The products on the shop page open in a new tab as expected.

    It follows that its either the Google Tag code or the Hotjar code that is suppressing the opening of products in new tabs. To confirm, temporarily remove all settings from the “Head, Footer and Post Injections” plugin but leave the plugin activated. I would expect the products will now open in new tabs? Then put the Google tag code in “Head, Footer and Post Injections” and test. If the products no longer open in a new tab, you’ll know its the Google tag code that’s blocking the new tabs. So looking for a new plugin to load Google tag, or putting it in snippets or functions.php isn’t going to make any difference.

    I don’t have any Google tag code to look at. Its not supported on this forum. Try going back to your Google account and regenerate your Google tag code, this time looking for any setting that might be suppressing new tabs.

    If not Google tags, do the same thing with the hotjar code. The way forward is one at a time.

    Thread Starter aldenzuck

    (@aldenzuck)

    Thanks @lorro turns out it’s the Google Tag specifically that’s causing it. Which is pretty strange, as you can’t refresh that or change that ability to my knowledge – I will post in the google tag forums and see if anyone has experience with that.

    I really appreciate your help in identifying the culprit!

    Thread Starter aldenzuck

    (@aldenzuck)

    FYI just to update this thread, still haven’t found a way to use Google Tag AND have the product pages open in a new tab, which really doesn’t make sense. If anyone has any recommendations or things to try, would love to hear it!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Make Product Pages Open In A New Tab’ is closed to new replies.