PDA

View Full Version : please help a noobie: how to find and display product data?


ljackson09
10-06-09, 16:19
Hi All,

I'm hoping someone would kindly help me, i'm really struggling to achieve the following:

i would like to search a list of merchants for a particular product which is going to be set by the user via a search but i'll add that later, then for each of the merchants that sells that product list the first occurance of that product and print out the store name, the product price, delivery cost, and avalibility.

so each of the stores within the list can only have one product displayed for it.

firstly is this possible?
and no 2 where have i gone wrong im my code
$oClient = ClientFactory::getClient(API_USERNAME, API_PASSWORD, API_USER_TYPE);

$stores = array('iMerchantId' => array(1599, 1598));
$oResponse1 = $oClient->call('getMerchant', $stores);
print_r ($oResponse1);
//$count = 1;
foreach($oResponse1 as $store){
$storename = $store->sName;
//if ($count <= 1){
$keywords = "Kaspersky Internet Security 2009";
$aParams7 = array("sQuery" => $keywords, "sSort" => "popularity");
$oResponse= $oClient->call('getProductList', $aParams7);
$prodcount = 1;
foreach($oResponse->oProduct as $details){
if ($prodcount <= 1){
$storename = $details->iCategoryId;
$name = $details->sName;
$price = $details->fPrice;
$image = $details->sAwThumbUrl;
$delcost = $details->fDeliveryCost;
$deltime = $details->sDeliveryTime;
print "<p>";
print "Store Name: ". $storename;
print "<br />";
print "Item: ". $name;
print "<br />";
print "Price: ". $price;
print "<br />";
print "Delivery Cost: ". $delcost;
print "<br />";
print "Delivery Time ".$deltime;
print "</p>";

}
$count ++;
$prodcount ++;

//}
}
}


with this im getting several products per store display instead of a maximum of one product per store?

many many thanks for any help
Luke

Andy
11-06-09, 14:07
Ill have a look and post back shortly

ljackson09
11-06-09, 14:29
ok thanks mate

Andy
11-06-09, 14:48
OK, this might not be the best way of doing it, but so long as you're only going to do what you've said you're going to do, it should suffice.


print('<pre>');

$keywords = "Kaspersky Internet Security 2009";
$stores = array('iMerchantId' => array('1599','1598'));
$aParams = array("sQuery" => $keywords, "sSort" => "popularity", "iMerchantId" => $stores);

print_r ($aParams);

$oResponse= $oClient->call('getProductList', $aParams);

print_r ($oResponse);



## get the merchant names in a handy array for later

$merchantNameArray = array();

foreach($oResponse->oRefineByGroup[1]->oRefineByDefinition as $merch){
$merchantNameArray[$merch->sId] = $merch->sName;
}


#basis of one per merchant
$onePerMerchant = array();


$x = 0;
foreach($oResponse->oProduct as $oProduct){

if(!in_array($oProduct->iMerchantId,$onePerMerchant)){#makes sure we dont already have a product from this merchant

# add merchantID to $onePerMerchant Array
$onePerMerchant[$x] = $oProduct->iMerchantId;


print "<p>";
print "Store Name: ". $merchantNameArray[$oProduct->iMerchantId];
print "<br />";
print "Item: ". $oProduct->sName;
print "<br />";
print "Price: ". $oProduct->fPrice;
print "<br />";
print "Delivery Cost: ". $oProduct->fDeliveryCost;
print "<br />";
print "Delivery Time ".$oProduct->sDeliveryTime;
print "</p>";
$x++;
}
}

Ive tested it and it does what its supposed to. There is a slight bugness where occasionally (depending on the search string) ONE store name does not get entered into $merchantNameArray array(), but you can always if/else that out.

Hope that helps you get to grips, mines a pint!!:D

One thing I would suggest for the future is that you try to keep your coding as efficient and as similar to the SW standards as possible.

For instance, your original had:

$storename = $details->iCategoryId;
$name = $details->sName;
$price = $details->fPrice;
$image = $details->sAwThumbUrl;
$delcost = $details->fDeliveryCost;
$deltime = $details->sDeliveryTime;
You're just setting extra duplicated variables and using more memory than you need to.

