• Pressnoob

    (@pressnoob)


    So I really want to make a plugin like this: https://www.convertazon.com/demo/ except without the cheesy loading bar and other stuff.

    I’m just wondering if anyone has any experience creating Amazon product APIs and if you have any advice. I’ve never made a WP plugin myself.

    This would go on top of the page:

    <?php
        include("amazon_api_class.php");
    
        $obj = new AmazonProductAPI();
    
        try
        {
            $result = $obj->getItemByAsin(B0067HQL30);
        }
        catch(Exception $e)
        {
            echo $e->getMessage();
        }
    
    function percent($Retail, $Low) {
    $count1 = $Retail - $Low;
    $count2 = $count1 / $Retail;
    $count3 = $count2 * 100;
    $count = number_format($count3, 2) . "%";
    return $count;
    }
    
    $LowestP = $result->Items->Item->OfferSummary->LowestNewPrice->Amount;
    $MSRP = $result->Items->Item->ItemAttributes->ListPrice->Amount;
    $Savings =  $MSRP - $LowestP; 
    
    $LowestP2 = number_format($LowestP /100,2);
    $MSRP2 = number_format($MSRP /100,2);
    
    ?>

    And this goes to the body of the page to style in however you’d like.

    <h1> Percent: <?php echo percent($MSRP, $LowestP); ?> </h1>
    <h2> Retail Price:$ <?php echo $MSRP2; ?> </h2>
    <h1> Lowest Price:$ <?php echo $LowestP2; ?> </h1>
    <h2> Total Savings:$ <?php echo number_format($Savings /100,2); ?> </h2>

    There is only one real dynamic aspect of the plugin, the string of the function getItemByAsin. I guess you’d just retrieve the value from the WYSIWYG editor, is that it?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    There’s a number of approaches, depending on how you plan to use this functionality. One approach would be for your plugin to essentially combine all of that into a shortcode. So when you create a post or page, to display the prices of an item, you type something like: [pn_prices asin='B0067HQL30'], and when the post or page is actually displayed in the browser, the appropriate prices are displayed in place of the shortcode you typed.

    Take a look at Shortcode API. The main difference is your handler function will need to assemble all the html output into a single string to be returned, all in php, instead of echoing snippets in between non-php elements.

    Thread Starter Pressnoob

    (@pressnoob)

    NM… Totally misread what you wrote, You’re saying make a plugin then add a shortcode at the end of it.

    Thread Starter Pressnoob

    (@pressnoob)

    Before even adding attributes to the short code I came up against some trouble already. Is there any reason why the script would stop working as soon as I transplanted it into a plugin? I’m not understanding the short code API, why can’t I retrieve other variables?

    include("amazon_api_class.php");
    
        $obj = new AmazonProductAPI();
    
        try
        {
            $result = $obj->getItemByAsin(B0067HQL30);
        }
        catch(Exception $e)
        {
            echo $e->getMessage();
        }
    
    $LowestP = $result->Items->Item->OfferSummary->LowestNewPrice->Amount;
    $MSRP = $result->Items->Item->ItemAttributes->ListPrice->Amount;
    $Savings =  $MSRP - $LowestP; 
    
    function get_product() {
    return $MSRP;
    }
    
    add_shortcode('get product', 'get_product');
    ?>

    Moderator bcworkz

    (@bcworkz)

    A php function can only access variables local to that function unless they are declared global. All the code except the include line needs to be in the handler function. And as mentioned in the API, any echos need to be resolved into a string and returned.

    Thread Starter Pressnoob

    (@pressnoob)

    Ah that’s what I thought! I’ll try putting everything into one function, and putting include() into a global variable and then into that function. Thanks a lot!

    Thread Starter Pressnoob

    (@pressnoob)

    Well it worked like a charm, I’m still trying to figure out the best way to style it but that should be really easy to do. For anyone else interested here’s the code:

    $get_amazon_api =   include("amazon_api_class.php");
    
     function get_product($atts) {
     extract(shortcode_atts(array(
     'asin' => ''
     ), $atts));
    global $get_amazon_api;
    
      $obj = new AmazonProductAPI();
    
        try
        {
            $result = $obj->getItemByAsin($asin);
        }
        catch(Exception $e)
        {
            echo $e->getMessage();
        }
    
    $LowestP = $result->Items->Item->OfferSummary->LowestNewPrice->Amount;
    $MSRP = $result->Items->Item->ItemAttributes->ListPrice->Amount;
    $Savings =  $MSRP - $LowestP; 
    
    $percent = $Savings / $MSRP;
    $percent2 = $percent * 100;
    $final_percent = number_format($percent2, 2) . "%";
    
    $LowestP2 = number_format($LowestP /100,2);
    $MSRP2 = number_format($MSRP /100,2);
    $Savings2 = number_format($Savings /100,2);
    
     return      "Save " . $final_percent ."<br>" . "Retail Price: $" . $MSRP2 .  "<br>" . "Lowest Price: $" . $LowestP2  . "<br>". "Total Savings: $" . $Savings2;
    
    }
    
    add_shortcode('get product', 'get_product');

    To use it just type [get product asin=”xxxxxxxx”]

    Thank you very much bcworkz.

    do you have a download version of that plugin?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Amazon Product API plugin that retrieves the list price and the lowest price’ is closed to new replies.