setAccessKey($access_key);
# Please add your secret key here
$config->setSecretKey($secret_key);
# Please add your partner tag (store/tracking id) here
$partnerTag = $asoc_tag;
/*
* PAAPI host and region to which you want to send request
* For more details refer: https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region
*/
if ($localeAmazon=="us"){
$config->setHost('webservices.amazon.com');
$config->setRegion('us-east-1');
} elseif ($localeAmazon=="uk"){
$config->setHost('webservices.amazon.co.uk');
$config->setRegion('eu-west-1');
} elseif ($localeAmazon=="es"){
$config->setHost('webservices.amazon.es');
$config->setRegion('eu-west-1');
} elseif ($localeAmazon=="mx"){
$config->setHost('webservices.amazon.com.mx');
$config->setRegion('us-east-1');
} elseif ($localeAmazon=="jp"){
$config->setHost('webservices.amazon.co.jp');
$config->setRegion('us-west-2');
} elseif ($localeAmazon=="it"){
$config->setHost('webservices.amazon.it');
$config->setRegion('eu-west-1');
} elseif ($localeAmazon=="in"){
$config->setHost('webservices.amazon.in');
$config->setRegion('eu-west-1');
} elseif ($localeAmazon=="de"){
$config->setHost('webservices.amazon.de');
$config->setRegion('eu-west-1');
} elseif ($localeAmazon=="fr"){
$config->setHost('webservices.amazon.fr');
$config->setRegion('eu-west-1');
} elseif ($localeAmazon=="ca"){
$config->setHost('webservices.amazon.ca');
$config->setRegion('us-east-1');
} elseif ($localeAmazon=="br"){
$config->setHost('webservices.amazon.com.br');
$config->setRegion('us-east-1');
} elseif ($localeAmazon=="au"){
$config->setHost('webservices.amazon.com.au');
$config->setRegion('us-west-2');
} elseif ($localeAmazon=="sg"){
$config->setHost('webservices.amazon.sg');
$config->setRegion('us-west-2');
} elseif ($localeAmazon=="tr"){
$config->setHost('webservices.amazon.com.tr');
$config->setRegion('eu-west-1');
} elseif ($localeAmazon=="ae"){
$config->setHost('webservices.amazon.ae');
$config->setRegion('eu-west-1');
} else {
$config->setHost('webservices.amazon.com');
$config->setRegion('us-east-1');
}
$apiInstance = new DefaultApi(
/*
* If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
* This is optional, `GuzzleHttp\Client` will be used as default.
*/
new GuzzleHttp\Client(), $config);
# Request initialization
# Specify keywords
$keyword = $keyword;
/*
* Specify the category in which search request is to be made
* For more details, refer: https://webservices.amazon.com/paapi5/documentation/use-cases/organization-of-items-on-amazon/search-index.html
*/
$searchIndex = "All";
# Specify item count to be returned in search result
//I need to convert text to number
$itemCount = (int)$product_number;
/*
* Choose resources you want from SearchItemsResource enum
* For more details, refer: https://webservices.amazon.com/paapi5/documentation/search-items.html#resources-parameter
*/
$resources = array(
SearchItemsResource::ITEM_INFOTITLE,
"Images.Primary.Medium","Images.Primary.Large","ItemInfo.Features",
SearchItemsResource::OFFERSLISTINGSPRICE);
# Forming the request
$searchItemsRequest = new SearchItemsRequest();
$searchItemsRequest->setSearchIndex($searchIndex);
$searchItemsRequest->setKeywords($keyword);
$searchItemsRequest->setItemCount($itemCount);
$searchItemsRequest->setPartnerTag($partnerTag);
$searchItemsRequest->setPartnerType(PartnerType::ASSOCIATES);
$searchItemsRequest->setResources($resources);
# Validating request
$invalidPropertyList = $searchItemsRequest->listInvalidProperties();
$length = count($invalidPropertyList);
if ($length > 0) {
echo "Error forming the request", PHP_EOL;
foreach ($invalidPropertyList as $invalidProperty) {
echo $invalidProperty, PHP_EOL;
}
return;
}
# Sending the request
try {
$searchItemsResponse = $apiInstance->searchItems($searchItemsRequest);
// echo 'API called successfully', PHP_EOL;
// echo 'Complete Response: ', $searchItemsResponse, PHP_EOL;
# Parsing the response
if ($searchItemsResponse->getSearchResult() != null) {
// echo '
Printing first item information in SearchResult:', PHP_EOL;
//$item = $searchItemsResponse->getSearchResult()->getItems()[0];
$productLink="";
$productImage="";
/******************************************************************/
// BORRA TODOS LOS POSTS DE LA CATEGORÍA (SI SETTING ACTIVADA)
/******************************************************************/
$options = get_option( 'amz_short_options' );
$opcionBuena=$options["select_field_0"];
if ($opcionBuena==1){
//Opcional, borra TODOS los posts de la categoría (por si existían otros con otros titles distintos a los del update que no se van a borrar abajo)
$query2 = new WP_Query(array(
'post_type' => 'amz_product', // name of post type.
'tax_query' => array(
array(
'taxonomy' => 'amazon_shortcode', // taxonomy name
'field' => 'term_id',
'terms' => $categoryId, // term id, term slug or term name
)
)
));
if ( $query2->have_posts() ) :
while ( $query2->have_posts() ) : $query2->the_post();
$idPostParaBorrar=get_the_ID();
//echo get_the_title();
wp_delete_post($idPostParaBorrar);
endwhile;
endif;
}
/******************************************************************/
// BORRA TODOS LOS POSTS DE LA CATEGORÍA (SI SETTING ACTIVADA)
/******************************************************************/
$amazonContents="";
foreach ($searchItemsResponse->getSearchResult()->getItems() as $item){
$pageExistente="";
$productLink="";
$amazonFeatures="";
$productPrice="";
$productImage="";
$postTitle="";
/*
print "
"; print_r($item); print ""; */ if ($item != null) { $amazonContents.="