0 ? Availability::available() : Availability::out_of_stock(); } if($availability === null && isset($item['Offers']['Offer']['OfferListing']['AvailabilityAttributes']['AvailabilityType'])) { $type = $item['Offers']['Offer']['OfferListing']['AvailabilityAttributes']['AvailabilityType']; $availability = $type == 'now' ? Availability::available() : Availability::out_of_stock(); } if($availability === null && isset($item['ItemAttributes']['ProductGroup']) && $item['ItemAttributes']['ProductGroup'] == 'eBooks') { $availability = Availability::available(); } $availability = apply_filters('aff_amazon_helper_find_availability', $availability, $item); return $availability; } /** * Find the price in the Amazon API item response. * * @since 0.9 * @param array $item The Amazon API response converted from XML to an array. * @return null|Money */ public static function find_price(array $item) { $price = null; if(isset($item['Offers']['Offer']['OfferListing'])) { $offer_listing = $item['Offers']['Offer']['OfferListing']; $price = isset($offer_listing['SalePrice']) ? $offer_listing['SalePrice'] : $offer_listing['Price']; if(isset($price['Amount']) && isset($price['CurrencyCode'])) { $amount = floatval($price['Amount']) / 100; $currency = $price['CurrencyCode']; $price = new Money($amount, new Currency($currency)); } } $price = apply_filters('aff_amazon_helper_find_price', $price, $item); return $price; } /** * Find the old price in the Amazon API item response. * * @since 0.9 * @param array $item The Amazon API response converted from XML to an array. * @return null|Money */ public static function find_old_price(array $item) { $old_price = null; if(isset($item['Offers']['Offer']['OfferListing'])) { $offerListing = $item['Offers']['Offer']['OfferListing']; $old_price = null; if(isset($offerListing['SalePrice']) && isset($offerListing['Price'])) { $old_price = $offerListing['Price']; } if(isset($offerListing['AmountSaved']) && isset($offerListing['Price'])) { $old_price = [ 'Amount' => intval($offerListing['Price']['Amount']) + intval($offerListing['AmountSaved']['Amount']), 'CurrencyCode' => $offerListing['Price']['CurrencyCode'] ]; } if(isset($old_price['Amount']) && isset($old_price['CurrencyCode'])) { $amount = floatval($old_price['Amount']) / 100; $currency = $old_price['CurrencyCode']; $old_price = new Money($amount, new Currency($currency)); } } $old_price = apply_filters('aff_amazon_helper_find_old_price', $old_price, $item); return $old_price; } /** * Find the product variant attributes in the Amazon API item response. * * @since 0.9 * @param array $item The Amazon API response converted from XML to an array. * @param bool $create_missing * @return Attribute[] */ public static function find_attributes(array $item, $create_missing = true) { /** @var Attribute_Template_Repository_Interface $attribute_template_repository */ $attribute_template_repository = \Affilicious::get('affilicious.attribute.repository.attribute_template'); /** @var Attribute_Template_Factory_Interface $attribute_template_factory */ $attribute_template_factory = \Affilicious::get('affilicious.attribute.factory.attribute_template'); $attributes = array(); if(isset($item['VariationAttributes']['VariationAttribute'])) { $variation_attributes = $item['VariationAttributes']['VariationAttribute']; foreach ($variation_attributes as $variation_attribute) { // Find the attribute template $attribute_template = $attribute_template_repository->find_one_by_name(new Name($variation_attribute['Name'])); if($attribute_template === null) { $attribute_template = $attribute_template_factory->create_from_name(new Name($variation_attribute['Name']), Type::text()); if($create_missing) { $attribute_template_repository->store($attribute_template); } } // Build the attribute from the template. $attribute = $attribute_template->build(new Value($variation_attribute['Value'])); $attributes[] = $attribute; } } $attributes = apply_filters('aff_amazon_helper_find_attributes', $attributes, $item); return $attributes; } /** * Find the shops in the Amazon API item response. * * @param array $item The Amazon API response converted from XML to an array. * @param Shop_Template_Id|null $shop_template_id * @param bool $create_missing * @return Shop|null */ public static function find_shop(array $item, Shop_Template_Id $shop_template_id = null, $create_missing = true) { /** @var Shop_Template_Repository_Interface $shop_template_repository */ $shop_template_repository = \Affilicious::get('affilicious.shop.repository.shop_template'); /** @var Shop_Template_Factory_Interface $shop_template_factory */ $shop_template_factory = \Affilicious::get('affilicious.shop.factory.shop_template'); /** @var \Affilicious\Provider\Repository\Provider_Repository_Interface $provider_repository **/ $provider_repository = \Affilicious::get('affilicious.provider.repository.provider'); $tracking = self::find_tracking($item); if($tracking === null) { return null; } $pricing = self::find_pricing($item); if($pricing === null) { return null; } if($shop_template_id !== null) { $shop_template = $shop_template_repository->find_one_by_id($shop_template_id); if($shop_template === null) { return null; } } else { $shop_template = $shop_template_repository->find_one_by_name(new Name('Amazon')); if($shop_template === null) { $shop_template = $shop_template_factory->create_from_name(new Name('Amazon')); // Find the related Amazon provider $amazon_provider = $provider_repository->find_one_by_slug(new Slug('amazon')); if($amazon_provider !== null) { $shop_template->set_provider_id($amazon_provider->get_id()); } if($create_missing) { $shop_template_repository->store($shop_template); } } } $shop = $shop_template->build($tracking, $pricing); $shop = apply_filters('aff_amazon_helper_find_shop', $shop, $item); return $shop; } /** * Create the product from the Amazon API response item. * * @since 0.9 * @param array $item The Amazon API response converted from XML to an array. * @param array $config * @param Complex_Product|null $parent * @return Product|\WP_Error */ public static function create_product(array $item, array $config, Complex_Product $parent = null) { /** @var Slug_Generator_Interface $slug_generator */ $slug_generator = \Affilicious::get('affilicious.common.generator.slug'); $name = new Name($item['ItemAttributes']['Title']); $slug = $slug_generator->generate_from_name($name); if (!empty($config['variants']) && isset($item['Variations'])) { $product = new Complex_Product($name, $slug); } elseif(!empty($config['variants']) && $parent !== null && isset($item['VariationAttributes'])) { $product = new Product_Variant($parent, $name, $slug); } else { $product = new Simple_Product($name, $slug); } if ($product instanceof Complex_Product) { $variant_items = $item['Variations']['Item']; foreach ($variant_items as $variant_item) { // The variants doesn't have an affiliate link and images. $variant_item = wp_parse_args($variant_item, [ 'DetailPageURL' => $item['DetailPageURL'], 'SmallImage' => isset($item['SmallImage']) ? $item['SmallImage'] : null, 'MediumImage' => isset($item['MediumImage']) ? $item['MediumImage'] : null, 'LargeImage' => isset($item['LargeImage']) ? $item['LargeImage'] : null, 'ImageSets' => isset($item['ImageSets']) ? $item['ImageSets'] : null, ]); /** @var Product_Variant $product_variant */ $product_variant = self::create_product($variant_item, $config, $product); if($product_variant !== null) { $product->add_variant($product_variant); } } $default_variant = $product->get_default_variant(); if ($default_variant !== null) { $variant_thumbnail = $default_variant->get_thumbnail(); $product->set_thumbnail($variant_thumbnail); } } if ($product instanceof Product_Variant) { $attributes = Amazon_Helper::find_attributes($item, !empty($config['store_attributes'])); foreach ($attributes as $attribute) { $product->add_attribute($attribute); } } if ($product instanceof Shop_Aware_Interface) { $shop = Amazon_Helper::find_shop($item, !empty($config['shop_template_id']) ? $config['shop_template_id'] : null, !empty($config['store_shop'])); if ($shop !== null) { $product->add_shop($shop); } } $thumbnail = Amazon_Helper::find_thumbnail($item, !empty($config['store_thumbnail'])); if ($thumbnail !== null) { $product->set_thumbnail($thumbnail); } $image_gallery = Amazon_Helper::find_image_gallery($item, !empty($config['store_image_gallery'])); if (!empty($image_gallery)) { $product->set_image_gallery($image_gallery); } if(isset($item['ParentASIN'])) { $product->add_custom_value('amazon_parent_asin', $item['ParentASIN'] ); } return $product; } }