Also, have a read up on Hungarian Notation (http://en.wikipedia.org/wiki/Hungarian_notation) as this is the style used in SW. Takes about 5 minutes to read and you'll have that EUREKA feeling when you see the code again.

:cool:

ljackson09
11-06-09, 15:01
Hi Mate,

Im getting the following print out on my website

Array
(
[sQuery] => Kaspersky Internet Security 2009
[sSort] => popularity
[iMerchantId] => Array
(
[iMerchantId] => Array
(
[0] => 1599
[1] => 1598
)

)

)


Fatal error: Call to a member function call() on a non-object in /customers/kernow-connect.com/kernow-connect.com/httpd.www/Price Comparison/AffiliateAPI/producttest.php on line 23

which is this line

$oResponse= $oClient->call('getProductList', $aParams);

any ideas
cheers
Luke

Andy
11-06-09, 15:15
Could be anything really, so Ill try to cover all bases.


looks to me like a file isn't being included. Make sure that this is above everything


define('API', 'PS');
require_once ('constants.inc.php');
require_once ('classes/class.ClientFactory.php');

$outPut .= 'All files included<br />';

# Username and password constants are defined in constants.inc.php
$oClient = ClientFactory::getClient(API_USERNAME, API_PASSWORD, API_USER_TYPE);


Make sure you're using V2 of the API. The top of constants.inc.php should look something like this:

// Constants for both APIs
define('API_VERSION', 2);
define('API_USER_TYPE', 'affiliate'); // (affiliate || merchant)
define('API_USERNAME', '12345'); // You affiliate || merchant ID (e.g: 12345)
define('API_PASSWORD', '1234567890987654321234567890987654321'); // You API password (found in your userarea)
Make sure the API_USERNAME is correct (your awin aff id) and the API_PASSWORD is the right one too. You can find it here (login to awin 1st): https://www.affiliatewindow.com/affiliates/accountdetails.php
Look for: ProductServe API (ShopWindow Client) Password
and its the V2 Hash you need.

Failing all that, try unpacking the API from the zip folder again (you did keep it didnt you?) and overwriting the "classes" directory.

Oh, and you are using PHP5 aren't you? Test by saving this to a file (test.php) and uploading to your server/localhost:
<?php
phpinfo();
?>

ljackson09
11-06-09, 15:28
ah ha...

when copying your code snippet in i removed this by mistake

$oClient = ClientFactory::getClient(API_USERNAME, API_PASSWORD, API_USER_TYPE);

oops lol

now i have some output :)

however im still getting details of items from stores i dont want info from, the query you kindly gave me is returning data from
7dayshop.com,
Micro Direct,
Saverstore,
and a few others

is there a way to ensure that i only get the details from the stores in the query?

thanks for all your help so far really appreciate it :)
Luke

ljackson09
11-06-09, 15:46
also mate is there a list of categorys and their ids so i can use please?

thanks
Luke

Andy
11-06-09, 15:47
Yeah, this should work..........


change:
$stores = array('iMerchantId' => array(1599, 1598)); to:

