I've started to integrate the amazon api within my couch website to experiment. I've basically created a clonable template which contains items on amazon.
What i do is create an array of those items and pass that array to amazon to return all the information i need like if the item is in stock or exists etc.
What i'm getting stuck on, is when that data is returned, i'd like to update each item within couch with some information returned from the api.
So for example, if the api returns the item is out of stock, i'd want to update that particular item within couch to set it to out of stock. or if the item is in stock, update that item with the url returned from the api, and how much stock it has etc.
What i have below is a rough test which is working perfectly, and within the forearch there is some if statements, if the item has a blank amazon id, or if the amazon id doesn't exist in the returned array - if these are the cases, for now i'd like to set that item to out of stock within couch.
So i'm wondering if it's possible to use cms:db_persist to update that item within my php script, i know you can use some couch tags stuff within the php tags, but doesn't play nice with cms:db_persist.
My rough code i was looking at would be:
Which i was placing within the else parts of the if statements, but so far no luck, just errors. wondering if db_persist would be the best way to do this, or if there's another better way to accomplish this?
Maybe have to go down the route of creating my own sql and running that instead to update the database with the values i want.
Thanks in advance for any tips/answers this lovely forum provides.
What i do is create an array of those items and pass that array to amazon to return all the information i need like if the item is in stock or exists etc.
What i'm getting stuck on, is when that data is returned, i'd like to update each item within couch with some information returned from the api.
So for example, if the api returns the item is out of stock, i'd want to update that particular item within couch to set it to out of stock. or if the item is in stock, update that item with the url returned from the api, and how much stock it has etc.
What i have below is a rough test which is working perfectly, and within the forearch there is some if statements, if the item has a blank amazon id, or if the amazon id doesn't exist in the returned array - if these are the cases, for now i'd like to set that item to out of stock within couch.
So i'm wondering if it's possible to use cms:db_persist to update that item within my php script, i know you can use some couch tags stuff within the php tags, but doesn't play nice with cms:db_persist.
My rough code i was looking at would be:
- Code: Select all
<cms:db_persist
_masterpage=k_template_name
_page_id=$itemId
_mode='edit'
amazon_status="<cms:set amazon_status='Out Of Stock' />"
/>
Which i was placing within the else parts of the if statements, but so far no luck, just errors. wondering if db_persist would be the best way to do this, or if there's another better way to accomplish this?
Maybe have to go down the route of creating my own sql and running that instead to update the database with the values i want.
- Code: Select all
<cms:php>
require('../php/amazon/AmazonECS.class.php');
$amazonEcs = new AmazonECS($api,$secret,$ext,$tag);
$itemArr=array(<cms:pages masterpage='store/index.php' limit='10' folder='uk' include_subfolders='1'>
array('asin'=>'<cms:show amazon_id />','pageId'=>'<cms:show k_page_id />'),
</cms:pages>
);
$arrAsin = array_map(function($asin) {return $asin['asin'];}, $itemArr);
$amzResponse = $amazonEcs->responseGroup('Small,VariationMatrix,Offers')->optionalParameters(array('Condition' => 'New'))->lookup($arrAsin);
foreach($itemArr as $item) {
$itemId = $item['pageId'];
$itemAsin = $item['asin'];
$itemUrl = '';
$itemTotalNew = '';
$itemVariations = '';
if(!empty($itemAsin)){
if(in_array_r($itemAsin, $amzResponse->Items->Item)){
echo $itemAsin . " - In Array<br>";
foreach ($amzResponse->Items->Item as $result) {
if($itemAsin == $result->ASIN){
$itemUrl = $result->DetailPageURL;
$itemTotalNew = $result->OfferSummary->TotalNew;
$itemVariations = $result->Variations->TotalVariations;
}
}
echo $itemAsin . " - <a target='_Blank' href='" . $itemUrl ."'>" . $itemAsin ."</a>" . "<br/>";
echo "Total New:" . $itemTotalNew . " Total Variations:" . $itemVariations . "<br/>";
echo "Page ID:" . $itemId . "<br><br>";
}else{
echo $itemAsin . " / ". $itemId . " - Not In Array<br><br>";
}
}else{
echo $itemId . " Has Blank ASIN<br><br>";
}
}
function in_array_r($item , $array){
return preg_match('/"'.$item.'"/i' , json_encode($array));
}
</cms:php>
Thanks in advance for any tips/answers this lovely forum provides.