public function getAmzSimilarityProducts($asin, $return_nr = 3, $force_update = false) { // add 1 fake return products, current product $return_nr = $return_nr + 1; $cache_valid_for = 60 * 60 * 24; // 24 hours in seconds // check for cache of this ASIN $cache_request = $this->db->get_row($this->db->prepare("SELECT * FROM " . $this->db->prefix . "amz_cross_sell WHERE ASIN = %s", $asin), ARRAY_A); // if cache found for this product if ($cache_request != "" && count($cache_request) > 0 && $force_update === false) { // if cache still valid, return from mysql cache if (isset($cache_request['add_date']) || strtotime($cache_request['add_date']) > time() + $cache_valid_for) { $ret = array(); // get products from DB cache amz_cross_sell table $products = @unserialize($cache_request['products']); return array_slice($products, 0, $return_nr); } } $amazon_settings = $this->getAllSettings('array', 'amazon'); // load the amazon webservices client class require_once $this->cfg['paths']['plugin_dir_path'] . '/lib/scripts/amazon/aaAmazonWS.class.php'; // create new amazon instance $aaAmazonWS = new aaAmazonWS($amazon_settings['AccessKeyID'], $amazon_settings['SecretAccessKey'], $amazon_settings['country'], $this->main_aff_id()); $retProd = array(); $similarity = $aaAmazonWS->responseGroup('Medium,ItemAttributes,Offers')->optionalParameters(array('MerchantId' => 'Amazon', 'Condition' => 'New'))->similarityLookup($asin); $thisProd = $aaAmazonWS->responseGroup('Large,OfferFull,Offers')->optionalParameters(array('MerchantId' => 'Amazon', 'Condition' => 'New'))->lookup($asin); if ($thisProd['Items']['Request']["IsValid"] == "True" && isset($thisProd['Items']['Item']) && count($thisProd['Items']['Item']) > 0) { $thisProd = $thisProd['Items']['Item']; // product large image $retProd[$thisProd['ASIN']]['thumb'] = $thisProd['SmallImage']['URL']; $retProd[$thisProd['ASIN']]['ASIN'] = $thisProd['ASIN']; // product title $retProd[$thisProd['ASIN']]['Title'] = isset($thisProd['ItemAttributes']['Title']) ? $thisProd['ItemAttributes']['Title'] : ''; // product Manufacturer $retProd[$thisProd['ASIN']]['Manufacturer'] = isset($thisProd['ItemAttributes']['Manufacturer']) ? $thisProd['ItemAttributes']['Manufacturer'] : ''; $retProd[$thisProd['ASIN']]['price'] = isset($thisProd['OfferSummary']['LowestNewPrice']['FormattedPrice']) ? preg_replace("/[^0-9,.]/", "", $thisProd['OfferSummary']['LowestNewPrice']['FormattedPrice']) : ''; } if ($similarity['Items']["Request"]["IsValid"] == "True" && isset($similarity['Items']['Item']) && count($similarity['Items']['Item']) > 1) { foreach ($similarity['Items']['Item'] as $key => $value) { $thisProd = $value; if (count($similarity['Items']['Item']) > 0 && count($value) > 0 && isset($thisProd['ASIN']) && strlen($thisProd['ASIN']) >= 10) { // product large image $retProd[$thisProd['ASIN']]['thumb'] = $thisProd['SmallImage']['URL']; $retProd[$thisProd['ASIN']]['ASIN'] = $thisProd['ASIN']; // product title $retProd[$thisProd['ASIN']]['Title'] = isset($thisProd['ItemAttributes']['Title']) ? $thisProd['ItemAttributes']['Title'] : ''; // product Manufacturer $retProd[$thisProd['ASIN']]['Manufacturer'] = isset($thisProd['ItemAttributes']['Manufacturer']) ? $thisProd['ItemAttributes']['Manufacturer'] : ''; $retProd[$thisProd['ASIN']]['price'] = isset($thisProd['OfferSummary']['LowestNewPrice']['FormattedPrice']) ? preg_replace("/[^0-9,.]/", "", $thisProd['OfferSummary']['LowestNewPrice']['FormattedPrice']) : ''; // remove if don't have valid price if (!isset($retProd[$thisProd['ASIN']]['price']) || trim($retProd[$thisProd['ASIN']]['price']) == "") { @unlink($retProd[$thisProd['ASIN']]); } } } } // if cache not found for this product if ($cache_request == "" && count($cache_request) == 0) { $this->db->insert($this->db->prefix . "amz_cross_sell", array('ASIN' => $asin, 'products' => serialize(array_slice($retProd, 0, $return_nr)), 'nr_products' => $return_nr), array('%s', '%s', '%d')); } else { $this->db->update($this->db->prefix . "amz_cross_sell", array('products' => serialize(array_slice($retProd, 0, $return_nr)), 'nr_products' => $return_nr), array('ASIN' => $asin), array('%s', '%s', '%d')); } return array_slice($retProd, 0, $return_nr); }
function AmazonWooCommerce_load_product_callback() { global $AmazonWooCommerce; $amazon_settings = $AmazonWooCommerce->getAllSettings('array', 'amazon'); $plugin_uri = $AmazonWooCommerce->cfg['paths']['plugin_dir_url'] . 'modules/amazon/'; $requestData = array('ASIN' => isset($_REQUEST['ASIN']) ? htmlentities($_REQUEST['ASIN']) : '', 'to-category' => isset($_REQUEST['to-category']) ? htmlentities($_REQUEST['to-category']) : 'amz'); // load the amazon webservices client class require_once $AmazonWooCommerce->cfg['paths']['plugin_dir_path'] . '/lib/scripts/amazon/aaAmazonWS.class.php'; // create new amazon instance $aaAmazonWS = new aaAmazonWS($amazon_settings['AccessKeyID'], $amazon_settings['SecretAccessKey'], $amazon_settings['country'], $amazon_settings['AffiliateId']); // create request by ASIN $product = $aaAmazonWS->responseGroup('Large')->optionalParameters(array('MerchantId' => 'All'))->lookup($requestData['ASIN']); if ($product['Items']["Request"]["IsValid"] == "True") { $thisProd = $product['Items']['Item']; if (count($product['Items']['Item']) > 0) { // start creating return array $retProd = $retProd['images'] = array(); // product large image $retProd['images'][] = $thisProd['LargeImage']['URL']; $retProd['ASIN'] = $thisProd['ASIN']; // get gallery images if (count($thisProd['ImageSets']) > 0) { $count = 0; foreach ($thisProd['ImageSets']["ImageSet"] as $key => $value) { if ($count > 5) { continue; } if (isset($value['LargeImage']['URL']) && $count > 0) { $retProd['images'][] = $value['LargeImage']['URL']; } $count++; } } // set other ItemAttributes // CustomerReviews url if ($thisProd['CustomerReviews']['HasReviews']) { $retProd['CustomerReviewsURL'] = $thisProd['CustomerReviews']['IFrameURL']; } // DetailPageURL $retProd['DetailPageURL'] = $thisProd['DetailPageURL']; // ItemLinks $retProd['ItemLinks'] = $thisProd['ItemLinks']; // product title $retProd['Title'] = $thisProd['ItemAttributes']['Title']; // Binding $retProd['Binding'] = $thisProd['ItemAttributes']['Binding']; // ProductGroup $retProd['ProductGroup'] = $thisProd['ItemAttributes']['ProductGroup']; // SKU $retProd['SKU'] = $thisProd['ItemAttributes']['SKU']; // Feature $retProd['Feature'] = $thisProd['ItemAttributes']['Feature']; // price (OfferSummary) //['Offers'] $retProd['price'] = array('Amount' => $thisProd['Offers']['Offer']['OfferListing']['Price']['Amount'], 'FormattedPrice' => preg_replace("/[^0-9,.]/", "", $thisProd['Offers']['Offer']['OfferListing']['Price']['FormattedPrice'])); // EditorialReviews $retProd['EditorialReviews'] = $thisProd['EditorialReviews']['EditorialReview']['Content']; if ($_REQUEST['dump'] == '1') { var_dump('<pre>', $retProd, $thisProd, '</pre>'); die; } $prod_id = $AmazonWooCommerce->addNewWooProduct($retProd); // now return everythink as json die(json_encode(array('status' => 'valid', 'prod_id' => $prod_id, 'redirect_url' => sprintf(admin_url('/post.php?post=%s&action=edit'), $prod_id)))); } } else { die(json_encode(array('status' => 'invalid', 'msg' => "Can't get product by given ASIN: " . $requestData['ASIN']))); } }