| @@ -25,9 +25,8 @@ | ||
| 25 | 25 | const SHORT_LINK_DOMAINS = array( 'amzn.com', 'amzn.to' ); |
| 26 | 26 | const FILTER_AMAZON_PRODUCT = 'filter_amazon_product'; |
| 27 | 27 | const TRACKING_ID_REGEX = '^[a-zA-Z0-9-]+-\d{2,3}$'; |
| 28 | 28 | const CURRENCY_ISO = array( 'USD', 'AUD', 'CAD', 'EUR', 'MXN', 'CNY', 'JPY', 'INR', 'SEK', 'BRL', 'TRY', 'GBP', 'PLN', 'EGP', 'SGD', 'AED' ); |
| 29 | - const VARIATION_PAGE_LIMIT = 2; | |
| 30 | 29 | |
| 31 | 30 | /** |
| 32 | 31 | * Get amazon API countries |
| 33 | 32 | */ |
| @@ -483,17 +482,8 @@ | ||
| 483 | 482 | $product_url = $amz_link ? $amz_link : $product['url']; |
| 484 | 483 | $product['status_code'] = 200; |
| 485 | 484 | $product['url'] = self::get_amazon_product_url( $product_url ); |
| 486 | 485 | |
| 487 | - // ? If $item->Offers is missing, we try to get the quantity from variation data | |
| 488 | - if ( ! isset( $item->Offers ) ) { // phpcs:ignore | |
| 489 | - sleep( 1 ); // ? Delay for a while before call the next request | |
| 490 | - $variation_product = $this->get_product_variation( $product_id, $product_url ); | |
| 491 | - if ( $variation_product && isset( $variation_product['quantity'] ) && $variation_product['quantity'] ) { | |
| 492 | - $product = $variation_product; | |
| 493 | - } | |
| 494 | - } | |
| 495 | - | |
| 496 | 486 | // ? Get Lasso ID |
| 497 | 487 | $query = ' |
| 498 | 488 | SELECT post_id |
| 499 | 489 | FROM ' . $lasso_db->postmeta . " |
| @@ -673,14 +663,13 @@ | ||
| 673 | 663 | * Extract result to an array |
| 674 | 664 | * |
| 675 | 665 | * @param object $response Data from Amazon. |
| 676 | 666 | * @param bool $large_image Get large image size. Default to false. |
| 677 | - * @param string $product_id Product Id. Default to empty. | |
| 678 | - * @param string $product_url Product url. Default to empty. | |
| 679 | 667 | * |
| 680 | 668 | * @return array |
| 681 | 669 | */ |
| 682 | - private function extract_search_result_v5( $response, $large_image = false, $product_id = '', $product_url = '' ) { | |
| 670 | + private function extract_search_result_v5( $response, $large_image = false ) { | |
| 671 | + | |
| 683 | 672 | $image = ''; |
| 684 | 673 | if ( isset( $response->Images->Primary ) ) { // phpcs:ignore |
| 685 | 674 | $image = $large_image ? $response->Images->Primary->Large->URL : $response->Images->Primary->Small->URL; // phpcs:ignore |
| 686 | 675 | } |
| @@ -686,11 +675,11 @@ | ||
| 686 | 675 | } |
| 687 | 676 | |
| 688 | 677 | // @codingStandardsIgnoreStart |
| 689 | 678 | $result = array( |
| 690 | - 'product_id' => $product_id ? $product_id : ( $response->ASIN ?? 0 ), | |
| 679 | + 'product_id' => $response->ASIN ?? 0, | |
| 691 | 680 | 'title' => $response->ItemInfo->Title->DisplayValue ?? '', |
| 692 | - 'url' => $product_url ? $product_url : ( $response->DetailPageURL ?? '' ), | |
| 681 | + 'url' => $response->DetailPageURL ?? '', | |
| 693 | 682 | 'default_url' => $response->DetailPageURL ?? '', |
| 694 | 683 | 'image' => $image, |
| 695 | 684 | 'quantity' => $response->Offers->Summaries[0]->OfferCount ?? 0, |
| 696 | 685 | 'is_prime' => $response->Offers->Listings[0]->DeliveryInfo->IsPrimeEligible ?? false, |
| @@ -1192,87 +1181,6 @@ | ||
| 1192 | 1181 | $currency_position = preg_match( '/[€]|R\$|TL|kr|zł/', $currency_symbol ) ? 'end' : 'begin'; |
| 1193 | 1182 | $price_format = preg_match( '/[€]|R\$|TL|kr|zł/', $currency_symbol ) ? number_format( $price_value, 2, ',', '.' ) : number_format( $price_value, 2, '.', ',' ); |
| 1194 | 1183 | |
| 1195 | 1184 | return 'begin' === $currency_position ? $currency_symbol . $price_format : $price_format . $currency_symbol; |
| 1196 | - } | |
| 1197 | - | |
| 1198 | - /** | |
| 1199 | - * Get the variation item in stock | |
| 1200 | - * | |
| 1201 | - * @param string $product_id Product id. | |
| 1202 | - * @param string $product_url Product url. | |
| 1203 | - * @param int $variation_page Variation page. | |
| 1204 | - * @return array | |
| 1205 | - */ | |
| 1206 | - public function get_product_variation( $product_id, $product_url, $variation_page = 1 ) { | |
| 1207 | - $result = $this->get_product_variations_by_id_v5( $product_id, $variation_page ); | |
| 1208 | - $items = $result->VariationsResult->Items ?? array(); // phpcs:ignore | |
| 1209 | - | |
| 1210 | - if ( ! empty( $items ) ) { | |
| 1211 | - $items = $result->VariationsResult->Items; // phpcs:ignore | |
| 1212 | - $product_variations = array(); | |
| 1213 | - | |
| 1214 | - // ? Get product variation list | |
| 1215 | - foreach ( $items as $item ) { | |
| 1216 | - $product_variations[] = $this->extract_search_result_v5( $item, true, $product_id, $product_url ); | |
| 1217 | - } | |
| 1218 | - | |
| 1219 | - // ? Sort price from lowest to highest | |
| 1220 | - usort( | |
| 1221 | - $product_variations, | |
| 1222 | - function( $a, $b ) { | |
| 1223 | - return strcmp( $a['amount'], $b['amount'] ); | |
| 1224 | - } | |
| 1225 | - ); | |
| 1226 | - | |
| 1227 | - // ? Get the in-stock product | |
| 1228 | - foreach ( $product_variations as $product_variation ) { | |
| 1229 | - if ( $product_variation['quantity'] && $product_variation['price'] ) { | |
| 1230 | - return $product_variation; | |
| 1231 | - } | |
| 1232 | - } | |
| 1233 | - | |
| 1234 | - // ? If all variation products in this page unavailable, we request to the next page | |
| 1235 | - $page_count = $result->VariationsResult->VariationSummary->PageCount ?? 1; // phpcs:ignore | |
| 1236 | - if ( $variation_page < self::VARIATION_PAGE_LIMIT && $variation_page < $page_count ) { | |
| 1237 | - sleep( 1 ); // ? Delay for a while before call the next request | |
| 1238 | - return $this->get_product_variation( $product_id, $product_url, $variation_page + 1 ); | |
| 1239 | - } | |
| 1240 | - } | |
| 1241 | - | |
| 1242 | - return array(); | |
| 1243 | - } | |
| 1244 | - | |
| 1245 | - /** | |
| 1246 | - * Get Amazon product variations by product id | |
| 1247 | - * | |
| 1248 | - * @param string $product_id Amazon product id. | |
| 1249 | - * @param int $variation_page Variation page. | |
| 1250 | - * | |
| 1251 | - * @return object | |
| 1252 | - */ | |
| 1253 | - public function get_product_variations_by_id_v5( $product_id, $variation_page = 1 ) { | |
| 1254 | - $parameters = array( | |
| 1255 | - 'Operation' => 'GetVariations', | |
| 1256 | - 'ASIN' => $product_id, | |
| 1257 | - 'Condition' => 'New', | |
| 1258 | - 'VariationPage' => $variation_page, | |
| 1259 | - 'Resources' => array( | |
| 1260 | - 'Images.Primary.Small', | |
| 1261 | - 'Images.Primary.Large', | |
| 1262 | - 'ItemInfo.Title', | |
| 1263 | - 'ItemInfo.ContentRating', | |
| 1264 | - 'ItemInfo.Features', | |
| 1265 | - 'ItemInfo.ProductInfo', | |
| 1266 | - 'ItemInfo.TechnicalInfo', | |
| 1267 | - 'Offers.Listings.Price', | |
| 1268 | - 'Offers.Listings.SavingBasis', | |
| 1269 | - 'Offers.Summaries.OfferCount', | |
| 1270 | - 'Offers.Listings.DeliveryInfo.IsPrimeEligible', | |
| 1271 | - ), | |
| 1272 | - ); | |
| 1273 | - | |
| 1274 | - $json_response = $this->query_amazon_v5( $parameters ); | |
| 1275 | - | |
| 1276 | - return $json_response; | |
| 1277 | 1185 | } |
| 1278 | 1186 | } |