';
//echo $startpos.'
';
//echo $endpos.'
';
//echo $endofstring.'
';
//echo substr($url, 0, $startpos).'
';
//echo substr($url, $endpos, strlen($url) - $endpos).'
';
$url = substr( $url, 0, $startpos ) . $trackingId . substr( $endofstring, $endpos, strlen( $endofstring ) - $endpos );
//echo $url;
//exit;
//$url = str_replace('&AssociateTag='.$associateTag, '&AssociateTag='.$trackingId, $url);
//$url = str_replace('&tag='.$associateTag, '&tag='.$trackingId, $url);
//$url = str_replace('?tag='.$associateTag, '?tag='.$trackingId, $url);
return $url;
}
private function validate_request_v5( $searchItemsRequest ) {
$invalidPropertyList = $searchItemsRequest->listInvalidProperties();
$length = count( $invalidPropertyList );
if ( $length > 0 ) {
$txt = "Error forming the request" . PHP_EOL;
foreach ( $invalidPropertyList as $invalidProperty ) {
$txt .= $invalidProperty . PHP_EOL;
}
throw new Exception( esc_html__( $txt, 'affiliate-toolkit-starter' ) );
}
}
private function validate_response_v5( $getItemsResponse ) {
if ( $getItemsResponse->getErrors() != null ) {
throw new Exception( esc_html__( $getItemsResponse->getErrors()[0]->getCode() . ': ' . $getItemsResponse->getErrors()[0]->getMessage(), 'affiliate-toolkit-starter' ) );
}
}
private function set_default_shop( $post_id ) {
$subshopsold = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_default_shops' );
$subshops = array();
//add subshop for amazon
$subshop = new subshop();
$subshop->title = esc_html__( 'Amazon', 'affiliate-toolkit-starter' );
$subshop->shopid = $post_id;
$website = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_website' );
switch ( $website ) {
default:
$subshop->logourl = plugins_url( 'images/logo-normal-amazon.jpg', ATKP_AMAZON_PLUGIN_FILE );
$subshop->smalllogourl = plugins_url( 'images/logo-small-amazon.jpg', ATKP_AMAZON_PLUGIN_FILE );
break;
case 'de':
$subshop->logourl = plugins_url( 'images/logo-normal-amazon-de.jpg', ATKP_AMAZON_PLUGIN_FILE );
$subshop->smalllogourl = plugins_url( 'images/logo-small-amazon-de.jpg', ATKP_AMAZON_PLUGIN_FILE );
break;
}
$subshop->enabled = true;
array_push( $subshops, $subshop );
//für bestehende alte subshops ist dieser teil noch drinnen
if ( is_array( $subshopsold ) ) {
foreach ( $subshopsold as $shopold ) {
if ( $subshop->shopid == $shopold->shopid && $subshop->programid == $shopold->programid ) {
$subshop->enabled = $shopold->enabled;
$subshop->customtitle = $shopold->customtitle;
$subshop->customsmalllogourl = $shopold->customsmalllogourl;
$subshop->customlogourl = $shopold->customlogourl;
$subshop->customfield1 = $shopold->customfield1;
$subshop->customfield2 = $shopold->customfield2;
$subshop->customfield3 = $shopold->customfield3;
}
}
}
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_default_shops', $subshops );
}
private function get_config( $apikey, $apisecretkey, $country ) {
$config = new Amazon\ProductAdvertisingAPI\v1\Configuration();
$config->setAccessKey( $apikey );
$config->setSecretKey( $apisecretkey );
$host = '';
$region = '';
switch ( $country ) {
default:
case 'de':
$host = 'webservices.amazon.de';
$region = 'eu-west-1';
break;
case 'nl':
$host = 'webservices.amazon.nl';
$region = 'eu-west-1';
break;
case 'com':
$host = 'webservices.amazon.com';
$region = 'us-east-1';
break;
case 'co.uk':
$host = 'webservices.amazon.co.uk';
$region = 'eu-west-1';
break;
case 'ca':
$host = 'webservices.amazon.ca';
$region = 'us-east-1';
break;
case 'fr':
$host = 'webservices.amazon.fr';
$region = 'eu-west-1';
break;
case 'co.jp':
$host = 'webservices.amazon.co.jp';
$region = 'us-west-2';
break;
case 'it':
$host = 'webservices.amazon.it';
$region = 'eu-west-1';
break;
case 'cn':
case 'es':
$host = 'webservices.amazon.es';
$region = 'eu-west-1';
break;
case 'in':
$host = 'webservices.amazon.in';
$region = 'eu-west-1';
break;
case 'au':
$host = 'webservices.amazon.com.au';
$region = 'us-west-2';
break;
case 'com.br':
$host = 'webservices.amazon.com.br';
$region = 'us-east-1';
break;
case 'com.mx':
$host = 'webservices.amazon.com.mx';
$region = 'us-east-1';
break;
case 'com.tr':
$host = 'webservices.amazon.com.tr';
$region = 'eu-west-1';
break;
case 'ae':
$host = 'webservices.amazon.ae';
$region = 'eu-west-1';
break;
case 'pl':
$host = 'webservices.amazon.pl';
$region = 'eu-west-1';
break;
case 'com.be':
$host = 'webservices.amazon.com.be';
$region = 'eu-west-1';
break;
}
$config->setHost( $host );
$config->setRegion( $region );
return $config;
}
private function check_guzzle() {
$funcInc = [
'GuzzleHttp\choose_handler' => 'lib/vendor/guzzlehttp/guzzle/src/functions_include.php',
'GuzzleHttp\Psr7\build_query' => 'lib/vendor/guzzlehttp/psr7/src/functions.php',
'GuzzleHttp\Promise\promise_for' => 'lib/vendor/guzzlehttp/promises/src/functions.php',
//'Promise\promise_for' => 'lib/vendor/guzzlehttp/promises/src/functions.php',
];
foreach ( $funcInc as $function => $incPath ) {
if ( ! function_exists( $function ) ) {
$includePath = ATKP_AMAZON_PLUGIN_DIR . DIRECTORY_SEPARATOR . $incPath;
if ( file_exists( $includePath ) ) {
require_once $includePath;
}
}
}
}
private function get_api_instance( $apikey, $apisecretkey, $website ) {
require_once ATKP_AMAZON_PLUGIN_DIR . '/lib/vendor/autoload.php';
$config = $this->get_config( $apikey, $apisecretkey, $website );
$this->check_guzzle();
$apiInstance = new Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\api\DefaultApi( new \GuzzleHttp\Client(), $config );
return $apiInstance;
}
private function check_configuration_v5( $post_id, $apikey, $apisecretkey, $website, $usessl, $trackingid ) {
//require_once ATKP_PLUGIN_DIR . '/lib/paapi5-sdk/vendor/autoload.php';
$apiInstance = $this->get_api_instance( $apikey, $apisecretkey, $website );
$searchItemsRequest = new Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsRequest();
$searchItemsRequest->setSearchIndex( 'All' );
$searchItemsRequest->setKeywords( 'Harry Potter' );
$searchItemsRequest->setItemCount( 1 );
$searchItemsRequest->setPartnerTag( $trackingid );
$searchItemsRequest->setPartnerType( Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\PartnerType::ASSOCIATES );
$this->validate_request_v5( $searchItemsRequest );
$test = '';
try {
$searchItemsResponse = $apiInstance->searchItems( $searchItemsRequest );
$this->validate_response_v5( $searchItemsResponse );
$itemcount = $searchItemsResponse->getSearchResult()->getTotalResultCount();
if ( $itemcount == 0 ) {
$test = 'item count is null';
}
} catch ( Amazon\ProductAdvertisingAPI\v1\ApiException $exception ) {
$test = "API-Error: " . $exception->getCode() . " " . $exception->getMessage();
if ( $exception->getResponseObject() instanceof Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\ProductAdvertisingAPIClientException ) {
$errors = $exception->getResponseObject()->getErrors();
foreach ( $errors as $error ) {
$test = "Response-Error: " . $error->getCode() . " " . $error->getMessage();
}
} else {
$test .= "Error response body: " . $exception->getResponseBody();
}
} catch ( Exception $exception ) {
$test = "Error Message: " . $exception->getMessage(); //. ' ' . $exception->getTraceAsString();
}
if ( $test == '' ) {
$this->set_default_shop( $post_id );
} else {
return $test;
}
}
private function check_configuration_creators( $post_id, $website, $trackingid ) {
$credentialId = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_creators_credential_id' );
$credentialSecret = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_creators_credential_secret' );
$credentialVersion = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_creators_credential_version' );
if ( $credentialId == '' || $credentialSecret == '' ) {
return 'Creators API Credentials are empty';
}
require_once ATKP_AMAZON_PLUGIN_DIR . '/lib/vendor/autoload.php';
require_once ATKP_AMAZON_PLUGIN_DIR . '/includes/atkp_marketplace_mapper.php';
$this->check_guzzle();
$marketplace = atkp_marketplace_mapper::get_marketplace( $website );
if ( $credentialVersion == '' ) {
$credentialVersion = atkp_marketplace_mapper::get_default_version( $website );
}
$config = new \Amazon\CreatorsAPI\v1\Configuration();
$config->setCredentialId( $credentialId );
$config->setCredentialSecret( $credentialSecret );
$config->setVersion( $credentialVersion );
$apiInstance = new \Amazon\CreatorsAPI\v1\com\amazon\creators\api\DefaultApi(
new \GuzzleHttp\Client(), $config
);
$searchRequest = new \Amazon\CreatorsAPI\v1\com\amazon\creators\model\SearchItemsRequestContent();
$searchRequest->setKeywords( 'Harry Potter' );
$searchRequest->setPartnerTag( $trackingid );
$searchRequest->setItemCount( 1 );
$searchRequest->setResources( array(
\Amazon\CreatorsAPI\v1\com\amazon\creators\model\SearchItemsResource::ITEM_INFO_TITLE,
) );
$test = '';
try {
$searchResponse = $apiInstance->searchItems( $marketplace, $searchRequest );
if ( $searchResponse->getErrors() != null ) {
$test = $searchResponse->getErrors()[0]->getCode() . ': ' . $searchResponse->getErrors()[0]->getMessage();
} else {
$itemcount = $searchResponse->getSearchResult()->getTotalResultCount();
if ( $itemcount == 0 ) {
$test = 'item count is null';
}
}
} catch ( \Amazon\CreatorsAPI\v1\ApiException $exception ) {
$test = "API-Error: " . $exception->getCode();
$responseBody = $exception->getResponseBody();
if ( $responseBody != null ) {
$decoded = json_decode( $responseBody, true );
if ( is_array( $decoded ) ) {
if ( ! empty( $decoded['message'] ) ) {
$test .= " Message: " . $decoded['message'];
}
if ( ! empty( $decoded['reason'] ) ) {
$test .= " Reason: " . $decoded['reason'];
}
if ( ! empty( $decoded['type'] ) ) {
$test .= " Type: " . $decoded['type'];
}
} else {
$test .= " " . $exception->getMessage();
}
} else {
$test .= " " . $exception->getMessage();
}
} catch ( Exception $exception ) {
$test = "Error Message: " . $exception->getMessage();
}
if ( $test == '' ) {
$this->set_default_shop( $post_id );
} else {
return $test;
}
return '';
}
public function check_configuration( $post_id ) {
try {
$apikey = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_key' );
$apisecretkey = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_secret_key' );
$website = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_website' );
$trackingid = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_tracking_id' );
$usessl = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_tracking_id' );
$sitestripe = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_sitestripe' );
$this->seconds_wait = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_seconds_wait' );
if ( $this->seconds_wait <= 0 ) {
$this->seconds_wait = 1;
}
if ( $sitestripe == 2 || $sitestripe == 3 ) {
return '';
}
if ( $sitestripe == 4 || $sitestripe == 5 ) {
return $this->check_configuration_creators( $post_id, $website, $trackingid );
}
$message = '';
if ( $apikey != '' && $apisecretkey != '' ) {
return $this->check_configuration_v5( $post_id, $apikey, $apisecretkey, $website, $usessl, $trackingid ) . '';
} else {
//wenn zugangscodes gelöscht werden muss message auch geleert werden
$message = 'Credientials are empty';
}
return $message;
} catch ( Exception $e ) {
if ( ATKPLog::$logenabled ) {
ATKPLog::LogError( $e->getMessage() );
}
return $e->getMessage();
}
}
private function convert_response( $response ) {
//return json_decode(json_encode($response), false);
return json_decode( json_encode( (array) simplexml_load_string( $response ) ), 0 );
}
public function set_configuration( $post_id ) {
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_amz_medium_image_size', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_medium_image_size', 'int' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_amz_small_image_size', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_small_image_size', 'int' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_website', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_access_website', 'string' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_tracking_id', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_access_tracking_id', 'string' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_languages_of_preference', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_languages_of_preference', 'string' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_asindataapikey', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_asindataapikey', 'string' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_load_customer_reviews', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_load_customer_reviews', 'bool' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_load_variations', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_load_variations', 'bool' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_onlynew', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_onlynew', 'int' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_key', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_access_key', 'string' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_secret_key', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_access_secret_key', 'string' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_sitestripe', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_sitestripe', 'int' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_seconds_wait', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_seconds_wait', 'int' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_apiversion', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_apiversion', 'string' ) );
// Creators API fields
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_creators_credential_id', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_creators_credential_id', 'string' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_creators_credential_secret', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_creators_credential_secret', 'string' ) );
ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_creators_credential_version', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_creators_credential_version', 'string' ) );
}
private function get_defaultshops( $post_id ) {
$subshops = array();
$website = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_website' );
$subshop = new subshop();
$subshop->title = esc_html__( 'Amazon', 'affiliate-toolkit-starter' );
switch ( $website ) {
default:
$subshop->logourl = plugins_url( 'images/logo-normal-amazon.jpg', ATKP_AMAZON_PLUGIN_FILE );
$subshop->smalllogourl = plugins_url( 'images/logo-small-amazon.jpg', ATKP_AMAZON_PLUGIN_FILE );
break;
case 'de':
$subshop->logourl = plugins_url( 'images/logo-normal-amazon-de.jpg', ATKP_AMAZON_PLUGIN_FILE );
$subshop->smalllogourl = plugins_url( 'images/logo-small-amazon-de.jpg', ATKP_AMAZON_PLUGIN_FILE );
break;
}
$subshop->shopid = $post_id;
$subshop->programid = '';
$subshop->enabled = true;
array_push( $subshops, $subshop );
return $subshops;
}
public function get_configuration( $post ) {
$webservice = ATKPTools::get_post_setting( $post->ID, ATKP_SHOP_POSTTYPE . '_access_webservice' );
$apikey = '';
$apisecretkey = '';
$subshops = null;
if ( $webservice == '1' ) {
$apikey = ATKPTools::get_post_setting( $post->ID, ATKP_SHOP_POSTTYPE . '_access_key' );
$apisecretkey = ATKPTools::get_post_setting( $post->ID, ATKP_SHOP_POSTTYPE . '_access_secret_key' );
}
// Determine the mode: Check if NoAPI plugin is available
$noapi_available = defined( 'ATKP_AMAZNOAPI_ITEM_ID' ) && ATKP_LicenseController::get_module_license_status( 'amaznoapi' ) == 'valid';
$noapi_mode = ATKPTools::get_post_setting( $post->ID, ATKP_SHOP_POSTTYPE . '_sitestripe' );
// Determine which mode to show (API, NoAPI, or Creators)
$use_mode = 'api'; // default to API
if ($noapi_mode == 4 || $noapi_mode == 5) {
$use_mode = 'creators';
} else if ($noapi_available && $noapi_mode == 2) {
$use_mode = 'noapi';
} else if ($noapi_available && !$apikey) {
$use_mode = 'noapi';
}
?>
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
', '' ) ) ?>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ID, ATKP_SHOP_POSTTYPE . '_load_variations' ), true ); ?>>
|
1 ) {
$subshops = $this->get_defaultshops( $post_id );
}
foreach ( $subshops as $subshop ) {
$subshop->shopid = $post_id;
$subshop->programid = '';
$subshop->logourl = $subshop->customlogourl == '' ? $subshop->logourl : $subshop->customlogourl;
$subshop->smalllogourl = $subshop->customsmalllogourl == '' ? $subshop->smalllogourl : $subshop->customsmalllogourl;
$subshop->title = $subshop->customtitle == '' ? $subshop->title : $subshop->customtitle;
$subshop->enabled = true;
}
return $subshops;
}
/* @var Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\api\DefaultApi $helper */
private $helper = null;
private $enable_ssl = false;
private $country = '';
private $load_customer_reviews = false;
private $associateTag = '';
/**
* @var string[]
*/
private $languages_of_preference = '';
private $accessKey = '';
private $shopid = '';
private $smallimagesize = 0;
private $mediumimagesize = 0;
public $sitetripemode = 0;
private $usev5 = 0;
private $load_variations = 0;
private $onlynew = 0;
private $seconds_wait = 0;
private $asindataapikey = '';
/* @var \Amazon\CreatorsAPI\v1\com\amazon\creators\api\DefaultApi $creatorsHelper */
private $creatorsHelper = null;
private $apiMode = 'paapi'; // 'paapi' | 'creators' | 'noapi'
private $credentialId = '';
private $credentialSecret = '';
private $credentialVersion = '';
private $marketplace = '';
private function checklogon_v5( $access_website, $access_key, $access_secret_key, $access_tracking_id ) {
$this->helper = $this->get_api_instance( $access_key, $access_secret_key, $access_website );
}
private function checklogon_creators() {
require_once ATKP_AMAZON_PLUGIN_DIR . '/lib/vendor/autoload.php';
$this->check_guzzle();
$config = new \Amazon\CreatorsAPI\v1\Configuration();
$config->setCredentialId( $this->credentialId );
$config->setCredentialSecret( $this->credentialSecret );
$config->setVersion( $this->credentialVersion );
$this->creatorsHelper = new \Amazon\CreatorsAPI\v1\com\amazon\creators\api\DefaultApi(
new \GuzzleHttp\Client(), $config
);
}
public function checklogon( $shop ) {
$this->shopid = $shop->id;
$this->accessKey = $access_key = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_access_key' );
$access_secret_key = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_access_secret_key' );
$this->country = $access_website = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_access_website' );
$this->associateTag = $access_tracking_id = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_access_tracking_id' );
$lang = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_languages_of_preference' );
$this->languages_of_preference = $lang != '' ? explode( ',', $lang ) : null;
$this->asindataapikey = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_asindataapikey' );
$this->load_variations = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_load_variations' );
$this->enable_ssl = true;
$this->onlynew = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_onlynew' );
$this->seconds_wait = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_seconds_wait' );
if ( $this->seconds_wait <= 0 ) {
$this->seconds_wait = 1;
}
$raw_sitestripe = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_sitestripe' );
// Creators API modes (4, 5) don't require NoAPI license
if ( $raw_sitestripe == 4 || $raw_sitestripe == 5 ) {
$this->sitetripemode = $raw_sitestripe;
$this->load_customer_reviews = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_load_customer_reviews' );
} else if ( ATKP_LicenseController::get_module_license_status( 'amaznoapi' ) == 'valid' &&
ATKP_LicenseController::get_module_license( 'amaznoapi' ) != '' ) {
$this->sitetripemode = $raw_sitestripe;
$this->load_customer_reviews = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_load_customer_reviews' );
}
$this->smallimagesize = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_amz_small_image_size' );
$this->mediumimagesize = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_amz_medium_image_size' );
if ( $this->smallimagesize <= 0 ) {
$this->smallimagesize = 75;
}
if ( $this->mediumimagesize <= 0 ) {
$this->mediumimagesize = 160;
}
//http://ws-eu.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&OneJS=1&Operation=GetAdHtml&MarketPlace=DE&source=ss&ref=as_ss_li_til&ad_type=product_link&tracking_id=werbeanzeige1-21&language=de_DE&marketplace=amazon®ion=DE&placement=B01MR8IST0&asins=B01MR8IST0&linkId=2b2c154e99d12d52b2eeedaca502173f&show_border=true&link_opens_in_new_window=true
//http://ws-eu.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&OneJS=1&Operation=GetAdHtml&MarketPlace=DE&source=ss&ref=as_ss_li_til&ad_type=product_link&tracking_id=werbeanzeige1-21&language=de_DE&marketplace=amazon®ion=DE&placement=B01MR8IST0&asins=B01MR8IST0&linkId=f9c12eaff04df7a156ab2470af8795b6&show_border=false&link_opens_in_new_window=true
//https://www.amazon.de/Anbernic-Handheld-Spielkonsole-Konsole-Retro/dp/B079KC8Y4Z/ref=as_li_ss_il?pf_rd_p=bf2f9e9c-e5d5-4935-a04d-fda59481ccaa&pd_rd_wg=o3sJU&pf_rd_r=2XY13YPN803RR4ST4DQ6&ref_=pd_gw_cr_cartx&pd_rd_w=VtV3b&pd_rd_r=9a23560a-0d57-4b39-bb61-6df20e25109c&linkCode=li3&tag=werbeanzeige1-21&linkId=3ece46f52aef6db73c170a5784594205&language=de_DE" target="_blank">


if ( $access_tracking_id == '' ) {
$access_tracking_id = 'empty';
}
if ( $this->sitetripemode == 4 || $this->sitetripemode == 5 ) {
$this->credentialId = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_creators_credential_id' );
$this->credentialSecret = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_creators_credential_secret' );
$this->credentialVersion = ATKPTools::get_post_setting( $shop->id, ATKP_SHOP_POSTTYPE . '_creators_credential_version' );
require_once ATKP_AMAZON_PLUGIN_DIR . '/includes/atkp_marketplace_mapper.php';
$this->marketplace = atkp_marketplace_mapper::get_marketplace( $access_website );
if ( $this->credentialVersion == '' ) {
$this->credentialVersion = atkp_marketplace_mapper::get_default_version( $access_website );
}
$this->checklogon_creators();
$this->apiMode = 'creators';
} else {
$this->checklogon_v5( $access_website, $access_key, $access_secret_key, $access_tracking_id );
$this->apiMode = 'paapi';
}
}
/**
* Sets itemIdType
*
* @param \Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\BrowseNodeAncestor $browsenodeancestor
*
* @return string $result
*/
private function getBrowseNodeTreeRec( $browsenodeancestor, &$nodes ) {
if ( $browsenodeancestor == null ) {
return '';
}
$nodes[ $browsenodeancestor->getId() ] = $browsenodeancestor->getDisplayName();
if ( $browsenodeancestor->getAncestor() != null ) {
$this->getBrowseNodeTreeRec( $browsenodeancestor->getAncestor(), $nodes );
}
}
private function retrieve_browsenodes_v5( $keyword ) {
$nodes = array();
$items = null;
try {
$searchItemsRequest = new Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsRequest();
$searchItemsRequest->setSearchIndex( 'All' );
$searchItemsRequest->setKeywords( $keyword );
$searchItemsRequest->setItemCount( 10 );
$searchItemsRequest->setPartnerTag( $this->associateTag );
if ( $this->languages_of_preference != null ) {
$searchItemsRequest->setLanguagesOfPreference( $this->languages_of_preference );
}
$searchItemsRequest->setPartnerType( Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\PartnerType::ASSOCIATES );
$searchItemsRequest->setResources(
\Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsResource::getAllowableEnumValues()
);
$searchItemsRequest->setItemPage( 1 );
$searchItemsResponse = $this->sendSearchRequest( $searchItemsRequest );
if ( $searchItemsResponse->getSearchResult() != null && $searchItemsResponse->getSearchResult()->getItems() != null ) {
$items = $searchItemsResponse->getSearchResult()->getItems();
}
} catch ( Amazon\ProductAdvertisingAPI\v1\ApiException $exception ) {
$check = "API-Error: " . $exception->getCode() . " " . $exception->getMessage();
if ( $exception->getResponseObject() instanceof Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\ProductAdvertisingAPIClientException ) {
$errors = $exception->getResponseObject()->getErrors();
foreach ( $errors as $error ) {
$check = "Response-Error: " . $error->getCode() . " " . $error->getMessage();
}
} else {
$check .= "Error response body: " . $exception->getResponseBody();
}
} catch ( Exception $exception ) {
$check = "Error Message: " . $exception->getMessage(); //. ' ' . $exception->getTraceAsString();
}
if ( $items != null ) {
foreach ( $items as $item ) {
foreach ( $item->getBrowseNodeInfo()->getBrowseNodes() as $bnw ) {
$this->getBrowseNodeTreeRec( $bnw->getAncestor(), $nodes );
}
}
}
return $nodes;
}
public function retrieve_browsenodes( $keyword ) {
if ( $this->helper == null && $this->creatorsHelper == null ) {
throw new Exception( 'checklogon required' );
}
if ( $this->apiMode == 'creators' ) {
$nodes = $this->retrieve_browsenodes_creators( $keyword );
} else {
$nodes = $this->retrieve_browsenodes_v5( $keyword );
}
$newNodes = array();
foreach ( $nodes as $node => $value ) {
if ( ! array_key_exists( $node, $newNodes ) ) {
$newNodes[ $node ] = $value;
}
}
return $newNodes;
}
private function retrieve_recursive_browsenodes( $parentBrowseNode ) {
$nodes = array();
if ( isset( $parentBrowseNode->Ancestors ) ) {
foreach ( $parentBrowseNode->Ancestors as $browsenode ) {
if ( ! isset( $browsenode->Name ) || ! is_string( $browsenode->Name ) ) {
continue;
}
$nodes[ $browsenode->BrowseNodeId ] = $browsenode->Name;
foreach ( $this->retrieve_recursive_browsenodes( $browsenode ) as $node => $value ) {
$nodes[ $node ] = $value;
}
//array_push($nodes, $this->RecursiveBrowseNodes($browsenode));
}
}
return $nodes;
}
private function quick_search_v5( $keyword, $searchType, $pagination ) {
$products = new atkp_search_resp();
$maxCount = 10;
if ( $this->sitetripemode == 2) {
if ( $searchType == 'product' || $searchType == 'asin' ) {
$products = $this->search_sitestripeproduct( $keyword, $searchType, $pagination );
} else {
$products->message = esc_html__( 'Search and import not supported. You enabled "sitestripe mode" in your amazon shop.', 'affiliate-toolkit-starter' );
}
return $products;
}
$items = array();
try {
//$searchType == 'ean'
if ( $searchType == 'asin' || $searchType == 'articlenumber' ) {
$getItemsRequest = new Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\GetItemsRequest();
$getItemsRequest->setItemIds( explode( ',', $keyword ) );
$getItemsRequest->setItemIdType( ( $searchType == 'ean' ? 'EAN' : 'ASIN' ) );
$getItemsRequest->setPartnerTag( $this->associateTag );
if ( $this->languages_of_preference != null )
$getItemsRequest->setLanguagesOfPreference( $this->languages_of_preference);
$getItemsRequest->setPartnerType( \Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\PartnerType::ASSOCIATES );
$getItemsRequest->setResources(
\Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\GetItemsResource::getAllowableEnumValues()
);
$getItemsResponse = $this->sendGetItemsRequest( $getItemsRequest );
if ( $getItemsResponse->getItemsResult() != null && $getItemsResponse->getItemsResult()->getItems() != null ) {
$items = $getItemsResponse->getItemsResult()->getItems();
}
} else {
$searchItemsRequest = new Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsRequest();
$searchItemsRequest->setSearchIndex( 'All' );
$searchItemsRequest->setKeywords( $keyword );
$searchItemsRequest->setItemCount( $maxCount );
$searchItemsRequest->setPartnerTag( $this->associateTag );
if ( $this->languages_of_preference != null )
$searchItemsRequest->setLanguagesOfPreference( $this->languages_of_preference);
$searchItemsRequest->setPartnerType( Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\PartnerType::ASSOCIATES );
$searchItemsRequest->setResources(
\Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsResource::getAllowableEnumValues()
);
$searchItemsRequest->setItemPage( $pagination );
$searchItemsResponse = $this->sendSearchRequest( $searchItemsRequest );
if ( $searchItemsResponse->getSearchResult() != null ) {
$products->pagecount = ceil( floatval( $searchItemsResponse->getSearchResult()->getTotalResultCount() ) / floatval( $maxCount ) );
$products->total = intval( $searchItemsResponse->getSearchResult()->getTotalResultCount() );
}
$products->currentpage = intval( $pagination );
if ( $searchItemsResponse->getSearchResult() != null && $searchItemsResponse->getSearchResult()->getItems() != null ) {
$items = $searchItemsResponse->getSearchResult()->getItems();
}
}
} catch ( Amazon\ProductAdvertisingAPI\v1\ApiException $exception ) {
$check = "API-Error: " . $exception->getCode() . " " . $exception->getMessage();
if ( $exception->getResponseObject() instanceof Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\ProductAdvertisingAPIClientException ) {
$errors = $exception->getResponseObject()->getErrors();
foreach ( $errors as $error ) {
$check = "Response-Error: " . $error->getCode() . " " . $error->getMessage();
}
} else {
$check .= "Error response body: " . $exception->getResponseBody();
}
} catch ( Exception $exception ) {
$check = "Error Message: " . $exception->getMessage(); //. ' ' . $exception->getTraceAsString();
}
if ( ! empty( $check ) ) {
throw new Exception( esc_html__( $check, 'affiliate-toolkit-starter' ) );
}
foreach ( $items as $result ) {
if ( $result->getASIN() != null ) {
$product = array();
if ( $result->getImages() != null && $result->getImages()->getPrimary() != null ) {
$product['imageurl'] = $this->checkimageurl( $result->getImages()->getPrimary()->getSmall()->getURL(), 'small' );
}
$product['articlenumber'] = $product['asin'] = $result->getASIN();
//var_dump($result->getExternalIds());exit;
$ean_full = '';
if ( $result->getItemInfo() != null && $result->getItemInfo()->getExternalIds() != null && $result->getItemInfo()->getExternalIds()->getEANs() != null ) {
foreach ( $result->getItemInfo()->getExternalIds()->getEANs()->getDisplayValues() as $ean ) {
if ( $ean_full != '' ) {
$ean_full .= ',';
}
$ean_full .= $ean;
}
}
$product['ean'] = $ean_full;
if ( $result->getDetailPageURL() != null ) {
$product['producturl'] = $result->getDetailPageURL();
}
if ( $result->getItemInfo() != null && $result->getItemInfo()->getTitle() != null && $result->getItemInfo()->getTitle()->getDisplayValue() != null ) {
$product['title'] = htmlspecialchars( $result->getItemInfo()->getTitle()->getDisplayValue() );
}
if ( $result->getOffersV2() != null ) {
foreach ( $result->getOffersV2()->getListings() as $listing ) {
if ( $listing->getAvailability() != null && $listing->getAvailability()->getMessage() != null ) {
$product['availability'] = $listing->getAvailability()->getMessage();
}
if ( $listing->getPrice() != null ) {
// OffersV2 hat eine verschachtelte Money-Struktur
$price = $listing->getPrice();
// SavingBasis für listprice
if ( $price->getSavingBasis() != null ) {
$savingBasis = $price->getSavingBasis();
// Prüfe ob SavingBasis das neue Money-Objekt hat
if ( $savingBasis->getMoney() != null ) {
$product['listprice'] = $savingBasis->getMoney()->getDisplayAmount();
$product['listpricefloat'] = $savingBasis->getMoney()->getAmount();
}
} else {
$product['listprice'] = '';
$product['listpricefloat'] = 0;
}
// Preis aus dem Money-Objekt holen
if ( $price->getMoney() != null ) {
$product['saleprice'] = $price->getMoney()->getDisplayAmount();
$product['salepricefloat'] = $price->getMoney()->getAmount();
}
// PricePerUnit (baseprice) - nur OffersV2 Struktur
if ( $price->getPricePerUnit() != null ) {
$pricePerUnit = $price->getPricePerUnit();
if ( $pricePerUnit->getAmount() != null ) {
$product['basepricefloat'] = $pricePerUnit->getAmount();
// Extrahiere Preis und Einheit aus dem displayAmount
if ( $pricePerUnit->getDisplayAmount() != null ) {
$parts = explode(' / ', $pricePerUnit->getDisplayAmount());
$product['baseprice'] = trim($parts[0]);
if ( count($parts) > 1 ) {
$product['baseunit'] = trim($parts[1]);
}
}
}
}
// Savings
if ( $price->getSavings() != null ) {
$savings = $price->getSavings();
if ( $savings->getPercentage() != null ) {
$product['percentagesaved'] = $savings->getPercentage();
}
if ( $savings->getMoney() != null ) {
$product['amountsaved'] = $savings->getMoney()->getDisplayAmount();
$product['amountsavedfloat'] = $savings->getMoney()->getAmount();
}
}
break;
}
}
}
$description = '';
if ( $result->getItemInfo() != null && $result->getItemInfo()->getFeatures() != null && $result->getItemInfo()->getFeatures()->getDisplayValues() != null ) {
$description = implode( '
', $result->getItemInfo()->getFeatures()->getDisplayValues() );
}
$product['features'] = $description != '' && strlen( $description ) > 350 ? substr( $description, 0, 350 ) : $description;
//$product['availability'] = $result->Offers->Offer->OfferListing->Availability;
array_push( $products->products, $product );
}
}
return $products;
}
public function quick_search( $keyword, $searchType, $pagination = 1 ) {
if ( $this->helper == null && $this->creatorsHelper == null ) {
throw new Exception( 'checklogon required' );
}
if ( $this->apiMode == 'creators' ) {
return $this->quick_search_creators( $keyword, $searchType, $pagination );
}
// try {
$products = $this->quick_search_v5( $keyword, $searchType, $pagination );
// } catch ( Exception $exception ) {
//
// if ( ATKPTools::str_contains( $exception->getMessage(), 'The request was denied due to request throttling.', false ) ) {
// sleep( ATKP_AMZ_WAIT );
// $products = $this->quick_search_v5( $keyword, $searchType, $pagination );
// } else {
// throw $exception;
// }
// }
return $products;
}
private function checkurl( $url, $enable_ssl = null ) {
if ( $enable_ssl == null ) {
$enable_ssl = $this->enable_ssl;
}
if ( $enable_ssl ) {
$url = str_replace( 'http://', 'https://', $url );
}
return $url;
}
private function checkimageurl( $url, $size ) {
//if ( $this->enable_ssl ) {
// $url = str_replace( 'http://ecx.images-amazon.com', 'https://images-na.ssl-images-amazon.com', $url );
//}
if ( $size == 'small' && $this->smallimagesize > 0 ) {
$url = str_replace( 'SL75', 'SL' . $this->smallimagesize, $url );
}
if ( $size == 'medium' && $this->mediumimagesize > 0 ) {
$url = str_replace( 'SL160', 'SL' . $this->mediumimagesize, $url );
}
return $url;
}
private function checkimageurl_sitestripe( $url, $size ) {
//if ( $this->enable_ssl ) {
// $url = str_replace( 'http://ecx.images-amazon.com', 'https://images-na.ssl-images-amazon.com', $url );
//}
if ( $size == 'small' && $this->smallimagesize > 0 ) {
$url = str_replace( '._AC_AC_SR98,95_', '.SL' . $this->smallimagesize, $url );
}
if ( $size == 'medium' && $this->mediumimagesize > 0 ) {
$url = str_replace( '._AC_AC_SR98,95_', '.SL' . $this->mediumimagesize, $url );
}
if ( $size == 'large' ) {
$url = str_replace( '._AC_AC_SR98,95_', '', $url );
}
return $url;
}
private function checkResponse( $response ) {
$requestHelp = null;
if ( isset( $response->BrowseNodes->Request ) ) {
$requestHelp = $response->BrowseNodes->Request;
} else if ( isset( $response->Items->Request ) ) {
$requestHelp = $response->Items->Request;
}
//echo('$response: ' .serialize($response));
$message = '';
if ( isset( $requestHelp->IsValid ) && $requestHelp->IsValid != 'True' ) {
$message .= 'Invalid Request. IsValid: ' . $requestHelp->IsValid;
//echo('xx '.serialize($requestHelp->Errors->Error));
}
if ( isset( $requestHelp->Errors->Error ) ) {
if ( isset( $requestHelp->Errors->Error->Code ) && $requestHelp->Errors->Error->Code != '' ) {
$error = $requestHelp->Errors->Error;
if ( $message != '' ) {
$message .= ' ';
}
$message .= 'ErrorCode: ' . $error->Code;
if ( $message != '' ) {
$message .= ' ';
}
$message .= 'Message: ' . $error->Message;
} else {
foreach ( $requestHelp->Errors->Error as $error ) {
if ( $message != '' ) {
$message .= ' ';
}
$message .= 'ErrorCode: ' . $error->Code;
if ( $message != '' ) {
$message .= ' ';
}
$message .= 'Message: ' . $error->Message;
}
}
}
return $message;
}
private function parse_department_file( $filename ) {
$departments = array();
if ( ( $handle = fopen( ATKP_AMAZON_PLUGIN_DIR . '/files/' . $filename, "r" ) ) !== false ) {
while ( ( $data = fgetcsv( $handle, 1000, ";" ) ) !== false ) {
$departments[ $data[0] ] = array(
'caption' => $data[1],
'sortvalues' => array(
'AvgCustomerReviews' => esc_html__( 'Sorts results according to average customer reviews', 'affiliate-toolkit-starter' ),
'Featured' => esc_html__( 'Sorts results with featured items having higher rank', 'affiliate-toolkit-starter' ),
'NewestArrivals' => esc_html__( 'Sorts results with according to newest arrivals', 'affiliate-toolkit-starter' ),
'Price:HighToLow' => esc_html__( 'Sorts results according to most expensive to least expensive', 'affiliate-toolkit-starter' ),
'Price:LowToHigh' => esc_html__( 'Sorts results according to least expensive to most expensive', 'affiliate-toolkit-starter' ),
'Relevance' => esc_html__( 'Sorts results with relevant items having higher rank', 'affiliate-toolkit-starter' ),
)
);
}
fclose( $handle );
}
return $departments;
}
private function retrieve_departments_v5() {
switch ( $this->country ) {
case 'de':
return $this->parse_department_file( 'germany.csv' );
break;
default:
case 'en':
return $this->parse_department_file( 'unitedstates.csv' );
break;
case 'co.uk':
return $this->parse_department_file( 'unitedkingdom.csv' );
break;
case 'ca':
return $this->parse_department_file( 'canada.csv' );
break;
case 'fr':
return $this->parse_department_file( 'france.csv' );
break;
case 'com.be':
return $this->parse_department_file( 'belgium.csv' );
break;
case 'co.jp':
return $this->parse_department_file( 'japan.csv' );
break;
case 'it':
return $this->parse_department_file( 'italy.csv' );
break;
case 'cn':
case 'es':
return $this->parse_department_file( 'spain.csv' );
break;
case 'in':
return $this->parse_department_file( 'india.csv' );
break;
case 'au':
return $this->parse_department_file( 'australia.csv' );
break;
case 'com.br':
return $this->parse_department_file( 'brazil.csv' );
break;
case 'com.mx':
return $this->parse_department_file( 'mexico.csv' );
break;
case 'com.tr':
return $this->parse_department_file( 'turkey.csv' );
break;
case 'ae':
return $this->parse_department_file( 'emirates.csv' );
break;
case 'pl':
return $this->parse_department_file( 'poland.csv' );
break;
case 'nl':
return $this->parse_department_file( 'netherlands.csv' );
break;
}
}
public function retrieve_departments() {
if ( $this->helper == null && $this->creatorsHelper == null ) {
throw new Exception( 'checklogon required' );
}
$departments = $this->retrieve_departments_v5();
return $departments;
}
private function retrieve_filters_v5() {
$durations = array(
'' => esc_html__( 'Not selected', 'affiliate-toolkit-starter' ),
'Actor' => esc_html__( 'Actor', 'affiliate-toolkit-starter' ),
'Artist' => esc_html__( 'Artist', 'affiliate-toolkit-starter' ),
'Author' => esc_html__( 'Author', 'affiliate-toolkit-starter' ),
'Availability' => esc_html__( 'Availability', 'affiliate-toolkit-starter' ),
'Brand' => esc_html__( 'Brand', 'affiliate-toolkit-starter' ),
'BrowseNode' => esc_html__( 'BrowseNode', 'affiliate-toolkit-starter' ),
'Condition' => esc_html__( 'Condition', 'affiliate-toolkit-starter' ),
'CurrencyOfPreference' => esc_html__( 'Currency Of Preference', 'affiliate-toolkit-starter' ),
'DeliveryFlags' => esc_html__( 'DeliveryFlags', 'affiliate-toolkit-starter' ),
'LanguagesOfPreference' => esc_html__( 'Languages Of Preference', 'affiliate-toolkit-starter' ),
'Marketplace' => esc_html__( 'Marketplace', 'affiliate-toolkit-starter' ),
'MaximumPrice' => esc_html__( 'Maximum price', 'affiliate-toolkit-starter' ),
'MinimumPrice' => esc_html__( 'Minimum price', 'affiliate-toolkit-starter' ),
'MerchantId' => esc_html__( 'Merchant Id', 'affiliate-toolkit-starter' ),
'MinReviewsRating' => esc_html__( 'Min Reviews Rating', 'affiliate-toolkit-starter' ),
'MinPercentageOff' => esc_html__( 'Min percentage off', 'affiliate-toolkit-starter' ),
'Keywords' => esc_html__( 'Keywords', 'affiliate-toolkit-starter' ),
'SearchIndex' => esc_html__( 'SearchIndex', 'affiliate-toolkit-starter' ),
'Sort' => esc_html__( 'Sort', 'affiliate-toolkit-starter' ),
'Title' => esc_html__( 'Title', 'affiliate-toolkit-starter' ),
);
return $durations;
}
public function retrieve_filters() {
$durations = $this->retrieve_filters_v5();
return $durations;
}
public function retrieve_products( $asins, $id_type = 'ASIN' ) {
if ( $this->apiMode == 'creators' ) {
return $this->retrieve_products_creators( $asins, $id_type );
}
//try {
return $this->retrieve_products_v5( $asins, $id_type );
// } catch ( Exception $exception ) {
//
// if ( ATKPTools::str_contains( $exception->getMessage(), 'The request was denied due to request throttling.', false ) ) {
// sleep( ATKP_AMZ_WAIT );
//
// return $this->retrieve_products_v5( $asins, $id_type );
// } else {
// throw $exception;
// }
// }
}
private $second_try_api = false;
private function search_sitestripeproduct( $keyword, $searchType, $pagination ) {
$license = ATKP_LicenseController::get_module_license( 'amaznoapi' );
try {
if($searchType == 'asin'){
$url = 'https://api.affiliate-toolkit.com/amazon/noapi.php?asin=' . $keyword . '&tag=' . $this->associateTag . '&country=' . strtoupper( $this->country ) . '&key=' . $license;
}else {
$url = 'https://api.affiliate-toolkit.com/amazon/noapi.php?keywords=' . urlencode( $keyword ) . '&tag=' . $this->associateTag . '&country=' . strtoupper( $this->country ) . '&key=' . $license . '&page_number=' . $pagination;
}
if ( $this->asindataapikey != '' )
$url .= '&apikey=' . $this->asindataapikey;
$page = '';
$statusCode = null;
if ( function_exists( 'wp_remote_get' ) ) {
$response = wp_remote_get( $url, array( 'timeout' => 20 ) );
if ( function_exists( 'is_wp_error' ) && is_wp_error( $response ) ) {
$products = new atkp_search_resp();
$products->message = 'API request failed: ' . $response->get_error_message() . ' / ' . esc_url( $url );
return $products;
}
if ( isset( $response['response']['code'] ) ) {
$statusCode = $response['response']['code'];
}
if ( isset( $response['body'] ) ) {
$page = $response['body'];
}
}
$products = new atkp_search_resp();
$products_x = array();
$products->currentpage = 0;
$products->pagecount = 0;
$products->total = 0;
if ( 200 == $statusCode ) {
$xx = json_decode( $page );
if ( $xx != null && isset( $xx->data->errormessage ) && $xx->data->errormessage != '' ) {
$products = new atkp_search_resp();
$products->message = $xx->data->errormessage . ' / ' . esc_url( $url );
return $products;
}
if ( $xx != null && isset( $xx->products ) ) {
foreach ( $xx->products as $p ) {
$xxd = array();
$xxd['imageurl'] = $p->imageurl;
$xxd['asin'] = $p->asin;
//$product['ean'] = $ean_full;
$xxd['producturl'] = $p->producturl;
$xxd['title'] = $p->title;
//$product['availability'] = $listing->getAvailability();
$xxd['saleprice'] = isset( $p->saleprice ) ? $p->saleprice : '';
$xxd['listprice'] = '';
$products_x[] = $xxd;
}
} else if ( $xx != null && isset( $xx->productitem ) && $xx->productitem != null ) {
$p = $xx->productitem->data;
$xxd = array();
$xxd['imageurl'] = isset( $p->mediumimageurl ) ? $p->mediumimageurl : '';
$xxd['asin'] = isset( $p->asin ) ? $p->asin : $keyword;
$xxd['producturl'] = isset( $p->producturl ) ? $p->producturl : '';
$xxd['title'] = isset( $p->title ) ? $p->title : '';
$xxd['saleprice'] = isset( $p->saleprice ) ? $p->saleprice : '';
$xxd['listprice'] = isset( $p->listprice ) ? $p->listprice : '';
$products_x[] = $xxd;
}
$products->products = $products_x;
if ( isset( $xx->data->currentpage ) ) {
$products->currentpage = intval( $xx->data->currentpage );
$products->pagecount = intval( $xx->data->pagecount );
$products->total = intval( $xx->data->total );
}
} else {
if ( ! $this->second_try_api ) {
$this->second_try_api = true;
sleep( 1 );
return $this->search_sitestripeproduct( $keyword, $searchType, $pagination );
}
$products = new atkp_search_resp();
if ( $statusCode == 429 ) {
$products->message = __( 'The query limit has been reached.', 'affiliate-toolkit-starter' );
} else {
$products->message = 'Invalid Status code: ' . $statusCode . ' / ' . esc_url( $url ) . ' / ' . substr( $page, 0, 200 ) . ' / Please try again.';
}
return $products;
}
return $products;
} catch ( Exception $e ) {
$titlecheck = $e->getMessage();
$products = new atkp_search_resp();
$products->message = $titlecheck;
return $products;
}
}
/**
* @param $asins
*
* @return atkp_response
*/
private function load_sitestripeproduct( $asins ) {
$atkpresponse = new atkp_response();
//https://ws-eu.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&OneJS=1&Operation=GetAdHtml&MarketPlace=DE&ad_type=product_link&tracking_id=werbeanzeige1-21&marketplace=amazon®ion=DE&asins=B07H2BRGPS
$license = ATKP_LicenseController::get_module_license( 'amaznoapi');
foreach ( $asins as $asin ) {
try {
$url = 'https://api.affiliate-toolkit.com/amazon/noapi.php?asin=' . $asin . '&tag=' . $this->associateTag . '&country=' . strtoupper( $this->country ) . '&key=' . $license;
if ( $this->asindataapikey != '' )
$url .= '&apikey=' . $this->asindataapikey;
$page = '';
if ( function_exists( 'wp_remote_get' ) ) {
$response = wp_remote_get( $url, array(
'timeout' => 20,
) );
$statusCode = null;
if ( function_exists( 'is_wp_error' ) && ! is_wp_error( $response ) ) {
// Success
if ( isset( $response['response']['code'] ) ) {
$statusCode = $response['response']['code'];
}
if ( '200' == $statusCode ) {
$page = $response['body'];
}
}
if ( $statusCode != 200 ) {
$product = new atkp_response_item();
$product->uniqueid = $asin;
$product->uniquetype = 'ASIN';
array_push( $atkpresponse->responseitems, $product );
if ( $statusCode == 429 ) {
$product->errormessage = __( 'The query limit has been reached.', 'affiliate-toolkit-starter' );
} else {
$product->errormessage = 'Invalid Status code: ' . $statusCode . ' / ' . esc_url( $url ) . ' / Please try again.';
}
array_push( $atkpresponse->responseitems, $product );
}
}
$xx = json_decode($page );
if ( $xx != null && isset( $xx->data->errormessage ) && $xx->data->errormessage != '' ) {
$product = new atkp_response_item();
$product->errormessage = 'product error: ' . $xx->data->errormessage . ' / ' . esc_url( $url );
$product->uniqueid = $asin;
$product->uniquetype = 'ASIN';
array_push( $atkpresponse->responseitems, $product );
} else {
if ( $xx != null && $xx->productitem != null ) {
$myproduct = new atkp_product();
foreach ( $xx->productitem->data as $key => $val ) {
$myproduct->$key = $val;
}
$myproduct->updatedon = ATKPTools::get_currenttime();
$myproduct->shopid = $this->shopid;
$myproduct->asin = $asin;
$product = new atkp_response_item();
if ( isset( $xx->errormessage ) ) {
$product->errormessage = $xx->errormessage;
}
$product->uniqueid = $asin;
$product->uniquetype = 'ASIN';
$product->productitem = $myproduct;
array_push( $atkpresponse->responseitems, $product );
}
}
} catch ( Exception $e ) {
if ( ! $this->second_try_api ) {
$this->second_try_api = true;
sleep( 1 );
return $this->load_sitestripeproduct( $asins );
}
$titlecheck = $e->getMessage();
$product = new atkp_response_item();
$product->errormessage = 'product error: ' . $titlecheck;
$product->uniqueid = $asin;
$product->uniquetype = 'ASIN';
array_push( $atkpresponse->responseitems, $product );
}
}
return $atkpresponse;
}
public function retrieve_products_v5( $asins, $id_type ) {
$atkpresponse = new atkp_response();
if ( count( $asins ) == 0 ) {
return $atkpresponse;
}
switch ( strtoupper($id_type) ) {
case 'TITLE':
case "EAN":
if ( $this->sitetripemode == 2) {
//the No-API mode can only load products by ASIN
foreach ( $asins as $title ) {
$responseitem = new atkp_response_item();
$responseitem->errormessage = 'The No-API mode can only load products by ASIN. A lookup by ' . strtoupper( $id_type ) . ' requires the Amazon API.';
$responseitem->uniqueid = $title;
$responseitem->uniquetype = $id_type;
$atkpresponse->responseitems[] = $responseitem;
}
return $atkpresponse;
}
foreach ( $asins as $title ) {
$items = null;
$titlecheck = '';
try {
$searchItemsRequest = new Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsRequest();
$searchItemsRequest->setSearchIndex( 'All' );
$searchItemsRequest->setKeywords( $title );
$searchItemsRequest->setItemCount( 2 );
if ( $this->languages_of_preference != null )
$searchItemsRequest->setLanguagesOfPreference( $this->languages_of_preference);
$searchItemsRequest->setPartnerTag( $this->associateTag );
$searchItemsRequest->setPartnerType( Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\PartnerType::ASSOCIATES );
$searchItemsRequest->setResources(
\Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsResource::getAllowableEnumValues()
);
$searchItemsRequest->setItemPage( 1 );
$searchItemsResponse = $this->sendSearchRequest( $searchItemsRequest );
if ( $searchItemsResponse->getSearchResult() != null && $searchItemsResponse->getSearchResult()->getItems() != null ) {
$items = $searchItemsResponse->getSearchResult()->getItems();
}
} catch ( Amazon\ProductAdvertisingAPI\v1\ApiException $exception ) {
$titlecheck = "API-Error: " . $exception->getCode() . " " . $exception->getMessage();
if ( $exception->getResponseObject() instanceof Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\ProductAdvertisingAPIClientException ) {
$errors = $exception->getResponseObject()->getErrors();
foreach ( $errors as $error ) {
$titlecheck = "Response-Error: " . $error->getCode() . " " . $error->getMessage();
}
} else {
$titlecheck .= "Error response body: " . $exception->getResponseBody();
}
} catch ( Exception $exception ) {
//NoResults
if ( !ATKPTools::str_contains( $exception->getMessage(), 'NoResults: No results found for your request.', false ) ) {
$titlecheck = "Error Message: " . $exception->getMessage(); //. ' ' . $exception->getTraceAsString();
}
}
$added = false;
if ( $titlecheck != '') {
$responseitem = new atkp_response_item();
$responseitem->errormessage = $titlecheck;
$responseitem->uniqueid = $title;
$responseitem->uniquetype = $id_type;
array_push( $atkpresponse->responseitems, $responseitem );
$added = true;
} else {
if ( $items != null ) {
foreach ( $items as $result2 ) {
if ( $result2->getASIN() == null || $added ) {
continue;
}
$result = $result2;
break;
}
if ( $result != null ) {
$responseitem = new atkp_response_item();
$responseitem->productitem = $this->fill_product_v5( $result );
$responseitem->uniqueid = $title;
$responseitem->uniquetype = $id_type;
array_push( $atkpresponse->responseitems, $responseitem );
$added = true;
}
}
}
}
break;
case 'ARTICLENUMBER':
case 'ASIN':
if ( $this->sitetripemode == 2 ) {
//bevorzugte nutzung, die artikelnummer ist bei amazon die ASIN
$atkpresponse = $this->load_sitestripeproduct( $asins );
foreach ( $atkpresponse->responseitems as $responseitem ) {
$responseitem->uniquetype = $id_type;
}
return $atkpresponse;
}
foreach ( $asins as $asin ) {
$items = array();
$check = '';
try {
$getItemsRequest = new Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\GetItemsRequest();
$getItemsRequest->setItemIds( array( $asin ) );
$getItemsRequest->setItemIdType( ( 'ASIN' ) );
if ( $this->languages_of_preference != null )
$getItemsRequest->setLanguagesOfPreference( $this->languages_of_preference);
$getItemsRequest->setPartnerTag( $this->associateTag );
$getItemsRequest->setPartnerType( \Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\PartnerType::ASSOCIATES );
$getItemsRequest->setResources(
\Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\GetItemsResource::getAllowableEnumValues()
);
$getItemsResponse = $this->sendGetItemsRequest( $getItemsRequest );
if ( $getItemsResponse->getItemsResult() != null && $getItemsResponse->getItemsResult()->getItems() != null ) {
$items = $getItemsResponse->getItemsResult()->getItems();
}
} catch ( Amazon\ProductAdvertisingAPI\v1\ApiException $exception ) {
$check = "API-Error: " . $exception->getCode() . " " . $exception->getMessage();
if ( $exception->getResponseObject() instanceof Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\ProductAdvertisingAPIClientException ) {
$errors = $exception->getResponseObject()->getErrors();
foreach ( $errors as $error ) {
$check = "Response-Error: " . $error->getCode() . " " . $error->getMessage();
}
} else {
$check .= "Error response body: " . $exception->getResponseBody();
}
} catch ( Exception $exception ) {
$check = "Error Message: " . $exception->getMessage(); //. ' ' . $exception->getTraceAsString();
}
$added = false;
if ( ! empty( $check ) && $this->sitetripemode == 3 && strtoupper($id_type) == 'ASIN' ) {
$atkpresponse2 = $this->load_sitestripeproduct( array( $asin ) );
array_push( $atkpresponse->responseitems, $atkpresponse2->responseitems[0] );
$added = true;
} else if ( $check != '' || $items == null ) {
$responseitem = new atkp_response_item();
$responseitem->errormessage = empty( $check ) ? 'product not found' : $check;
$responseitem->uniqueid = $asin;
$responseitem->uniquetype = $id_type;
array_push( $atkpresponse->responseitems, $responseitem );
$added = true;
} else {
if ( $items != null ) {
$result = null;
foreach ( $items as $result2 ) {
if ( $result2->getASIN() == null || $added ) {
continue;
}
$result = $result2;
break;
}
if ( $result != null ) {
$responseitem = new atkp_response_item();
$responseitem->productitem = $this->fill_product_v5( $result );
$responseitem->uniqueid = $asin;
$responseitem->uniquetype = $id_type;
array_push( $atkpresponse->responseitems, $responseitem );
$added = true;
}
}
}
}
break;
default:
throw new Exception( esc_html__( 'unknown id_type: ' . $id_type, 'affiliate-toolkit-starter' ) );
break;
}
return $atkpresponse;
}
/**
* Sets itemIdType
*
* @param atkp_product $myproduct
* @param \Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\Item $result
*
* @return $myproduct atkp_product
*/
public function load_variations_v5( $myproduct, $result ) {
$variations = array();
$dimmension = array();
$parentasin = $result->getParentASIN();
if ( $parentasin == '' ) {
//try to load the variations
try {
$getItemsRequest = new Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\GetVariationsRequest();
$getItemsRequest->setASIN( $result->getASIN() );
$getItemsRequest->setVariationCount( 10 );
$getItemsRequest->setPartnerTag( $this->associateTag );
if ( $this->languages_of_preference != null )
$getItemsRequest->setLanguagesOfPreference( $this->languages_of_preference);
$getItemsRequest->setPartnerType( \Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\PartnerType::ASSOCIATES );
$getItemsRequest->setResources(
\Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\GetVariationsResource::getAllowableEnumValues()
);
$getItemsResponse = $this->sendVariationRequest( $getItemsRequest );
if ( $getItemsResponse->getVariationsResult() != null && $getItemsResponse->getVariationsResult()->getItems() != null ) {
foreach ( $getItemsResponse->getVariationsResult()->getItems() as $variationItem ) {
$att = $variationItem->getVariationAttributes();
foreach ( $att as $tmp ) {
$dimmension[ $tmp['name'] ] = apply_filters( 'atkp_variation_name', $tmp['name'] );
}
$varpd = $this->fill_product_v5( $variationItem, $result );
$dimmfullname = array();
foreach ( $att as $tmp ) {
$dimmfullname[ $tmp['name'] ] = $tmp['value'];
}
$varpd->variationname = $dimmfullname;
array_push( $variations, $varpd );
}
}
} catch ( Exception $x ) {
}
}
$myproduct->variationname = $dimmension;
$myproduct->variations = $variations;
return $myproduct;
}
private function allowedCondition($condition) {
//Any Offer Listings for items across any condition
//New Offer Listings for New items
//Used Offer Listings for Used items
//Collectible Offer Listings for Collectible items
//Refurbished Offer Listings for Certified Refurbished items
return true;
/*
if($condition == '' || $condition == null)
return true;
if(ATKPTools::str_contains($condition, 'New', true)) {
return true;
} else if($this->onlynew)
return false;
else
return true;
*/
}
/**
* Sets itemIdType
*
* @param \Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\Item $result
*
* @return $myproduct atkp_product
*/
private function fill_product_v5( $result, $parentResult = null ) {
if ( $result == null ) {
return null;
}
$myproduct = new atkp_product();
$myproduct->updatedon = ATKPTools::get_currenttime();
$myproduct->shopid = $this->shopid;
//store the ASIN code in case we need it
$myproduct->asin = $result->getASIN();
$myproduct->parentasin = $result->getParentASIN();
//TODO: Variationen einbauen mit optionsfeld im shop
if ( $parentResult == null && $this->load_variations ) {
$myproduct = $this->load_variations_v5( $myproduct, $result );
}
if ( $result->getDetailPageURL() != null ) {
$myproduct->producturl = urldecode( $result->getDetailPageURL() );
//für 100% im titel - replace muss geklärt werden..
$myproduct->producturl = str_replace( '%', '%25', $myproduct->producturl );
$myproduct->addtocarturl = $this->checkurl( 'http://www.amazon.' . $this->country . '/gp/aws/cart/add.html?AWSAccessKeyId=' . $this->accessKey . '&AssociateTag=' . $this->associateTag . '&ASIN.1=' . $myproduct->asin . '&Quantity.1=1' );
} else if ( $parentResult != null && $parentResult->getDetailPageURL() != null ) {
$myproduct->producturl = urldecode( $parentResult->getDetailPageURL() );
//für 100% im titel - replace muss geklärt werden..
$myproduct->producturl = str_replace( '%', '%25', $myproduct->producturl );
$myproduct->producturl = $this->checkurl( 'https://www.amazon.' . $this->country . '/dp/' . $myproduct->asin . '?tag=' . $this->associateTag );
$myproduct->addtocarturl = $this->checkurl( 'https://www.amazon.' . $this->country . '/gp/aws/cart/add.html?AWSAccessKeyId=' . $this->accessKey . '&AssociateTag=' . $this->associateTag . '&ASIN.1=' . $parentResult->ASIN . '&Quantity.1=1' );
} else {
$myproduct->producturl = '';
$myproduct->addtocarturl = '';
}
$images = array();
if ( $result->getImages() != null ) {
if ( $result->getImages()->getPrimary() != null ) {
$myproduct->smallimageurl = $this->checkimageurl( $result->getImages()->getPrimary()->getSmall()->getURL(), 'small' );
$myproduct->mediumimageurl = $this->checkimageurl( $result->getImages()->getPrimary()->getMedium()->getURL(), 'medium' );
$myproduct->largeimageurl = $this->checkimageurl( $result->getImages()->getPrimary()->getLarge()->getURL(), 'large' );
}
if ( $result->getImages()->getVariants() != null ) {
foreach ( $result->getImages()->getVariants() as $variant ) {
if ( $variant->getLarge() == null && $variant->getMedium() == null && $variant->getSmall() == null ) {
continue;
}
$udf = new atkp_product_image();
$udf->id = uniqid();
if ( $variant->getSmall() != null ) {
$udf->smallimageurl = $this->checkimageurl( $variant->getSmall()->getURL(), 'small' );
}
if ( $variant->getMedium() != null ) {
$udf->mediumimageurl = $this->checkimageurl( $variant->getMedium()->getURL(), 'medium' );
}
if ( $variant->getLarge() != null ) {
$udf->largeimageurl = $this->checkimageurl( $variant->getLarge()->getURL(), 'large' );
}
array_push( $images, $udf );
}
}
}
$myproduct->images = $images;
if($result->getCustomerReviews() != null) {
if($result->getCustomerReviews()->getStarRating() != null)
$myproduct->rating = $result->getCustomerReviews()->getStarRating()->getValue();
if($result->getCustomerReviews() != null)
$myproduct->reviewcount = $result->getCustomerReviews()->getCount();
}
if ( $this->load_customer_reviews && $myproduct->reviewcount == 0) {
$averageRating = 0;
$totalReviews = 0;
$this->get_customer_rating_api( $myproduct->asin, $averageRating, $totalReviews );
$myproduct->rating = $averageRating;
$myproduct->reviewcount = $totalReviews;
}
$myproduct->customerreviewurl = $this->checkurl( 'http://www.amazon.' . $this->country . '/product-reviews/' . $myproduct->asin . '/?tag=' . $this->associateTag );
$description = '';
$features = '';
if ( $result->getItemInfo() != null && $result->getItemInfo()->getFeatures() != null && $result->getItemInfo()->getFeatures()->getDisplayValues() != null ) {
foreach ( $result->getItemInfo()->getFeatures()->getDisplayValues() as $feature ) {
$features .= '' . $feature . '';
$description .= $feature.'
';
}
}
$myproduct->features = $features == '' ? '' : '';
$myproduct->description = $description;
if ( $result->getItemInfo() != null && $result->getItemInfo()->getTitle() != null && $result->getItemInfo()->getTitle()->getDisplayValue() != null ) {
$myproduct->title = htmlentities( $result->getItemInfo()->getTitle()->getDisplayValue() );
} else {
$myproduct->title = '';
}
if($result->getItemInfo() != null && $result->getItemInfo()->getProductInfo() != null) {
$productInfo = $result->getItemInfo()->getProductInfo();
if($productInfo->getColor() != null)
$myproduct->customfields["a_color"] = $productInfo->getColor()->getDisplayValue();
if($productInfo->getItemDimensions() != null) {
if( $productInfo->getItemDimensions()->getHeight() != null)
$myproduct->customfields["a_height"] = round($productInfo->getItemDimensions()->getHeight()->getDisplayValue(), 2). ' '.($productInfo->getItemDimensions()->getHeight()->getUnit());
if($productInfo->getItemDimensions()->getLength() != null)
$myproduct->customfields["a_length"] = round($productInfo->getItemDimensions()->getLength()->getDisplayValue(),2). ' '.($productInfo->getItemDimensions()->getLength()->getUnit());
if($productInfo->getItemDimensions()->getWidth() != null)
$myproduct->customfields["a_width"] = round($productInfo->getItemDimensions()->getWidth()->getDisplayValue(), 2). ' '.($productInfo->getItemDimensions()->getWidth()->getUnit());
if($productInfo->getItemDimensions()->getWeight() != null)
$myproduct->customfields["a_weight"] = round($productInfo->getItemDimensions()->getWeight()->getDisplayValue(), 2) . ' '.($productInfo->getItemDimensions()->getWeight()->getUnit());
}
}
//preise laden
// Prüfe zuerst OffersV2 (neue API), dann Offers (alte API) als Fallback
$offersObject = null;
$isOffersV2 = false;
if ( $result->getOffersV2() != null ) {
$offersObject = $result->getOffersV2();
$isOffersV2 = true;
} else if ( $result->getOffers() != null ) {
$offersObject = $result->getOffers();
$isOffersV2 = false;
}
if ( $offersObject != null ) {
$offerlisting = null;
if($offersObject->getListings() != null) {
foreach ( $offersObject->getListings() as $listing ) {
if ( $listing->getIsBuyBoxWinner() &&
$this->allowedCondition( $listing->getCondition() == null ? null : $listing->getCondition()->getValue() ) ) {
$offerlisting = $listing;
break;
}
}
if ( $offerlisting == null) {
$listings = array();
foreach ( $offersObject->getListings() as $list ) {
if ( $this->allowedCondition( $list->getCondition() == null ? null : $list->getCondition()->getValue() ) ) {
$listings[] = $list;
}
}
$offerlisting = reset( $listings );
}
}
$myproduct->iswarehouse = false;
if ( $offerlisting && $offerlisting != null ) {
if($offerlisting->getDeliveryInfo() != null) {
$myproduct->isprime = $offerlisting->getDeliveryInfo()->getIsPrimeEligible();
if ( $offerlisting->getDeliveryInfo()->getShippingCharges() != null && count( $offerlisting->getDeliveryInfo()->getShippingCharges() ) > 0 ) {
$myproduct->shipping = $offerlisting->getDeliveryInfo()->getShippingCharges()[0]->getDisplayAmount();
}
}
if($offerlisting->getMerchantInfo() != null) {
if($offerlisting->getMerchantInfo()->getName() == 'Amazon Warehouse') {
$myproduct->iswarehouse = true;
}
}
// Helper-Funktion für OffersV2 Money-Struktur
$getDisplayAmount = function($priceObject) use ($isOffersV2) {
if ($priceObject == null) return '';
if ($isOffersV2 && $priceObject->getMoney() != null) {
return $priceObject->getMoney()->getDisplayAmount();
}
return $priceObject->getDisplayAmount();
};
$getAmount = function($priceObject) use ($isOffersV2) {
if ($priceObject == null) return 0;
if ($isOffersV2 && $priceObject->getMoney() != null) {
return $priceObject->getMoney()->getAmount();
}
return $priceObject->getAmount();
};
// Baseprice und PricePerUnit für OffersV2
$pricePerUnit = 0;
$pricePerUnitDisplay = '';
if ($offerlisting->getPrice() != null && $offerlisting->getPrice()->getPricePerUnit() != null) {
$pricePerUnitObj = $offerlisting->getPrice()->getPricePerUnit();
if ($isOffersV2 && is_object($pricePerUnitObj) && method_exists($pricePerUnitObj, 'getAmount')) {
// OffersV2: PricePerUnit hat direkt amount und displayAmount
$pricePerUnit = $pricePerUnitObj->getAmount();
$pricePerUnitDisplay = $pricePerUnitObj->getDisplayAmount();
}
}
if($pricePerUnit > 0) {
$myproduct->basepricefloat = $pricePerUnit;
// Extrahiere Preis (vor /) und Einheit (nach /) aus displayAmount
$parts = explode(' / ', $pricePerUnitDisplay);
$myproduct->baseprice = trim($parts[0]);
if(count($parts) > 1) {
$myproduct->baseunit = trim($parts[1]);
}
}
$myproduct->saleprice = $getDisplayAmount($offerlisting->getPrice());
$myproduct->salepricefloat = $getAmount($offerlisting->getPrice());
$myproduct->unitpricefloat = $pricePerUnit;
if ( $offerlisting->getPrice() != null && $offerlisting->getPrice()->getSavings() != null ) {
$savings = $offerlisting->getPrice()->getSavings();
$myproduct->percentagesaved = $savings->getPercentage();
// OffersV2 hat Money-Objekt in Savings
if ($isOffersV2 && $savings->getMoney() != null) {
$myproduct->amountsaved = $savings->getMoney()->getDisplayAmount();
$myproduct->amountsavedfloat = $savings->getMoney()->getAmount();
} else {
$myproduct->amountsaved = $savings->getDisplayAmount();
$myproduct->amountsavedfloat = $savings->getAmount();
}
}
if ( $offerlisting->getPrice() != null && $offerlisting->getPrice()->getSavingBasis() != null ) {
$savingBasis = $offerlisting->getPrice()->getSavingBasis();
// OffersV2 hat ein OffersV2SavingBasis-Objekt mit Money
if ($isOffersV2 && is_object($savingBasis) && method_exists($savingBasis, 'getMoney') && $savingBasis->getMoney() != null) {
$myproduct->listprice = $savingBasis->getMoney()->getDisplayAmount();
$myproduct->listpricefloat = $savingBasis->getMoney()->getAmount();
} else if (is_object($savingBasis) && method_exists($savingBasis, 'getDisplayAmount')) {
// Alte Offers-Struktur (SavingBasis ist ein OfferPrice-Objekt)
$myproduct->listprice = $savingBasis->getDisplayAmount();
$myproduct->listpricefloat = $savingBasis->getAmount();
}
}
if ( $offerlisting->getAvailability() != null ) {
/*
if(ATKPTools::str_contains($offerlisting->getAvailability()->getMessage() ,'Nicht auf Lager', false)) {
//preis = 0 ?
$myproduct->saleprice = '';
$myproduct->salepricefloat = 0;
}*/
$myproduct->availability = $offerlisting->getAvailability()->getMessage();
}
}
}
//$myproduct->salepricefloat = $this->price_to_float( $myproduct->saleprice );
//$myproduct->amountsavedfloat = $this->price_to_float( $myproduct->amountsaved );
//$myproduct->listpricefloat = $this->price_to_float( $myproduct->listprice );
$myproduct->shippingfloat = (float) 0;
if ( $result->getItemInfo() != null && $result->getItemInfo()->getByLineInfo() != null && $result->getItemInfo()->getByLineInfo()->getManufacturer() != null ) {
$myproduct->manufacturer = $result->getItemInfo()->getByLineInfo()->getManufacturer()->getDisplayValue();
}
if ( $result->getItemInfo() != null && $result->getItemInfo()->getByLineInfo() != null && $result->getItemInfo()->getByLineInfo()->getBrand() != null ) {
$myproduct->brand = $result->getItemInfo()->getByLineInfo()->getBrand()->getDisplayValue();
}
$isbn_full = '';
if ( $result->getItemInfo() != null && $result->getItemInfo()->getExternalIds() != null && $result->getItemInfo()->getExternalIds()->getISBNs() != null ) {
foreach ( $result->getItemInfo()->getExternalIds()->getISBNs()->getDisplayValues() as $ean ) {
if ( $isbn_full != '' ) {
$isbn_full .= ',';
}
$isbn_full .= $ean;
}
}
$myproduct->isbn = $isbn_full;
$ean_full = '';
if ( $result->getItemInfo() != null && $result->getItemInfo()->getExternalIds() != null && $result->getItemInfo()->getExternalIds()->getEANs() != null ) {
foreach ( $result->getItemInfo()->getExternalIds()->getEANs()->getDisplayValues() as $ean ) {
if ( $ean_full != '' ) {
$ean_full .= ',';
}
$ean_full .= $ean;
}
}
$myproduct->ean = $ean_full;
$category = '';
if ( $result->getBrowseNodeInfo() != null && $result->getBrowseNodeInfo()->getBrowseNodes() != null ) {
foreach ( $result->getBrowseNodeInfo()->getBrowseNodes() as $bnw ) {
$category .= $this->getBrowseNodeTree( $bnw->getAncestor() );
break;
}
}
$myproduct->productgroup = $category;
if ( $result->getItemInfo() != null && $result->getItemInfo()->getProductInfo() && $result->getItemInfo()->getProductInfo()->getReleaseDate() ) {
$myproduct->releasedate = substr( $result->getItemInfo()->getProductInfo()->getReleaseDate()->getDisplayValue(), 0, 10 );
}
if ( $result->getItemInfo() != null && $result->getItemInfo()->getByLineInfo() != null && $result->getItemInfo()->getByLineInfo()->getContributors() != null ) {
foreach ( $result->getItemInfo()->getByLineInfo()->getContributors() as $const ) {
if ( $const->getRole() == 'Autor' ) {
$myproduct->author = $const->getName();
break;
}
}
}
if ( $result->getItemInfo() != null && $result->getItemInfo()->getContentInfo() != null && $result->getItemInfo()->getContentInfo()->getPagesCount() != null ) {
$myproduct->numberofpages = $result->getItemInfo()->getContentInfo()->getPagesCount()->getDisplayValue();
}
$myproduct->mpn = '';
if ( $result->getItemInfo() != null && $result->getItemInfo()->getManufactureInfo() != null && $result->getItemInfo()->getManufactureInfo()->getItemPartNumber() != null ) {
$myproduct->mpn = $result->getItemInfo()->getManufactureInfo()->getItemPartNumber()->getDisplayValue();
}
if ( count( $myproduct->variations ) > 0 && $myproduct->salepricefloat == 0 ) {
foreach ( $myproduct->variations as $variation ) {
if ( $variation->salepricefloat > 0 ) {
$myproduct->listprice = $variation->listprice;
$myproduct->amountsaved = $variation->amountsaved;
$myproduct->saleprice = $variation->saleprice;
$myproduct->listpricefloat = $variation->listpricefloat;
$myproduct->amountsavedfloat = $variation->amountsavedfloat;
$myproduct->percentagesaved = $variation->percentagesaved;
$myproduct->salepricefloat = $variation->salepricefloat;
$myproduct->shippingfloat = $variation->shippingfloat;
$myproduct->availability = $variation->availability;
$myproduct->shipping = $variation->shipping;
$myproduct->isprime = $variation->isprime;
$myproduct->iswarehouse = $variation->iswarehouse;
$myproduct->smallimageurl = $variation->smallimageurl;
$myproduct->mediumimageurl = $variation->mediumimageurl;
$myproduct->largeimageurl = $variation->largeimageurl;
break;
}
}
}
if ($myproduct->salepricefloat == 0 && $this->sitetripemode == 3 && $myproduct->asin != '') {
//bevorzugte nutzung
$atkpresponse = $this->load_sitestripeproduct( array($myproduct->asin) );
if(count($atkpresponse->responseitems) > 0) {
$myproduct->saleprice = $atkpresponse->responseitems[0]->productitem->saleprice;
$myproduct->salepricefloat = $atkpresponse->responseitems[0]->productitem->salepricefloat;
}
}
return $myproduct;
}
/**
* Sets itemIdType
*
* @param \Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\BrowseNodeAncestor $browsenodeinfo
*
* @return string $result
*/
private function getBrowseNodeTree( $browsenodeancestor ) {
if ( $browsenodeancestor == null ) {
return '';
}
$result = '';
if ( $browsenodeancestor->getAncestor() != null ) {
$result .= ( $this->getBrowseNodeTree( $browsenodeancestor->getAncestor() ) ) . ' > ';
}
$result .= $browsenodeancestor->getDisplayName();
return $result;
}
private function get_customer_rating_api($asin, &$averageRating, &$totalReviews) {
$p = $this->load_sitestripeproduct(array($asin));
if(count($p->responseitems) > 0) {
$averageRating = $p->responseitems[0]->productitem->rating;
$totalReviews = $p->responseitems[0]->productitem->reviewcount;
}
}
private function setCondition($request) {
switch($this->onlynew) {
default:
//$request->setCondition(\Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\Condition::ANY);
break;
case 1:
$request->setCondition(\Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\Condition::_NEW);
break;
case 2:
$request->setCondition(\Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\Condition::USED);
break;
case 3:
$request->setCondition(\Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\Condition::COLLECTIBLE);
break;
case 4:
$request->setCondition(\Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\Condition::REFURBISHED);
break;
}
return $request;
}
private function sendSearchRequest( $searchItemsRequest ) {
$searchItemsRequest = $this->setCondition($searchItemsRequest);
$this->validate_request_v5( $searchItemsRequest );
if ( $this->seconds_wait > 0 ) {
sleep( $this->seconds_wait );
}
//try {
$searchItemsResponse = $this->helper->searchItems( $searchItemsRequest );
// } catch ( Exception $exception ) {
// if ( ATKPTools::str_contains( $exception->getMessage(), '429 Too Many Requests', false ) ) {
//
// sleep( ATKP_AMZ_WAIT );
// $searchItemsResponse = $this->helper->searchItems( $searchItemsRequest );
// } else {
// throw $exception;
// }
// }
$this->validate_response_v5( $searchItemsResponse );
return $searchItemsResponse;
}
private function sendVariationRequest( $variationRequest ) {
$this->validate_request_v5( $variationRequest );
if ( $this->seconds_wait > 0 ) {
sleep( $this->seconds_wait );
}
//try {
$getItemsResponse = $this->helper->getVariations( $variationRequest );
// } catch ( Exception $exception ) {
// if ( ATKPTools::str_contains( $exception->getMessage(), '429 Too Many Requests', false ) ) {
// sleep( ATKP_AMZ_WAIT );
// $getItemsResponse = $this->helper->getVariations( $variationRequest );
// } else {
// throw $exception;
// }
// }
$this->validate_response_v5( $getItemsResponse );
return $getItemsResponse;
}
private function sendGetItemsRequest( $getItemsRequest ) {
$searchItemsRequest = $this->setCondition($getItemsRequest);
$this->validate_request_v5( $getItemsRequest );
if ( $this->seconds_wait > 0 ) {
sleep( $this->seconds_wait );
}
//try {
$getItemsResponse = $this->helper->getItems( $getItemsRequest );
// } catch ( Exception $exception ) {
// if ( ATKPTools::str_contains( $exception->getMessage(), '429 Too Many Requests', false ) ) {
// sleep( ATKP_AMZ_WAIT );
// $getItemsResponse = $this->helper->getItems( $getItemsRequest );
// } else {
// throw $exception;
// }
// }
$this->validate_response_v5( $getItemsResponse );
return $getItemsResponse;
}
public function retrieve_product_list( $search_request ) {
if ( $this->helper == null && $this->creatorsHelper == null ) {
throw new Exception( 'checklogon required' );
}
if ( $this->apiMode == 'creators' ) {
return $this->retrieve_product_list_creators( $search_request );
}
$mylist = new atkp_list_resp();
$mylist->updatedon = ATKPTools::get_currenttime();
$mylist->asins = array();
$mylist->products = null;
if ( $this->sitetripemode == 2 && $this->accessKey == '' ) {
//lists are only available through the Amazon API, the No-API mode can only load single products by ASIN
$mylist->message = 'Lists are not supported in No-API mode. Enter the credentials of the Amazon API or switch the shop to the Creators API.';
return $mylist;
}
switch ( $search_request->request_type ) {
case atkp_list_request_type::TopSellers:
case atkp_list_request_type::NewReleases:
$searchItemsRequest = new Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsRequest();
//$searchItemsRequest->setSearchIndex( 'All' );
$searchItemsRequest->setSortBy( $search_request->request_type == atkp_list_request_type::TopSellers ? 'Featured' : 'NewestArrivals' );
$searchItemsRequest->setBrowseNodeId( $search_request->category );
$searchItemsRequest->setKeywords( "*" );
if ( $this->languages_of_preference != null )
$searchItemsRequest->setLanguagesOfPreference( $this->languages_of_preference);
$searchItemsRequest->setPartnerTag( $this->associateTag );
$searchItemsRequest->setPartnerType( Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\PartnerType::ASSOCIATES );
$searchItemsRequest->setResources(
\Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsResource::getAllowableEnumValues()
);
$searchItemsRequest->setItemCount( 10 );
$searchItemsRequest->setItemPage( 1 );
$searchItemsResponse = $this->sendSearchRequest( $searchItemsRequest );
$products = array();
if ( $searchItemsResponse->getSearchResult() != null) {
$mylist->total_items_count = $searchItemsResponse->getSearchResult()->getTotalResultCount();
$mylist->total_pages = ceil( $mylist->total_items_count / $this->get_maximum_items_per_page() );
if ($searchItemsResponse->getSearchResult()->getItems() != null ) {
$items = $searchItemsResponse->getSearchResult()->getItems();
foreach ( $items as $item ) {
if ( $item->getASIN() != null ) {
$products[] = $this->fill_product_v5( $item );
}
}
}
}
$mylist->products = $products;
break;
case atkp_list_request_type::ExtendedSearch:
case atkp_list_request_type::Search:
$searchItemsRequest = new Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsRequest();
if ( $search_request->category != '' && $search_request->category != 'All' ) {
$searchItemsRequest->setSearchIndex( $search_request->category );
if ( $search_request->sort_order != '' ) {
//AvgCustomerReviews', 'Featured', 'NewestArrivals', 'Price:HighToLow', 'Price:LowToHigh', 'Relevance
switch ( $search_request->sort_order ) {
case '-pubdate':
case '-publication_date':
case 'date-desc-rank':
case 'launch_date':
$sortorder = 'NewestArrivals';
break;
case 'popularity-rank':
case 'relevancerank':
case 'salesrank':
case 'psrank':
case 'titlerank':
case '-titlerank':
case '-unit-sales':
default:
$sortorder = 'Relevance';
break;
case 'reviewrank':
case 'pmrank':
case 'reviewrank_authority':
case 'review-rank':
$sortorder = 'AvgCustomerReviews';
break;
case 'price':
case 'price-asc-rank':
case 'pricerank':
$sortorder = 'Price:LowToHigh';
break;
case '-price':
case 'price-desc-rank':
case 'inverse-pricerank':
$sortorder = 'Price:HighToLow';
break;
case 'featured':
$sortorder = 'Featured';
break;
case 'AvgCustomerReviews':
case 'Featured':
case 'NewestArrivals':
case 'Price:HighToLow':
case 'Price:LowToHigh':
case 'Relevance':
break;
}
$searchItemsRequest->setSortBy( $sortorder );
}
}
//TODO: Filterfelder ergänzen
$keyword = $search_request->keyword;
if ( $search_request->filter != null ) {
foreach ( $search_request->filter as $field => $value ) {
switch ( $field ) {
case 'Keywords':
$keyword = $value;
break;
case 'SearchIndex':
$searchItemsRequest->setSearchIndex( $value );
break;
case 'Sort':
$searchItemsRequest->setSortBy( $value );
break;
case 'Actor':
$searchItemsRequest->setActor( $value );
break;
case 'Artist':
$searchItemsRequest->setArtist( $value );
break;
case 'Author':
$searchItemsRequest->setAuthor( $value );
break;
case 'Availability':
$searchItemsRequest->setAvailability( $value );
break;
case 'Brand':
$searchItemsRequest->setBrand( $value );
break;
case 'Condition':
$searchItemsRequest->setCondition( $value );
break;
case 'DeliveryFlags':
$searchItemsRequest->setDeliveryFlags( explode(',', $value) );
break;
case 'CurrencyOfPreference':
$searchItemsRequest->setCurrencyOfPreference( $value );
break;
case 'LanguagesOfPreference':
$searchItemsRequest->setLanguagesOfPreference( $value );
break;
case 'Marketplace':
$searchItemsRequest->setMarketplace( $value );
break;
case 'MaximumPrice':
$searchItemsRequest->setMaxPrice( floatval($value) );
break;
case 'MinimumPrice':
$searchItemsRequest->setMinPrice( floatval($value) );
break;
case 'MerchantId':
$searchItemsRequest->setMerchant( $value );
break;
case 'MinPercentageOff':
$searchItemsRequest->setMinSavingPercent( intval($value) );
break;
case 'MinReviewsRating':
$searchItemsRequest->setMinReviewsRating( intval($value) );
break;
case 'Title':
$searchItemsRequest->setTitle( $value );
break;
case 'BrowseNode':
$searchItemsRequest->setBrowseNodeId( $value );
break;
}
}
}
if ( $keyword != '' ) {
$keywords = explode( ',', $keyword );
if ( $keywords != null && count( $keywords ) > 1 ) {
$searchItemsRequest->setKeywords( $keywords );
} else {
$searchItemsRequest->setKeywords( $keyword );
}
}
if ( $this->languages_of_preference != null )
$searchItemsRequest->setLanguagesOfPreference( $this->languages_of_preference);
$searchItemsRequest->setPartnerTag( $this->associateTag );
$searchItemsRequest->setPartnerType( Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\PartnerType::ASSOCIATES );
$searchItemsRequest->setResources(
\Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsResource::getAllowableEnumValues()
);
$init_page = 1;
if($search_request->items_per_page > 0) {
$searchItemsRequest->setItemCount( $search_request->items_per_page );
$init_page = $search_request->page;
$pages = $search_request->page;
}else {
$itemsperpage = $this->get_maximum_items_per_page() > $search_request->max_count ? $search_request->max_count : $this->get_maximum_items_per_page();
$pages = ceil( $search_request->max_count / $itemsperpage );
$searchItemsRequest->setItemCount( intval( $itemsperpage ) );
}
$products = array();
for ( $x = $init_page; $x <= $pages; $x ++ ) {
$searchItemsRequest->setItemPage( $x );
$searchItemsResponse = $this->sendSearchRequest( $searchItemsRequest );
if($searchItemsResponse->getSearchResult() != null) {
$mylist->total_items_count = $searchItemsResponse->getSearchResult()->getTotalResultCount();
$mylist->total_pages = ceil( $mylist->total_items_count / $this->get_maximum_items_per_page() );
if ($searchItemsResponse->getSearchResult()->getItems() != null ) {
$items = $searchItemsResponse->getSearchResult()->getItems();
foreach ( $items as $item ) {
if ( $item->getASIN() != null ) {
$products[] = $this->fill_product_v5( $item );
if ( $search_request->items_per_page == 0 && count( $products ) >= $search_request->max_count ) {
break;
}
}
}
if ( count( $items ) < $this->get_maximum_items_per_page() || ($search_request->items_per_page == 0 && count( $products ) >= $search_request->max_count )) {
break;
}
}
}
}
$mylist->products = $products;
break;
default:
$mylist->message = 'unknown request_type: ' . $search_request->request_type ;
break;
}
return $mylist;
}
public function get_maximum_items_per_page() {
return 10;
}
public function get_maximum_pages() {
return 10;
}
public function retrieve_list( $requestType, $nodeid, $keyword, $asin, $maxCount, $sortByOrder, $filters ) {
$my_request = new atkp_list_req();
$my_request->keyword = $keyword;
$my_request->request_type = $requestType;
$my_request->max_count = $maxCount;
$my_request->filter = $filters;
$my_request->category = $nodeid;
$my_request->sort_order = $sortByOrder;
return $this->retrieve_product_list($my_request);
}
public function get_supportedlistsources() {
return implode(',', array(atkp_list_source_type::BestSeller,atkp_list_source_type::NewReleases, atkp_list_source_type::Search, atkp_list_source_type::ExtendedSearch));
}
// ========================================================================
// Creators API Methods
// ========================================================================
private function setConditionCreators( $request ) {
switch ( $this->onlynew ) {
default:
break;
case 1:
$request->setCondition( \Amazon\CreatorsAPI\v1\com\amazon\creators\model\Condition::_NEW );
break;
// Creators API only supports Any and New
}
return $request;
}
private function sendSearchRequestCreators( $searchRequest ) {
$searchRequest = $this->setConditionCreators( $searchRequest );
if ( $this->seconds_wait > 0 ) {
sleep( $this->seconds_wait );
}
$response = $this->creatorsHelper->searchItems( $this->marketplace, $searchRequest );
if ( $response->getErrors() != null ) {
throw new Exception( $response->getErrors()[0]->getCode() . ': ' . $response->getErrors()[0]->getMessage() );
}
return $response;
}
private function sendGetItemsRequestCreators( $getItemsRequest ) {
$getItemsRequest = $this->setConditionCreators( $getItemsRequest );
if ( $this->seconds_wait > 0 ) {
sleep( $this->seconds_wait );
}
$response = $this->creatorsHelper->getItems( $this->marketplace, $getItemsRequest );
if ( $response->getErrors() != null ) {
throw new Exception( $response->getErrors()[0]->getCode() . ': ' . $response->getErrors()[0]->getMessage() );
}
return $response;
}
private function sendVariationRequestCreators( $variationRequest ) {
if ( $this->seconds_wait > 0 ) {
sleep( $this->seconds_wait );
}
$response = $this->creatorsHelper->getVariations( $this->marketplace, $variationRequest );
if ( $response->getErrors() != null ) {
throw new Exception( $response->getErrors()[0]->getCode() . ': ' . $response->getErrors()[0]->getMessage() );
}
return $response;
}
private function quick_search_creators( $keyword, $searchType, $pagination ) {
$products = new atkp_search_resp();
$maxCount = 10;
$items = array();
try {
if ( $searchType == 'asin' || $searchType == 'articlenumber' ) {
$getItemsRequest = new \Amazon\CreatorsAPI\v1\com\amazon\creators\model\GetItemsRequestContent();
$getItemsRequest->setItemIds( explode( ',', $keyword ) );
$getItemsRequest->setPartnerTag( $this->associateTag );
if ( $this->languages_of_preference != null )
$getItemsRequest->setLanguagesOfPreference( $this->languages_of_preference );
$getItemsRequest->setResources(
\Amazon\CreatorsAPI\v1\com\amazon\creators\model\GetItemsResource::getAllowableEnumValues()
);
$getItemsResponse = $this->sendGetItemsRequestCreators( $getItemsRequest );
if ( $getItemsResponse->getItemsResult() != null && $getItemsResponse->getItemsResult()->getItems() != null ) {
$items = $getItemsResponse->getItemsResult()->getItems();
}
} else {
$searchItemsRequest = new \Amazon\CreatorsAPI\v1\com\amazon\creators\model\SearchItemsRequestContent();
$searchItemsRequest->setSearchIndex( 'All' );
$searchItemsRequest->setKeywords( $keyword );
$searchItemsRequest->setItemCount( $maxCount );
$searchItemsRequest->setPartnerTag( $this->associateTag );
if ( $this->languages_of_preference != null )
$searchItemsRequest->setLanguagesOfPreference( $this->languages_of_preference );
$searchItemsRequest->setResources(
\Amazon\CreatorsAPI\v1\com\amazon\creators\model\SearchItemsResource::getAllowableEnumValues()
);
$searchItemsRequest->setItemPage( $pagination );
$searchItemsResponse = $this->sendSearchRequestCreators( $searchItemsRequest );
if ( $searchItemsResponse->getSearchResult() != null ) {
$products->pagecount = ceil( floatval( $searchItemsResponse->getSearchResult()->getTotalResultCount() ) / floatval( $maxCount ) );
$products->total = intval( $searchItemsResponse->getSearchResult()->getTotalResultCount() );
}
$products->currentpage = intval( $pagination );
if ( $searchItemsResponse->getSearchResult() != null && $searchItemsResponse->getSearchResult()->getItems() != null ) {
$items = $searchItemsResponse->getSearchResult()->getItems();
}
}
} catch ( \Amazon\CreatorsAPI\v1\ApiException $exception ) {
$check = "API-Error: " . $exception->getCode();
$responseBody = $exception->getResponseBody();
if ( $responseBody != null ) {
$decoded = json_decode( $responseBody, true );
if ( is_array( $decoded ) ) {
if ( ! empty( $decoded['message'] ) ) {
$check .= " Message: " . $decoded['message'];
}
if ( ! empty( $decoded['reason'] ) ) {
$check .= " Reason: " . $decoded['reason'];
}
if ( ! empty( $decoded['type'] ) ) {
$check .= " Type: " . $decoded['type'];
}
} else {
$check .= " " . $exception->getMessage();
}
} else {
$check .= " " . $exception->getMessage();
}
} catch ( Exception $exception ) {
$check = "Error Message: " . $exception->getMessage();
}
if ( ! empty( $check ) ) {
throw new Exception( esc_html__( $check, 'affiliate-toolkit-starter' ) );
}
foreach ( $items as $result ) {
if ( $result->getAsin() != null ) {
$product = array();
if ( $result->getImages() != null && $result->getImages()->getPrimary() != null ) {
$product['imageurl'] = $this->checkimageurl( $result->getImages()->getPrimary()->getSmall()->getUrl(), 'small' );
}
$product['articlenumber'] = $product['asin'] = $result->getAsin();
$ean_full = '';
if ( $result->getItemInfo() != null && $result->getItemInfo()->getExternalIds() != null && $result->getItemInfo()->getExternalIds()->getEANs() != null ) {
foreach ( $result->getItemInfo()->getExternalIds()->getEANs()->getDisplayValues() as $ean ) {
if ( $ean_full != '' ) {
$ean_full .= ',';
}
$ean_full .= $ean;
}
}
$product['ean'] = $ean_full;
if ( $result->getDetailPageURL() != null ) {
$product['producturl'] = $result->getDetailPageURL();
}
if ( $result->getItemInfo() != null && $result->getItemInfo()->getTitle() != null && $result->getItemInfo()->getTitle()->getDisplayValue() != null ) {
$product['title'] = htmlspecialchars( $result->getItemInfo()->getTitle()->getDisplayValue() );
}
if ( $result->getOffersV2() != null ) {
foreach ( $result->getOffersV2()->getListings() as $listing ) {
if ( $listing->getAvailability() != null && $listing->getAvailability()->getMessage() != null ) {
$product['availability'] = $listing->getAvailability()->getMessage();
}
if ( $listing->getPrice() != null ) {
$price = $listing->getPrice();
if ( $price->getSavingBasis() != null ) {
$savingBasis = $price->getSavingBasis();
if ( $savingBasis->getMoney() != null ) {
$product['listprice'] = $savingBasis->getMoney()->getDisplayAmount();
$product['listpricefloat'] = $savingBasis->getMoney()->getAmount();
}
} else {
$product['listprice'] = '';
$product['listpricefloat'] = 0;
}
if ( $price->getMoney() != null ) {
$product['saleprice'] = $price->getMoney()->getDisplayAmount();
$product['salepricefloat'] = $price->getMoney()->getAmount();
}
if ( $price->getPricePerUnit() != null ) {
$pricePerUnit = $price->getPricePerUnit();
if ( $pricePerUnit->getAmount() != null ) {
$product['basepricefloat'] = $pricePerUnit->getAmount();
if ( $pricePerUnit->getDisplayAmount() != null ) {
$parts = explode( ' / ', $pricePerUnit->getDisplayAmount() );
$product['baseprice'] = trim( $parts[0] );
if ( count( $parts ) > 1 ) {
$product['baseunit'] = trim( $parts[1] );
}
}
}
}
if ( $price->getSavings() != null ) {
$savings = $price->getSavings();
if ( $savings->getPercentage() != null ) {
$product['percentagesaved'] = $savings->getPercentage();
}
if ( $savings->getMoney() != null ) {
$product['amountsaved'] = $savings->getMoney()->getDisplayAmount();
$product['amountsavedfloat'] = $savings->getMoney()->getAmount();
}
}
}
if ( $result->getItemInfo() != null && $result->getItemInfo()->getFeatures() != null && $result->getItemInfo()->getFeatures()->getDisplayValues() != null ) {
$features = '';
foreach ( $result->getItemInfo()->getFeatures()->getDisplayValues() as $feature ) {
$features .= $feature . ' ';
}
$product['features'] = mb_substr( $features, 0, 350 );
}
break;
}
}
$products->products[] = $product;
}
}
return $products;
}
public function retrieve_products_creators( $asins, $id_type ) {
$atkpresponse = new atkp_response();
if ( count( $asins ) == 0 ) {
return $atkpresponse;
}
switch ( strtoupper( $id_type ) ) {
case 'TITLE':
case 'EAN':
foreach ( $asins as $title ) {
$items = null;
$titlecheck = '';
try {
$searchRequest = new \Amazon\CreatorsAPI\v1\com\amazon\creators\model\SearchItemsRequestContent();
$searchRequest->setSearchIndex( 'All' );
$searchRequest->setKeywords( $title );
$searchRequest->setItemCount( 2 );
$searchRequest->setPartnerTag( $this->associateTag );
if ( $this->languages_of_preference != null )
$searchRequest->setLanguagesOfPreference( $this->languages_of_preference );
$searchRequest->setResources(
\Amazon\CreatorsAPI\v1\com\amazon\creators\model\SearchItemsResource::getAllowableEnumValues()
);
$searchRequest->setItemPage( 1 );
$searchResponse = $this->sendSearchRequestCreators( $searchRequest );
if ( $searchResponse->getSearchResult() != null && $searchResponse->getSearchResult()->getItems() != null ) {
$items = $searchResponse->getSearchResult()->getItems();
}
} catch ( \Amazon\CreatorsAPI\v1\ApiException $exception ) {
$titlecheck = "API-Error: " . $exception->getCode();
$responseBody = $exception->getResponseBody();
if ( $responseBody != null ) {
$decoded = json_decode( $responseBody, true );
if ( is_array( $decoded ) ) {
if ( ! empty( $decoded['message'] ) ) {
$titlecheck .= " Message: " . $decoded['message'];
}
if ( ! empty( $decoded['reason'] ) ) {
$titlecheck .= " Reason: " . $decoded['reason'];
}
if ( ! empty( $decoded['type'] ) ) {
$titlecheck .= " Type: " . $decoded['type'];
}
} else {
$titlecheck .= " " . $exception->getMessage();
}
} else {
$titlecheck .= " " . $exception->getMessage();
}
} catch ( Exception $exception ) {
if ( ! ATKPTools::str_contains( $exception->getMessage(), 'NoResults', false ) ) {
$titlecheck = "Error Message: " . $exception->getMessage();
}
}
$added = false;
if ( $titlecheck != '' ) {
$responseitem = new atkp_response_item();
$responseitem->errormessage = $titlecheck;
$responseitem->uniqueid = $title;
$responseitem->uniquetype = $id_type;
array_push( $atkpresponse->responseitems, $responseitem );
$added = true;
} else {
if ( $items != null ) {
$result = null;
foreach ( $items as $result2 ) {
if ( $result2->getAsin() == null || $added ) {
continue;
}
$result = $result2;
break;
}
if ( $result != null ) {
$responseitem = new atkp_response_item();
$responseitem->productitem = $this->fill_product_creators( $result );
$responseitem->uniqueid = $title;
$responseitem->uniquetype = $id_type;
array_push( $atkpresponse->responseitems, $responseitem );
$added = true;
}
}
}
}
break;
case 'ARTICLENUMBER':
case 'ASIN':
foreach ( $asins as $asin ) {
$items = array();
$check = '';
try {
$getItemsRequest = new \Amazon\CreatorsAPI\v1\com\amazon\creators\model\GetItemsRequestContent();
$getItemsRequest->setItemIds( array( $asin ) );
$getItemsRequest->setPartnerTag( $this->associateTag );
if ( $this->languages_of_preference != null )
$getItemsRequest->setLanguagesOfPreference( $this->languages_of_preference );
$getItemsRequest->setResources(
\Amazon\CreatorsAPI\v1\com\amazon\creators\model\GetItemsResource::getAllowableEnumValues()
);
$getItemsResponse = $this->sendGetItemsRequestCreators( $getItemsRequest );
if ( $getItemsResponse->getItemsResult() != null && $getItemsResponse->getItemsResult()->getItems() != null ) {
$items = $getItemsResponse->getItemsResult()->getItems();
}
} catch ( \Amazon\CreatorsAPI\v1\ApiException $exception ) {
$check = "API-Error: " . $exception->getCode();
$responseBody = $exception->getResponseBody();
if ( $responseBody != null ) {
$decoded = json_decode( $responseBody, true );
if ( is_array( $decoded ) ) {
if ( ! empty( $decoded['message'] ) ) {
$check .= " Message: " . $decoded['message'];
}
if ( ! empty( $decoded['reason'] ) ) {
$check .= " Reason: " . $decoded['reason'];
}
if ( ! empty( $decoded['type'] ) ) {
$check .= " Type: " . $decoded['type'];
}
} else {
$check .= " " . $exception->getMessage();
}
} else {
$check .= " " . $exception->getMessage();
}
} catch ( Exception $exception ) {
$check = "Error Message: " . $exception->getMessage();
}
$added = false;
if ( ! empty( $check ) && $this->sitetripemode == 5 && strtoupper( $id_type ) == 'ASIN' ) {
$atkpresponse2 = $this->load_sitestripeproduct( array( $asin ) );
array_push( $atkpresponse->responseitems, $atkpresponse2->responseitems[0] );
$added = true;
} else if ( $check != '' || $items == null ) {
$responseitem = new atkp_response_item();
$responseitem->errormessage = empty( $check ) ? 'product not found' : $check;
$responseitem->uniqueid = $asin;
$responseitem->uniquetype = $id_type;
array_push( $atkpresponse->responseitems, $responseitem );
$added = true;
} else {
if ( $items != null ) {
$result = null;
foreach ( $items as $result2 ) {
if ( $result2->getAsin() == null || $added ) {
continue;
}
$result = $result2;
break;
}
if ( $result != null ) {
$responseitem = new atkp_response_item();
$responseitem->productitem = $this->fill_product_creators( $result );
$responseitem->uniqueid = $asin;
$responseitem->uniquetype = $id_type;
array_push( $atkpresponse->responseitems, $responseitem );
$added = true;
}
}
}
}
break;
default:
throw new Exception( esc_html__( 'unknown id_type: ' . $id_type, 'affiliate-toolkit-starter' ) );
}
return $atkpresponse;
}
private function retrieve_product_list_creators( $search_request ) {
$mylist = new atkp_list_resp();
$mylist->updatedon = ATKPTools::get_currenttime();
$mylist->asins = array();
$mylist->products = null;
switch ( $search_request->request_type ) {
case atkp_list_request_type::TopSellers:
case atkp_list_request_type::NewReleases:
$searchRequest = new \Amazon\CreatorsAPI\v1\com\amazon\creators\model\SearchItemsRequestContent();
$searchRequest->setSortBy( $search_request->request_type == atkp_list_request_type::TopSellers ? 'Featured' : 'NewestArrivals' );
$searchRequest->setBrowseNodeId( $search_request->category );
$searchRequest->setKeywords( '*' );
if ( $this->languages_of_preference != null )
$searchRequest->setLanguagesOfPreference( $this->languages_of_preference );
$searchRequest->setPartnerTag( $this->associateTag );
$searchRequest->setResources(
\Amazon\CreatorsAPI\v1\com\amazon\creators\model\SearchItemsResource::getAllowableEnumValues()
);
$searchRequest->setItemCount( 10 );
$searchRequest->setItemPage( 1 );
$searchResponse = $this->sendSearchRequestCreators( $searchRequest );
$products = array();
if ( $searchResponse->getSearchResult() != null ) {
$mylist->total_items_count = $searchResponse->getSearchResult()->getTotalResultCount();
$mylist->total_pages = ceil( $mylist->total_items_count / $this->get_maximum_items_per_page() );
if ( $searchResponse->getSearchResult()->getItems() != null ) {
foreach ( $searchResponse->getSearchResult()->getItems() as $item ) {
if ( $item->getAsin() != null ) {
$products[] = $this->fill_product_creators( $item );
}
}
}
}
$mylist->products = $products;
break;
case atkp_list_request_type::ExtendedSearch:
case atkp_list_request_type::Search:
$searchRequest = new \Amazon\CreatorsAPI\v1\com\amazon\creators\model\SearchItemsRequestContent();
if ( $search_request->category != '' && $search_request->category != 'All' ) {
$searchRequest->setSearchIndex( $search_request->category );
if ( $search_request->sort_order != '' ) {
switch ( $search_request->sort_order ) {
case '-pubdate':
case '-publication_date':
case 'date-desc-rank':
case 'launch_date':
$sortorder = 'NewestArrivals';
break;
case 'reviewrank':
case 'pmrank':
case 'reviewrank_authority':
case 'review-rank':
$sortorder = 'AvgCustomerReviews';
break;
case 'price':
case 'price-asc-rank':
case 'pricerank':
$sortorder = 'Price:LowToHigh';
break;
case '-price':
case 'price-desc-rank':
case 'inverse-pricerank':
$sortorder = 'Price:HighToLow';
break;
case 'featured':
$sortorder = 'Featured';
break;
case 'AvgCustomerReviews':
case 'Featured':
case 'NewestArrivals':
case 'Price:HighToLow':
case 'Price:LowToHigh':
case 'Relevance':
$sortorder = $search_request->sort_order;
break;
default:
$sortorder = 'Relevance';
break;
}
$searchRequest->setSortBy( $sortorder );
}
}
$keyword = $search_request->keyword;
if ( $search_request->filter != null ) {
foreach ( $search_request->filter as $field => $value ) {
switch ( $field ) {
case 'Keywords':
$keyword = $value;
break;
case 'SearchIndex':
$searchRequest->setSearchIndex( $value );
break;
case 'Sort':
$searchRequest->setSortBy( $value );
break;
case 'Actor':
$searchRequest->setActor( $value );
break;
case 'Artist':
$searchRequest->setArtist( $value );
break;
case 'Author':
$searchRequest->setAuthor( $value );
break;
case 'Availability':
$searchRequest->setAvailability( $value );
break;
case 'Brand':
$searchRequest->setBrand( $value );
break;
case 'Condition':
$searchRequest->setCondition( $value );
break;
case 'DeliveryFlags':
$searchRequest->setDeliveryFlags( explode( ',', $value ) );
break;
case 'CurrencyOfPreference':
$searchRequest->setCurrencyOfPreference( $value );
break;
case 'LanguagesOfPreference':
$searchRequest->setLanguagesOfPreference( $value );
break;
case 'MaximumPrice':
$searchRequest->setMaxPrice( floatval( $value ) );
break;
case 'MinimumPrice':
$searchRequest->setMinPrice( floatval( $value ) );
break;
case 'MinPercentageOff':
$searchRequest->setMinSavingPercent( intval( $value ) );
break;
case 'MinReviewsRating':
$searchRequest->setMinReviewsRating( intval( $value ) );
break;
case 'Title':
$searchRequest->setTitle( $value );
break;
case 'BrowseNode':
$searchRequest->setBrowseNodeId( $value );
break;
}
}
}
if ( $keyword != '' ) {
$keywords = explode( ',', $keyword );
if ( $keywords != null && count( $keywords ) > 1 ) {
$searchRequest->setKeywords( $keywords );
} else {
$searchRequest->setKeywords( $keyword );
}
}
if ( $this->languages_of_preference != null )
$searchRequest->setLanguagesOfPreference( $this->languages_of_preference );
$searchRequest->setPartnerTag( $this->associateTag );
$searchRequest->setResources(
\Amazon\CreatorsAPI\v1\com\amazon\creators\model\SearchItemsResource::getAllowableEnumValues()
);
$init_page = 1;
if ( $search_request->items_per_page > 0 ) {
$searchRequest->setItemCount( $search_request->items_per_page );
$init_page = $search_request->page;
$pages = $search_request->page;
} else {
$itemsperpage = $this->get_maximum_items_per_page() > $search_request->max_count ? $search_request->max_count : $this->get_maximum_items_per_page();
$pages = ceil( $search_request->max_count / $itemsperpage );
$searchRequest->setItemCount( intval( $itemsperpage ) );
}
$products = array();
for ( $x = $init_page; $x <= $pages; $x ++ ) {
$searchRequest->setItemPage( $x );
$searchResponse = $this->sendSearchRequestCreators( $searchRequest );
if ( $searchResponse->getSearchResult() != null ) {
$mylist->total_items_count = $searchResponse->getSearchResult()->getTotalResultCount();
$mylist->total_pages = ceil( $mylist->total_items_count / $this->get_maximum_items_per_page() );
if ( $searchResponse->getSearchResult()->getItems() != null ) {
$items = $searchResponse->getSearchResult()->getItems();
foreach ( $items as $item ) {
if ( $item->getAsin() != null ) {
$products[] = $this->fill_product_creators( $item );
if ( $search_request->items_per_page == 0 && count( $products ) >= $search_request->max_count ) {
break;
}
}
}
if ( count( $items ) < $this->get_maximum_items_per_page() || ( $search_request->items_per_page == 0 && count( $products ) >= $search_request->max_count ) ) {
break;
}
}
}
}
$mylist->products = $products;
break;
default:
$mylist->message = 'unknown request_type: ' . $search_request->request_type;
break;
}
return $mylist;
}
private function retrieve_browsenodes_creators( $keyword ) {
$nodes = array();
$items = null;
try {
$searchRequest = new \Amazon\CreatorsAPI\v1\com\amazon\creators\model\SearchItemsRequestContent();
$searchRequest->setSearchIndex( 'All' );
$searchRequest->setKeywords( $keyword );
$searchRequest->setItemCount( 10 );
$searchRequest->setPartnerTag( $this->associateTag );
if ( $this->languages_of_preference != null )
$searchRequest->setLanguagesOfPreference( $this->languages_of_preference );
$searchRequest->setResources(
\Amazon\CreatorsAPI\v1\com\amazon\creators\model\SearchItemsResource::getAllowableEnumValues()
);
$searchRequest->setItemPage( 1 );
$searchResponse = $this->sendSearchRequestCreators( $searchRequest );
if ( $searchResponse->getSearchResult() != null && $searchResponse->getSearchResult()->getItems() != null ) {
$items = $searchResponse->getSearchResult()->getItems();
}
} catch ( Exception $exception ) {
// silently fail
}
if ( $items != null ) {
foreach ( $items as $item ) {
if ( $item->getBrowseNodeInfo() != null && $item->getBrowseNodeInfo()->getBrowseNodes() != null ) {
foreach ( $item->getBrowseNodeInfo()->getBrowseNodes() as $bnw ) {
$this->getBrowseNodeTreeRec( $bnw->getAncestor(), $nodes );
}
}
}
}
return $nodes;
}
public function load_variations_creators( $myproduct, $result ) {
$variations = array();
$dimmension = array();
$parentasin = $result->getParentASIN();
if ( $parentasin == '' ) {
try {
$getVariationsRequest = new \Amazon\CreatorsAPI\v1\com\amazon\creators\model\GetVariationsRequestContent();
$getVariationsRequest->setAsin( $result->getAsin() );
$getVariationsRequest->setVariationCount( 10 );
$getVariationsRequest->setPartnerTag( $this->associateTag );
if ( $this->languages_of_preference != null )
$getVariationsRequest->setLanguagesOfPreference( $this->languages_of_preference );
$getVariationsRequest->setResources(
\Amazon\CreatorsAPI\v1\com\amazon\creators\model\GetVariationsResource::getAllowableEnumValues()
);
$getVariationsResponse = $this->sendVariationRequestCreators( $getVariationsRequest );
if ( $getVariationsResponse->getVariationsResult() != null && $getVariationsResponse->getVariationsResult()->getItems() != null ) {
foreach ( $getVariationsResponse->getVariationsResult()->getItems() as $variationItem ) {
$att = $variationItem->getVariationAttributes();
if ( $att != null ) {
foreach ( $att as $tmp ) {
$dimmension[ $tmp['name'] ] = apply_filters( 'atkp_variation_name', $tmp['name'] );
}
}
$varpd = $this->fill_product_creators( $variationItem, $result );
$dimmfullname = array();
if ( $att != null ) {
foreach ( $att as $tmp ) {
$dimmfullname[ $tmp['name'] ] = $tmp['value'];
}
}
$varpd->variationname = $dimmfullname;
array_push( $variations, $varpd );
}
}
} catch ( Exception $x ) {
// silently fail
}
}
$myproduct->variationname = $dimmension;
$myproduct->variations = $variations;
return $myproduct;
}
/**
* Maps a Creators API Item object to an atkp_product.
*
* @param \Amazon\CreatorsAPI\v1\com\amazon\creators\model\Item $result
* @param \Amazon\CreatorsAPI\v1\com\amazon\creators\model\Item|null $parentResult
*
* @return atkp_product
*/
private function fill_product_creators( $result, $parentResult = null ) {
if ( $result == null ) {
return null;
}
$myproduct = new atkp_product();
$myproduct->updatedon = ATKPTools::get_currenttime();
$myproduct->shopid = $this->shopid;
$myproduct->asin = $result->getAsin();
$myproduct->parentasin = $result->getParentASIN();
if ( $parentResult == null && $this->load_variations ) {
$myproduct = $this->load_variations_creators( $myproduct, $result );
}
// Product URL
if ( $result->getDetailPageURL() != null ) {
$myproduct->producturl = urldecode( $result->getDetailPageURL() );
$myproduct->producturl = str_replace( '%', '%25', $myproduct->producturl );
$myproduct->addtocarturl = $this->checkurl( 'https://www.amazon.' . $this->country . '/gp/aws/cart/add.html?AssociateTag=' . $this->associateTag . '&ASIN.1=' . $myproduct->asin . '&Quantity.1=1' );
} else if ( $parentResult != null && $parentResult->getDetailPageURL() != null ) {
$myproduct->producturl = urldecode( $parentResult->getDetailPageURL() );
$myproduct->producturl = str_replace( '%', '%25', $myproduct->producturl );
$myproduct->producturl = $this->checkurl( 'https://www.amazon.' . $this->country . '/dp/' . $myproduct->asin . '?tag=' . $this->associateTag );
$myproduct->addtocarturl = $this->checkurl( 'https://www.amazon.' . $this->country . '/gp/aws/cart/add.html?AssociateTag=' . $this->associateTag . '&ASIN.1=' . $myproduct->asin . '&Quantity.1=1' );
} else {
$myproduct->producturl = '';
$myproduct->addtocarturl = '';
}
// Images
$images = array();
if ( $result->getImages() != null ) {
if ( $result->getImages()->getPrimary() != null ) {
if ( $result->getImages()->getPrimary()->getSmall() != null )
$myproduct->smallimageurl = $this->checkimageurl( $result->getImages()->getPrimary()->getSmall()->getUrl(), 'small' );
if ( $result->getImages()->getPrimary()->getMedium() != null )
$myproduct->mediumimageurl = $this->checkimageurl( $result->getImages()->getPrimary()->getMedium()->getUrl(), 'medium' );
if ( $result->getImages()->getPrimary()->getLarge() != null )
$myproduct->largeimageurl = $this->checkimageurl( $result->getImages()->getPrimary()->getLarge()->getUrl(), 'large' );
}
if ( $result->getImages()->getVariants() != null ) {
foreach ( $result->getImages()->getVariants() as $variant ) {
if ( $variant->getLarge() == null && $variant->getMedium() == null && $variant->getSmall() == null ) {
continue;
}
$udf = new atkp_product_image();
$udf->id = uniqid();
if ( $variant->getSmall() != null ) {
$udf->smallimageurl = $this->checkimageurl( $variant->getSmall()->getUrl(), 'small' );
}
if ( $variant->getMedium() != null ) {
$udf->mediumimageurl = $this->checkimageurl( $variant->getMedium()->getUrl(), 'medium' );
}
if ( $variant->getLarge() != null ) {
$udf->largeimageurl = $this->checkimageurl( $variant->getLarge()->getUrl(), 'large' );
}
array_push( $images, $udf );
}
}
}
$myproduct->images = $images;
// Customer Reviews
if ( $result->getCustomerReviews() != null ) {
if ( $result->getCustomerReviews()->getStarRating() != null )
$myproduct->rating = $result->getCustomerReviews()->getStarRating()->getValue();
if ( $result->getCustomerReviews()->getCount() != null )
$myproduct->reviewcount = $result->getCustomerReviews()->getCount();
}
if ( $this->load_customer_reviews && $myproduct->reviewcount == 0 ) {
$averageRating = 0;
$totalReviews = 0;
$this->get_customer_rating_api( $myproduct->asin, $averageRating, $totalReviews );
$myproduct->rating = $averageRating;
$myproduct->reviewcount = $totalReviews;
}
$myproduct->customerreviewurl = $this->checkurl( 'https://www.amazon.' . $this->country . '/product-reviews/' . $myproduct->asin . '/?tag=' . $this->associateTag );
// Features and Description
$description = '';
$features = '';
if ( $result->getItemInfo() != null && $result->getItemInfo()->getFeatures() != null && $result->getItemInfo()->getFeatures()->getDisplayValues() != null ) {
foreach ( $result->getItemInfo()->getFeatures()->getDisplayValues() as $feature ) {
$features .= '' . $feature . '';
$description .= $feature . '
';
}
}
$myproduct->features = $features == '' ? '' : '';
$myproduct->description = $description;
// Title
if ( $result->getItemInfo() != null && $result->getItemInfo()->getTitle() != null && $result->getItemInfo()->getTitle()->getDisplayValue() != null ) {
$myproduct->title = htmlentities( $result->getItemInfo()->getTitle()->getDisplayValue() );
} else {
$myproduct->title = '';
}
// Product Info (dimensions, color)
if ( $result->getItemInfo() != null && $result->getItemInfo()->getProductInfo() != null ) {
$productInfo = $result->getItemInfo()->getProductInfo();
if ( $productInfo->getColor() != null )
$myproduct->customfields["a_color"] = $productInfo->getColor()->getDisplayValue();
if ( $productInfo->getItemDimensions() != null ) {
if ( $productInfo->getItemDimensions()->getHeight() != null )
$myproduct->customfields["a_height"] = round( $productInfo->getItemDimensions()->getHeight()->getDisplayValue(), 2 ) . ' ' . ( $productInfo->getItemDimensions()->getHeight()->getUnit() );
if ( $productInfo->getItemDimensions()->getLength() != null )
$myproduct->customfields["a_length"] = round( $productInfo->getItemDimensions()->getLength()->getDisplayValue(), 2 ) . ' ' . ( $productInfo->getItemDimensions()->getLength()->getUnit() );
if ( $productInfo->getItemDimensions()->getWidth() != null )
$myproduct->customfields["a_width"] = round( $productInfo->getItemDimensions()->getWidth()->getDisplayValue(), 2 ) . ' ' . ( $productInfo->getItemDimensions()->getWidth()->getUnit() );
if ( $productInfo->getItemDimensions()->getWeight() != null )
$myproduct->customfields["a_weight"] = round( $productInfo->getItemDimensions()->getWeight()->getDisplayValue(), 2 ) . ' ' . ( $productInfo->getItemDimensions()->getWeight()->getUnit() );
}
}
// OffersV2 pricing
if ( $result->getOffersV2() != null ) {
$offerlisting = null;
if ( $result->getOffersV2()->getListings() != null ) {
foreach ( $result->getOffersV2()->getListings() as $listing ) {
if ( $listing->getIsBuyBoxWinner() &&
$this->allowedCondition( $listing->getCondition() == null ? null : $listing->getCondition()->getValue() ) ) {
$offerlisting = $listing;
break;
}
}
if ( $offerlisting == null ) {
$listings = array();
foreach ( $result->getOffersV2()->getListings() as $list ) {
if ( $this->allowedCondition( $list->getCondition() == null ? null : $list->getCondition()->getValue() ) ) {
$listings[] = $list;
}
}
$offerlisting = reset( $listings );
}
}
$myproduct->iswarehouse = false;
if ( $offerlisting && $offerlisting != null ) {
// Prime and Shipping - check via OfferType or MerchantInfo
if ( $offerlisting->getMerchantInfo() != null ) {
if ( $offerlisting->getMerchantInfo()->getName() == 'Amazon Warehouse' ) {
$myproduct->iswarehouse = true;
}
}
// Price
if ( $offerlisting->getPrice() != null ) {
$price = $offerlisting->getPrice();
if ( $price->getMoney() != null ) {
$myproduct->saleprice = $price->getMoney()->getDisplayAmount();
$myproduct->salepricefloat = $price->getMoney()->getAmount();
}
// PricePerUnit (baseprice)
if ( $price->getPricePerUnit() != null ) {
$pricePerUnitObj = $price->getPricePerUnit();
if ( is_object( $pricePerUnitObj ) && method_exists( $pricePerUnitObj, 'getAmount' ) && $pricePerUnitObj->getAmount() != null ) {
$myproduct->basepricefloat = $pricePerUnitObj->getAmount();
$myproduct->unitpricefloat = $pricePerUnitObj->getAmount();
if ( $pricePerUnitObj->getDisplayAmount() != null ) {
$parts = explode( ' / ', $pricePerUnitObj->getDisplayAmount() );
$myproduct->baseprice = trim( $parts[0] );
if ( count( $parts ) > 1 ) {
$myproduct->baseunit = trim( $parts[1] );
}
}
}
}
// Savings
if ( $price->getSavings() != null ) {
$savings = $price->getSavings();
$myproduct->percentagesaved = $savings->getPercentage();
if ( $savings->getMoney() != null ) {
$myproduct->amountsaved = $savings->getMoney()->getDisplayAmount();
$myproduct->amountsavedfloat = $savings->getMoney()->getAmount();
}
}
// SavingBasis (list price)
if ( $price->getSavingBasis() != null ) {
$savingBasis = $price->getSavingBasis();
if ( $savingBasis->getMoney() != null ) {
$myproduct->listprice = $savingBasis->getMoney()->getDisplayAmount();
$myproduct->listpricefloat = $savingBasis->getMoney()->getAmount();
}
}
}
// Availability
if ( $offerlisting->getAvailability() != null ) {
$myproduct->availability = $offerlisting->getAvailability()->getMessage();
}
}
}
$myproduct->shippingfloat = (float) 0;
// Manufacturer and Brand
if ( $result->getItemInfo() != null && $result->getItemInfo()->getByLineInfo() != null && $result->getItemInfo()->getByLineInfo()->getManufacturer() != null ) {
$myproduct->manufacturer = $result->getItemInfo()->getByLineInfo()->getManufacturer()->getDisplayValue();
}
if ( $result->getItemInfo() != null && $result->getItemInfo()->getByLineInfo() != null && $result->getItemInfo()->getByLineInfo()->getBrand() != null ) {
$myproduct->brand = $result->getItemInfo()->getByLineInfo()->getBrand()->getDisplayValue();
}
// ISBN
$isbn_full = '';
if ( $result->getItemInfo() != null && $result->getItemInfo()->getExternalIds() != null && $result->getItemInfo()->getExternalIds()->getISBNs() != null ) {
foreach ( $result->getItemInfo()->getExternalIds()->getISBNs()->getDisplayValues() as $ean ) {
if ( $isbn_full != '' ) {
$isbn_full .= ',';
}
$isbn_full .= $ean;
}
}
$myproduct->isbn = $isbn_full;
// EAN
$ean_full = '';
if ( $result->getItemInfo() != null && $result->getItemInfo()->getExternalIds() != null && $result->getItemInfo()->getExternalIds()->getEANs() != null ) {
foreach ( $result->getItemInfo()->getExternalIds()->getEANs()->getDisplayValues() as $ean ) {
if ( $ean_full != '' ) {
$ean_full .= ',';
}
$ean_full .= $ean;
}
}
$myproduct->ean = $ean_full;
// Browse Node / Product Group
$category = '';
if ( $result->getBrowseNodeInfo() != null && $result->getBrowseNodeInfo()->getBrowseNodes() != null ) {
foreach ( $result->getBrowseNodeInfo()->getBrowseNodes() as $bnw ) {
$category .= $this->getBrowseNodeTree( $bnw->getAncestor() );
break;
}
}
$myproduct->productgroup = $category;
// Release Date
if ( $result->getItemInfo() != null && $result->getItemInfo()->getProductInfo() && $result->getItemInfo()->getProductInfo()->getReleaseDate() ) {
$myproduct->releasedate = substr( $result->getItemInfo()->getProductInfo()->getReleaseDate()->getDisplayValue(), 0, 10 );
}
// Author
if ( $result->getItemInfo() != null && $result->getItemInfo()->getByLineInfo() != null && $result->getItemInfo()->getByLineInfo()->getContributors() != null ) {
foreach ( $result->getItemInfo()->getByLineInfo()->getContributors() as $const ) {
if ( $const->getRole() == 'Autor' ) {
$myproduct->author = $const->getName();
break;
}
}
}
// Number of Pages
if ( $result->getItemInfo() != null && $result->getItemInfo()->getContentInfo() != null && $result->getItemInfo()->getContentInfo()->getPagesCount() != null ) {
$myproduct->numberofpages = $result->getItemInfo()->getContentInfo()->getPagesCount()->getDisplayValue();
}
// MPN
$myproduct->mpn = '';
if ( $result->getItemInfo() != null && $result->getItemInfo()->getManufactureInfo() != null && $result->getItemInfo()->getManufactureInfo()->getItemPartNumber() != null ) {
$myproduct->mpn = $result->getItemInfo()->getManufactureInfo()->getItemPartNumber()->getDisplayValue();
}
// If no price found and has variations, try to get price from first variation
if ( count( $myproduct->variations ) > 0 && $myproduct->salepricefloat == 0 ) {
foreach ( $myproduct->variations as $variation ) {
if ( $variation->salepricefloat > 0 ) {
$myproduct->listprice = $variation->listprice;
$myproduct->amountsaved = $variation->amountsaved;
$myproduct->saleprice = $variation->saleprice;
$myproduct->listpricefloat = $variation->listpricefloat;
$myproduct->amountsavedfloat = $variation->amountsavedfloat;
$myproduct->percentagesaved = $variation->percentagesaved;
$myproduct->salepricefloat = $variation->salepricefloat;
$myproduct->shippingfloat = $variation->shippingfloat;
$myproduct->availability = $variation->availability;
$myproduct->shipping = $variation->shipping;
$myproduct->isprime = $variation->isprime;
$myproduct->iswarehouse = $variation->iswarehouse;
$myproduct->smallimageurl = $variation->smallimageurl;
$myproduct->mediumimageurl = $variation->mediumimageurl;
$myproduct->largeimageurl = $variation->largeimageurl;
break;
}
}
}
// NoAPI fallback for mode 5
if ( $myproduct->salepricefloat == 0 && $this->sitetripemode == 5 && $myproduct->asin != '' ) {
$atkpresponse = $this->load_sitestripeproduct( array( $myproduct->asin ) );
if ( count( $atkpresponse->responseitems ) > 0 && $atkpresponse->responseitems[0]->productitem != null ) {
$myproduct->saleprice = $atkpresponse->responseitems[0]->productitem->saleprice;
$myproduct->salepricefloat = $atkpresponse->responseitems[0]->productitem->salepricefloat;
}
}
return $myproduct;
}
}
?>