/** * 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; }
public function testGetSwatchesByOptionsIdIf3() { $swatchMock = $this->getMock('\\Magento\\Swatches\\Model\\Swatch', [], [], '', false); $optionsData = ['type' => 0, 'store_id' => 0, 'value' => 'test_test', 'option_id' => 35, 'id' => 423]; $swatchMock->expects($this->at(0))->method('offsetGet')->with('type')->willReturn(0); $swatchMock->expects($this->at(1))->method('offsetGet')->with('store_id')->willReturn(0); $swatchMock->expects($this->at(2))->method('offsetGet')->with('store_id')->willReturn(0); $swatchMock->expects($this->at(3))->method('offsetGet')->with('option_id')->willReturn(35); $swatchMock->expects($this->at(4))->method('getData')->with('')->willReturn($optionsData); $swatchCollectionMock = $this->objectManager->getCollectionMock('\\Magento\\Swatches\\Model\\ResourceModel\\Swatch\\Collection', [$swatchMock]); $this->swatchCollectionFactoryMock->method('create')->willReturn($swatchCollectionMock); $swatchCollectionMock->method('addFilterByOptionsIds')->with([35])->will($this->returnSelf()); $storeMock = $this->getMock('\\Magento\\Store\\Model\\Store', [], [], '', false); $this->storeManagerMock->method('getStore')->willReturn($storeMock); $storeMock->method('getId')->willReturn(1); $this->swatchHelperObject->getSwatchesByOptionsId([35]); }
public function testAfterAfterSaveNotSwatchAttribute() { $this->abstractSource->expects($this->once())->method('getAllOptions')->willReturn($this->allOptions); $this->swatch->expects($this->once())->method('getId')->willReturn(1); $this->swatch->expects($this->once())->method('save'); $this->swatch->expects($this->once())->method('isDeleted')->with(false); $this->swatch->expects($this->exactly(2))->method('setData')->withConsecutive(['type', Swatch::SWATCH_TYPE_TEXTUAL], ['value', null]); $this->collection->expects($this->exactly(2))->method('addFieldToFilter')->withConsecutive(['option_id', self::OPTION_ID], ['store_id', self::OPTION_ID])->willReturnSelf(); $this->collection->expects($this->once())->method('getFirstItem')->willReturn($this->swatch); $this->collectionFactory->expects($this->once())->method('create')->willReturn($this->collection); $this->attribute->expects($this->at(0))->method('getData')->with('option')->willReturn($this->optionIds); $this->attribute->expects($this->at(1))->method('getSource')->willReturn($this->abstractSource); $this->attribute->expects($this->at(2))->method('getData')->with('swatch/value')->willReturn([self::STORE_ID => [self::OPTION_ID => null]]); $this->attribute->expects($this->at(3))->method('getData')->with('option/delete/' . self::OPTION_ID)->willReturn(false); $this->swatchHelper->expects($this->exactly(2))->method('isSwatchAttribute')->with($this->attribute)->will($this->onConsecutiveCalls(true, false)); $this->swatchHelper->expects($this->once())->method('isVisualSwatch')->with($this->attribute)->willReturn(false); $this->swatchHelper->expects($this->once())->method('isTextSwatch')->with($this->attribute)->willReturn(true); $this->eavAttribute->afterAfterSave($this->attribute); }
/** * 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; }