Search code examples
phpxmlamazon-mws

weird result when parsing XML document using php


I am attempting to use PHP to parse an XML document. It is basically working except I am getting a very weird error that I do not see a reason for. The XML for the first ASIN is this:

    <?xml version="1.0"?>
<GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestOfferListingsForASINResult ASIN="1580230032" status="Success">
<AllOfferListingsConsidered>true</AllOfferListingsConsidered>
<Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>1580230032</ASIN>
  </MarketplaceASIN>
</Identifiers>
<LowestOfferListings>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Merchant</FulfillmentChannel>
      <ShipsDomestically>True</ShipsDomestically>
      <ShippingTime>
        <Max>0-2 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>2</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>221</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>10.12</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>6.13</Amount>
      </ListingPrice>

and the XML for the second ASIN is:

    <GetLowestOfferListingsForASINResult ASIN="0870714376" status="Success">
  <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>0870714376</ASIN>
  </MarketplaceASIN>
</Identifiers>
<LowestOfferListings>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Merchant</FulfillmentChannel>
      <ShipsDomestically>True</ShipsDomestically>
      <ShippingTime>
        <Max>0-2 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>98-100%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>2</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>1416</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>18.49</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>14.50</Amount>
      </ListingPrice>

As you can see, the Amount for each of these is different, yet when I run the program I am only getting the Amount from the first ASIN. Here is my PHP code to get the results:

    $parsed_xml = amazon_xml($isbn);

$current = $parsed_xml->ListMatchingProductsResult->Products->Product;
$asin = $current->Identifiers->MarketplaceASIN->ASIN;

// get information based on the items ASIN
$price_xml = amazonPrice_xml($asin, $ItemCondition);
$currentPrice = $price_xml ->GetLowestOfferListingsForASINResult->Product->LowestOfferListings->LowestOfferListing;

// check to see if there are values
if(!empty($currentPrice))
{  

    foreach($currentPrice as $offer){
            $totalFeedback = $offer->SellerFeedbackCount;
        //if ($totalFeedback >99) {
            $condition = $offer->Qualifiers->ItemSubcondition;

                //amazon condition matching algorithm (so we can match our condition up against amazons conditions)
                switch ($condition) {
                case "New":
                    $amazonCondition = 5;
                    break;
                case "Mint":
                    $amazonCondition = 4;
                    break;
                case "VeryGood":
                    $amazonCondition = 3;
                    break;
                case "Good":
                    $amazonCondition = 2;
                    break;
                case "Acceptable":
                    $amazonCondition = 1;
                    break;
                default:
                    $amazonCondition = 0;
                }

            $lowestAmazonPrice = 0;

                            $currentPrice = $price_xml ->GetLowestOfferListingsForASINResult->Product->LowestOfferListings;
             foreach($currentPrice->LowestOfferListing as $offer){
                                if( ($ourCondition = $amazonCondition) /*&& ($totalFeedback >= 5000)*/) {
                    $offerArray[$y] = str_replace('$','',$offer->Price->ListingPrice->Amount);
                    $y++;
                                }   
            }
            $lowestAmazonPrice = min($offerArray);
    }

                    if ($fillzPrice > $lowestAmazonPrice ) {
                        $avgPrice = ($lowestAmazonPrice - ($lowestAmazonPrice * .08));
                         $source = "Adjusted Fillz Price";
                    } else {
                        $avgPrice = $fillzPrice;
                        $source = "Original Price";
                    }
    // check to make sure we are not going below established floor for prices
            if($avgPrice < ($follettPrice * 2.37)){
        $avgPrice = $follettPrice * 2.37;
        $source = "Follett Pricing";
    }
    if($avgPrice < ($row['cost'] * 2)){
        $avgPrice = $row['cost'] * 2;
        $source = "Double Cost";
    }
    /*if($avgPrice < 5.50){
        $avgPrice = 5.50;
        $source = "Lowest Base Cost";
    }*/
            //update fillzPrice
    $conn->query("UPDATE inventory SET ourPrice = $avgPrice, upload = '1' WHERE sku=" . $row['sku']);

    $pricedSKU[$n] = array('sku' => $row['sku'],
                            'new price' => number_format($avgPrice, 2, '.', ''),
                            'price source' => $source,
                                                            'Fillz' => $fillzPrice,
                                                            'Amazon'=> $lowestAmazonPrice,
                                                            'asin'=> $asin,
                                                            'condition'=>$ourCondition
                            );

    $n++;

}

}

As I stated earlier, most of the output is correct with the exception of this issue. I have an email sent to me with the ASIN and amounts so I can check the results. Any thoughts onbn what I am doing wrong and how to correct it?


Solution

  • In case anyone else has this issue, the resolution was to add this line of code:

     $offerArray = array();
    

    in after the first while statement. Everything works fine now.