External link in shop should open in new tab

Hello,
in my shop I have many affiliate items which I pasted in under external link the affiliate link. Unfortunately when a users clicks buy, the link will open in the same window and not in a tab.
How can this be achieved, that, the external of the product will open in a new tab?

Thanks in advance and regards,
Roger

    Nastia

    Hello Vaishnava , I trust you are doing well today!

    This is not possible out of the box with the MarketPress plugin. It is possible by adding some custom code.

    I am not a code expert so I have flagged a Second Level support so they could provide a feedback. please note that out developer's response is slower than the usual staff, so we appreciate your patience

    Kind regards,
    Nastia

    Jude

    Hey Vaishnava

    Hope all is well with you ! Sorry about the delay here

    Here is some code you can use to do this

    add_action('wp_footer', 'custom_JS') ; 
    
    function custom_JS() {
    echo <<<'JS'
    <script type="text/javascript">
    $(document).ready(function(){
    
      $( '.mp_link_buynow' ).click(function(){
        window.open(this.href);
        return false;
      });
    });
    
    </script>
    JS;
    }

    Insert that into either your theme's functions.php, or you can insert it as a mu-plugin.

    To create an mu-plugin, go to your /wp-content/ folder via FTP, and find the folder called "mu-plugins". If there is no folder with that name, then you'll want to create one. Then, create a file inside that folder, and give the file any name you like (making sure the file as an extension of .php, e.g. example.php) and paste the code in there.

    You don't need to activate that plugin, as it will be always be automatically activated. Please be sure to add a PHP start tag at the beginning of the code if you insert this code as a mu-plugin, like this:

    <?php

    Thank you for being a WPMU DEV member and have a fantastic day!

    Cheers
    Jude

    Mahlamusa

    Hello Vaishnava,

    I hope you are doing great today. This is a follow up to see if you had success using Jude's suggestion above. I will be delighted to know the code worked for you but if not, I will be more than happy to help you further.

    I also noticed that from the code suggested by jude, you should change 'mp_link_buynow' in to 'mp_link-buynow' that is new CSS class for the link now if it was like that before, so your final code should look like the following:

    add_action('wp_footer', 'custom_JS') ; 
    
    function custom_JS() {
    echo <<<'JS'
    <script type="text/javascript">
    $(document).ready(function(){
    
      $( '.mp_link-buynow' ).click(function(){
        window.open(this.href);
        return false;
      });
    });
    
    </script>
    JS;
    }

    I have tested this code on my install and it works as expected. If you need help implementing this on your website please let us know.

    Please enjoy your day further.

    Cheers,
    Mahlamusa