/**
  * @param \Magento\Catalog\Model\Product $parentProduct
  * @param array $filterArray
  * @return bool|\Magento\Catalog\Model\Product
  */
 protected function loadSimpleVariation(\Magento\Catalog\Model\Product $parentProduct, array $filterArray)
 {
     $childProduct = $this->swatchHelperData->loadVariationByFallback($parentProduct, $filterArray);
     if ($childProduct && !$childProduct->getImage()) {
         $childProduct = $this->swatchHelperData->loadFirstVariationWithImage($parentProduct, $filterArray);
     }
     if (!$childProduct) {
         $childProduct = $parentProduct;
     }
     return $childProduct;
 }
Esempio n. 2
0
 protected function getProductVariationWithMedia(Product $currentConfigurable, array $attributes)
 {
     $product = null;
     $layeredAttributes = [];
     $configurableAttributes = $this->swatchHelper->getAttributesFromConfigurable($currentConfigurable);
     if ($configurableAttributes) {
         $layeredAttributes = $this->getLayeredAttributesIfExists($configurableAttributes);
     }
     $resultAttributes = array_merge($layeredAttributes, $attributes);
     $product = $this->swatchHelper->loadVariationByFallback($currentConfigurable, $resultAttributes);
     if (!$product || (!$product->getImage() || $product->getImage() == 'no_selection')) {
         $product = $this->swatchHelper->loadFirstVariationWithImage($currentConfigurable, $resultAttributes);
     }
     return $product;
 }
Esempio n. 3
0
 /**
  * @SuppressWarnings(PHPMD.ElseExpression)
  * {@inheritDoc}
  */
 public function loadVariationByFallback(Product $parentProduct, array $attributes)
 {
     $variation = false;
     if ($this->isProductHasSwatch($parentProduct) && $parentProduct->getDocumentSource() !== null) {
         $documentSource = $parentProduct->getDocumentSource();
         $childrenIds = isset($documentSource['children_ids']) ? $documentSource['children_ids'] : [];
         if (!empty($childrenIds)) {
             $childrenIds = array_map('intval', $childrenIds);
             $productCollection = $this->productCollectionFactory->create();
             $productCollection->addIdFilter($childrenIds);
             $configurableAttributes = $this->getAttributesFromConfigurable($parentProduct);
             $allAttributesArray = [];
             foreach ($configurableAttributes as $attribute) {
                 foreach ($attribute->getOptions() as $option) {
                     $allAttributesArray[$attribute['attribute_code']][] = (int) $option->getValue();
                 }
             }
             $resultAttributesToFilter = array_merge($attributes, array_diff_key($allAttributesArray, $attributes));
             $this->addFilterByAttributes($productCollection, $resultAttributesToFilter);
             $variationProduct = $productCollection->getFirstItem();
             if ($variationProduct && $variationProduct->getId()) {
                 $variation = $this->productRepository->getById($variationProduct->getId());
             }
         }
     } else {
         $variation = parent::loadVariationByFallback($parentProduct, $attributes);
     }
     return $variation;
 }
Esempio n. 4
0
 public function testLoadFirstVariationWithImageNoProduct()
 {
     $result = $this->swatchHelperObject->loadVariationByFallback($this->productMock, ['color' => 31]);
     $this->assertFalse($result);
 }