/**
  * Load swatch if it exists in database
  *
  * @param int $optionId
  * @param int $storeId
  * @return Swatch
  */
 protected function loadSwatchIfExists($optionId, $storeId)
 {
     $collection = $this->swatchCollectionFactory->create();
     $collection->addFieldToFilter('option_id', $optionId);
     $collection->addFieldToFilter('store_id', $storeId);
     $collection->setPageSize(1);
     $loadedSwatch = $collection->getFirstItem();
     if ($loadedSwatch->getId()) {
         $this->isSwatchExists = true;
     }
     return $loadedSwatch;
 }
Exemple #2
0
 /**
  * Get swatch options by option id's according to fallback logic
  *
  * @param array $optionIds
  * @return array
  */
 public function getSwatchesByOptionsId(array $optionIds)
 {
     /** @var \Magento\Swatches\Model\ResourceModel\Swatch\Collection $swatchCollection */
     $swatchCollection = $this->swatchCollectionFactory->create();
     $swatchCollection->addFilterByOptionsIds($optionIds);
     $swatches = [];
     $currentStoreId = $this->storeManager->getStore()->getId();
     foreach ($swatchCollection as $item) {
         if ($item['type'] != Swatch::SWATCH_TYPE_TEXTUAL) {
             $swatches[$item['option_id']] = $item->getData();
         } elseif ($item['store_id'] == $currentStoreId && $item['value']) {
             $fallbackValues[$item['option_id']][$currentStoreId] = $item->getData();
         } elseif ($item['store_id'] == self::DEFAULT_STORE_ID) {
             $fallbackValues[$item['option_id']][self::DEFAULT_STORE_ID] = $item->getData();
         }
     }
     if (!empty($fallbackValues)) {
         $swatches = $this->addFallbackOptions($fallbackValues, $swatches);
     }
     return $swatches;
 }