$storesArray = array(1599, 1598);
$stores = array('iMerchantId' => $storesArray);
then find:if(!in_array($oProduct->iMerchantId,$onePerMerchant)){and change to:
if(!in_array($oProduct->iMerchantId,$onePerMerchant) && in_array($oProduct->iMerchantId,$storesArray)){That should do the job. Haven't test that bit but it looks about right!
That'll be a pint n a bag of dry roasted;)

the cat ids can be found in the documentation.
Heres the cat id pdf: http://www.affiliatewindow.com/documents/affiliates/documentation/shopwindow/ShopWindow_Appendix_2_-_Category_IDs.pdf

ljackson09
11-06-09, 16:18
ok mate its now now printing out any data :(

i have added a new merchant to the list which is play.com and i tried a search for slumdog millionaire but it only returned products from
[sId] => 2066
[sName] => Borders
)

[1] => stdClass Object
(
[sId] => 1344
[sName] => Bangcd.Com
)

[2] => stdClass Object
(
[sId] => 1152
[sName] => MyMemory
)

[3] => stdClass Object
(
[sId] => 15
[sName] => CD WOW!
)
but no play.com so the fact that nothing was printed out may be because this query isnt returning play.com and im sure play has slumdog for sale?

here is my code
<?php
define('API', 'PS');
require_once ('constants.inc.php');
require_once ('classes/class.ClientFactory.php');

$outPut .= 'All files included<br />';

# Username and password constants are defined in constants.inc.php
$oClient = ClientFactory::getClient(API_USERNAME, API_PASSWORD, API_USER_TYPE);
?>
</head>

<body>
<?php
print('<pre>');

$keywords = "Slumdog Millionaire";
$category = "240";
$storesArray = array(1599, 1598, 1418);
$stores = array('iMerchantId' => $storesArray);
$aParams = array("sQuery" => $keywords, "sSort" => "popularity", "iMerchantId" => $stores, "iCategoryId" => $category);

print_r ($aParams);

$oResponse=$oClient->call('getProductList', $aParams);

print_r ($oResponse);



## get the merchant names in a handy array for later

$merchantNameArray = array();

foreach($oResponse->oRefineByGroup[1]->oRefineByDefinition as $merch){
$merchantNameArray[$merch->sId] = $merch->sName;
}


#basis of one per merchant
$onePerMerchant = array();


$x = 0;
foreach($oResponse->oProduct as $oProduct){

if(!in_array($oProduct->iMerchantId,$onePerMerchant) && in_array($oProduct->iMerchantId,$storesArray)){ #makes sure we dont already have a product from this merchant

# add merchantID to $onePerMerchant Array
$onePerMerchant[$x] = $oProduct->iMerchantId;


print "<p>";
print "Store Name: ". $merchantNameArray[$oProduct->iMerchantId];
print "<br />";
print "Item: ". $oProduct->sName;
print "<br />";
print "Price: ". $oProduct->fPrice;
print "<br />";
print "Delivery Cost: ". $oProduct->fDeliveryCost;
print "<br />";
print "Delivery Time ".$oProduct->sDeliveryTime;
print "</p>";
$x++;
}
}
?>

and here is the resulting output
http://www.kernow-connect.com/Price%20Comparison/AffiliateAPI/producttest.php

any ideas why this isnt finding play.com?

p.s i have also included the category field as well which is currently set to dvd, but even without that it still doesnt work :(

thanks again for your time and efforts
Luke

GeorgeGaz
11-06-09, 16:32
Hello,

Play.com are set to ADULT content so unless you are allowing adult content then they would not show up.

Cheers

ljackson09
11-06-09, 16:45
hi mate, thanks that explains things,

however... :D

when i add this:
"bAdult" => true
in the following query

$aParams = array("sQuery" => $keywords, "sSort" => "popularity", "iMerchantId" => $stores, "iCategoryId" => $category, "bAdult" => true);

the whole thing breaks :(

i get
Warning: Invalid argument supplied for foreach() in /customers/kernow-connect.com/kernow-connect.com/httpd.www/Price Comparison/AffiliateAPI/producttest.php on line 40

Warning: Invalid argument supplied for foreach() in /customers/kernow-connect.com/kernow-connect.com/httpd.www/Price Comparison/AffiliateAPI/producttest.php on line 50

but if i remove it i dont get any errors?

am i doing it wrong?

thanks
Luke

ljackson09
11-06-09, 18:24
also appart from not being able to set adult to true the results returned are not as expected?

for example if i search for
$keywords = "Quantum of Solace";
$category = "550";
$storesArray = array(1186,1109,380,1257,1418,1228,172,1672,1744,8 54,2147,1331,2258,1599,1487,1946,1322,323,2249,159 8,1964);
$stores = array('iMerchantId' => $storesArray);
$aParams = array("sQuery" => $keywords, "sSort" => "popularity", "iMerchantId" => $stores, "iCategoryId" => $category, "bAdult" => false);

you'd of thought the result would be Quantum of Solace on blu-ray not Microsoft Xbox 360 60GB console package with 4 top games which includes the Quantum of Solace game.

have i set up the category right as it is set to blu-ray in the query

many thanks
Luke

Andy
12-06-09, 11:29
Ive just run an exact copy of your code above andit only printed out 1 product out of the 9 it found (so that bit's still working).

All 9 products were from play.com.

Every affiliate's list of merchants is different, so everyone will get a slightly different result set, so Im not sure what the problem could be. Its definitely not a coding error though.

Perhaps you have the Adult supression on in your AffWin account or in constants.inc.php.
At present, the Adult filter is merchant specific rather than product specific. Play.com sell porn, so the filter will block them completely. Have a look in that file or your account and make sure you're not inadvertantly blocking them.

Also, make sure you're signed up to them too!

I know that SW are working on a more accurate filter which works "per product" for V3, so this wouldnt be an issue for too long.

ljackson09
12-06-09, 15:40
Ah sweet !!! it was turned off in my account :)

now i have play.com results show.

hopefully that will have solved my problems :)

although just noticed that when printing out the store name it doesnt always print the name out? like with the search above im not getting any name print out?

think its since changing
$stores = array('iMerchantId' => array(1599, 1598));

to

$storesArray = array(1599, 1598);
$stores = array('iMerchantId' => $storesArray);

and
if(!in_array($oProduct->iMerchantId,$onePerMerchant)){

to
if(!in_array($oProduct->iMerchantId,$onePerMerchant) && in_array($oProduct->iMerchantId,$storesArray)){

any ideas?

also something else that has just cropped up:
if adult = true will i still get non adult products show up?
and how can i utalise the product count so that if the number of products is 0 or 1 then i need it to just print the data out because the for loops dont work with 0 or 1 products

i have tried this
$count=iTotalCount($oResponse);
print "<h1>$count</h1>";

but thats not displaying anything? if i can get the number of products i should beable to sort the rest out? hopefully :)

thank you so much for what you have done so far i really appreciate it :)

Luke

Andy
15-06-09, 14:19
$stores = array('iMerchantId' => array(1599, 1598)); Is exactly the same as:
$storesArray = array(1599, 1598);
$stores = array('iMerchantId' => $storesArray);except it allows you to seperate the array of Merchant IDs from the $stores array. Its a readability thing. It'll also allow you to build that array dynamically in future.

if(!in_array($oProduct->iMerchantId,$onePerMerchant)){if(!in_array($oProdu ct->iMerchantId,$onePerMerchant) && in_array($oProduct->iMerchantId,$storesArray)){ This change COULD be the reason, but I put it there to make sure that you were only printing a merchant that you called for.


The adult boolean is a switch to allow adult products through. If you set it to true, the filter is not applied so you'll get everything. False prevents adult stuff.


Last Q:
try the array count function (http://us3.php.net/manual/en/function.count.php), and make sure you're only counting the oProduct array like so:

$count = count($oResponse->oProduct);
print($count);

ljackson09
15-06-09, 14:30
cool thanks for this

another problem im having is on some searches im getting
fatal error or Warning: Invalid argument supplied for foreach() in /customers/kernow-connect.com/kernow-connect.com/httpd.www/Price Comparison/price_results.php on line 253

Warning: Invalid argument supplied for foreach() in /customers/kernow-connect.com/kernow-connect.com/httpd.www/Price Comparison/price_results.php on line 262

and im not sure why? any ideas
would this mean that there are no products/merchants to do a for each on???

thank you
Luke

ljackson09
17-06-09, 18:44
ok i have sorted it out now :D
i just added
if(count($oResponse->oProduct) <> 0){;
before
foreach($oResponse->oProduct as $oProduct){

and that did the trick whoooo lol

thank you guys SOOOOOO much for all your help and advice, wouldn't have got here without you!!!! expect to see me back when V3 arrives :D (any ideas when that may be?) cheers

Luke

Andy
18-06-09, 14:58
That warning is simply because there are no products to loop through, so adding a comparison statement is the right thing to do here.

although:if(count($oResponse->oProduct) <> 0){
is not the right way to go about it. I'd be inclined to use this instead:
if(count($oResponse->oProduct) > '0'){

ljackson09
18-06-09, 18:51
Ah right thank you :)

Luke

ljackson09
12-08-09, 12:55
Hi Andy,

i expect you know what im gonna ask :D

now that v3 has been launched how do i set a product filter? so i only get the products from a stated category :)

also i read in the changes list about being able to pull out discount codes from the v3 api? how do i go about this, i would like to pull out all the codes of the stores im signed up to?

and finally :D

how to i switch over to v3?

many thanks
Luke