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 AmazonWooCommerceamazon_request_callback()
{
    global $AmazonWooCommerce;
    $amazon_settings = $AmazonWooCommerce->getAllSettings('array', 'amazon');
    $plugin_uri = $AmazonWooCommerce->cfg['paths']['plugin_dir_url'] . 'modules/amazon/';
    $requestData = array('search' => isset($_REQUEST['search']) ? htmlentities($_REQUEST['search']) : '', 'category' => isset($_REQUEST['category']) ? htmlentities($_REQUEST['category']) : '', 'page' => isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : '');
    // 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']);
    // changing the category to {$requestData['category']} and the response to only images and looking for some matrix stuff.
    $response = $aaAmazonWS->category($requestData['category'])->page($requestData['page'])->responseGroup('Large')->search($requestData['search']);
    // print some debug if requested
    if ($_GET['dump'] == 1 && is_admin()) {
        var_dump('<pre>', $requestData, $response, '</pre>');
        die;
    }
    if ($response['Items']['Request']['IsValid'] == 'False') {
        die('<div class="error" style="float: left;margin: 10px;padding: 6px;">BDA Amazon error id: <bold>' . $response['Items']['Request']['Errors']['Error']['Code'] . '</bold>: <br /> ' . $response['Items']['Request']['Errors']['Error']['Message'] . '</div>');
    } elseif (count($response['Items']) > 0) {
        if ($response['Items']['TotalResults'] > 1) {
            ?>
			<div class="resultsTopBar">
				<h2>
					Showing <?php 
            echo $requestData['page'];
            ?>
 - <?php 
            echo $response['Items']["TotalPages"];
            ?>
 of <span id="AmazonWooCommerce-totalPages"><?php 
            echo $response['Items']["TotalResults"];
            ?>
</span> Results
				</h2>

				<div class="AmazonWooCommerce-pagination">
					<span>View page:</span>
					<select id="AmazonWooCommerce-page">
						<?php 
            for ($p = 1; $p <= 5; $p++) {
                echo '<option value="' . $p . '" ' . ($p == $requestData['page'] ? 'selected' : '') . '> ' . $p . ' </option>';
            }
            ?>
					</select>
				</div>
			</div>

		<?php 
        }
        // don't show paging if total results it's not bigget than 1
        if (count($response['Items']['Item']) > 0) {
            echo '
				<div class="AmazonWooCommerce-product-box">
					<table class="product">
				';
            $cc = 0;
            foreach ($response['Items']['Item'] as $key => $value) {
                if ($response['Items']['TotalResults'] == 1) {
                    $value = $response['Items']['Item'];
                    if ($_REQUEST['dump'] == 1) {
                        var_dump('<pre>', $value, '</pre>');
                        die;
                    }
                }
                if ($cc + 1 > $response['Items']['TotalResults']) {
                    continue;
                }
                $thumb = $value['SmallImage']['URL'];
                if (trim($thumb) == "") {
                    // try to find image as first image from image sets
                    $thumb = $value['ImageSets']['ImageSet'][0]['SmallImage']['URL'];
                }
                ?>
					<tr>
						<td class="product-number"><?php 
                echo ++$cc;
                ?>
.</td>
						<td class="product-image">
							<a href="<?php 
                echo $value['DetailPageURL'];
                ?>
" target="_blank">
								<img class="productImage" src="<?php 
                echo $thumb;
                ?>
">
							</a>
						</td>
						<td class="product-data">
							<h4 class="product-title">
								<a href="<?php 
                echo $value['DetailPageURL'];
                ?>
" target="_blank"><?php 
                echo $value['ItemAttributes']['Title'];
                ?>
</a>
							</h4>
							<div class="newPrice">
								<span class="price"><?php 
                echo $value['Offers']['Offer']['OfferListing']['Price']['FormattedPrice'];
                ?>
</span>
							</div>
							<div class="product-description"><?php 
                echo $value['EditorialReviews']['EditorialReview']['Content'];
                ?>
</div>
						</td>
						<td class="product-options">
							<?php 
                if ($value['CustomerReviews']['HasReviews'] == true) {
                    echo '<a class="thickbox AmazonWooCommerce-option-btn" href="' . $value['CustomerReviews']['IFrameURL'] . '&TB_iframe=true"> <img src="' . $plugin_uri . 'assets/comments.png" /> Customer Reviews</a>';
                }
                ?>
							<a class="AmazonWooCommerce-option-btn AmazonWooCommerce-load-product" href="#" rel="<?php 
                echo $value['ASIN'];
                ?>
"><img src="<?php 
                echo $plugin_uri;
                ?>
assets/update.png" /> Load this product </a>
						<td>
					</tr>
		<?php 
            }
            // end foreach
            echo '</table></div>';
            // close the table
        }
        // end if have products
    }
    die;
    // this is required to return a proper result
}