Simple Machines Community Forum

Customizing SMF => Modifications and Packages => Mod Requests => Topic started by: dcr7 on March 13, 2021, 06:52:53 PM

Title: Amazon Affiliate Mods?
Post by: dcr7 on March 13, 2021, 06:52:53 PM
Are there any mods for, or is anyone working on a mod for, Amazon affiliate links?  Preferably one that will work with SMF 2.1.  All I've found are older ones.

Something like this:
https://custom.simplemachines.org/mods/index.php?mod=893

Note on the page says the mod was taken over by another user, but the link goes to an error page.
Title: Re: Amazon Affiliate Mods?
Post by: Kindred on March 13, 2021, 10:42:34 PM
That mod was for 1.1.x and never updated to 2.0.x
Title: Re: Amazon Affiliate Mods?
Post by: shadav on March 14, 2021, 04:09:23 AM
there's nothing for 2.1, there are 3 mods for 2.x that may or may not be updated when 2.1 is finalized
https://custom.simplemachines.org/mods/index.php?mod=1540
https://custom.simplemachines.org/mods/index.php?mod=1553
https://custom.simplemachines.org/mods/index.php?mod=1541

all 3 mods allow you to use your amazon affiliate link

if you are wanting to post like the links you get off of amazon into a post what I did was used
Personalized BBC (https://custom.simplemachines.org/mods/index.php?mod=3864) (which says it works for 2.1 rc 2)
created a bbcode
<iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="{content}">
    </iframe>

and then on the code that amazon gives you remove everything before the src="
and only keep the part between the src=" and then the next "
paste that into your post and wrap it with whatever tags you used, I just used aa

so in the end it should look something like
[aa]//ws-na.amazon-adsystem.com/widgets/q?ServiceVersion=SOME NUMBERS AND MUMBO JUMBO HERE and your affiliate link&marketplace=amazon&region=SOME MORE MUMBO JUMBO HERE...all the way down til the part that says price_color=333333&title_color=c44e19&bg_color=ffffff[/aa]

I'm sure there's a better way, but I wasn't sure how amazon does all of it's links so this way there shouldn't be an issue since you're using the entire long link of theirs
Title: Re: Amazon Affiliate Mods?
Post by: szinski on March 23, 2024, 02:11:55 PM
I solved this issue by using some custom code that I wrote.

Edit Sources/Subs.php

Search for this:

        // Cleanup whitespace.
        $message = strtr($message, array('  ' => ' &nbsp;', "\r" => '', "\n" => '<br />', '<br /> ' => '<br />&nbsp;', '&#13;' => "\n"));

And insert this below it:
        // Fix Amazon links (SCZ)
        if (preg_match_all('/<a href="([^"]+)/i', $message, $links))
        {
                // SKIP IF NO LINKS
                if (!empty($links))
                {
                        // REMOVE DUPLICATES/EMPTIES
                        $links = array_unique($links[1]);

                        // LOOP THROUGH EACH LINK
                        foreach($links as $link)
                        {
                                $parsed = parse_url($link);
                                if (isset($parsed['host']))
                                {
                                        // MATCH AMAZON.COM AND AMZN.COM LINKS
                                        if (preg_match('/(.*)(amazon|amzn)\.(com|co.uk|de|fr|co.jp|ca|cn|it|es|in)+/i', $parsed['host']))
                                        {
                                                $my = preg_match('/(\/(dp|gp\/product|exec\/obidos\/(ASIN|tg\/detail\/-))\/\w{10}\b)/i', $parsed['path'], $amazon_link);
                                                if (isset($amazon_link[1]))
                                                {
                                                        $product_link = $amazon_link[1];
                                                }
                                                else
                                                {
                                                        $my = preg_match('/^(\/\w{10}\b)/i', $parsed['path'], $amzn_link);
                                                        if (isset($amzn_link[1]))
                                                        {
                                                                $product_link = $amzn_link[1];
                                                        }
                                                }

                                                if (isset($product_link))
                                                {
                                                        $replace = 'https://' . $parsed['host'] . $product_link . '/?tag=myaffiliate-20';
                                                        $message = str_replace($link, $replace, $message);
                                                }
                                        }
                                }
                                unset($parsed, $product_link);
                        }
                }
        }

Don't forget to change "myaffiliate-20" to your own affiliate ID number.

I've tested this with both SMF 2.0 and SMF 2.1 and it works.

FYI, this code doesn't touch the underlying message as saved to the database, it converts Amazon links into shortened links with your affiliate ID appended to the end ON THE FLY (as the message is being displayed). If the OP edits the message, it will appear exactly as he typed it... the transformation happens when the message is displayed. Pretty clever if I don't say so myself. :)

Enjoy!