WordPress.org

Ready to get started?Download WordPress

Forums

Amazon Product API plugin that retrieves the list price and the lowest price (8 posts)

  1. Pressnoob
    Member
    Posted 1 year ago #

    So I really want to make a plugin like this: http://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?

  2. bcworkz
    Member
    Posted 1 year ago #

    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.

  3. Pressnoob
    Member
    Posted 1 year ago #

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

  4. Pressnoob
    Member
    Posted 1 year ago #

    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');
    ?>
  5. bcworkz
    Member
    Posted 1 year ago #

    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.

  6. Pressnoob
    Member
    Posted 1 year ago #

    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!

  7. Pressnoob
    Member
    Posted 1 year ago #

    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.

  8. pedroxe
    Member
    Posted 1 year ago #

    do you have a download version of that plugin?

Topic Closed

This topic has been closed to new replies.

About this Topic