/** * Get Swatch config data * * @return string */ public function getJsonSwatchConfig() { $attributesData = $this->getSwatchAttributesData(); $allOptionIds = $this->getConfigurableOptionsIds($attributesData); $swatchesData = $this->swatchHelper->getSwatchesByOptionsId($allOptionIds); $config = []; foreach ($attributesData as $attributeId => $attributeDataArray) { if (isset($attributeDataArray['options'])) { $config[$attributeId] = $this->addSwatchDataForAttribute($attributeDataArray['options'], $swatchesData, $attributeDataArray); } } return $this->jsonEncoder->encode($config); }
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]); }
/** * @return array */ public function getSwatchData() { if (false === $this->eavAttribute instanceof Attribute) { throw new \RuntimeException('Magento_Swatches: RenderLayered: Attribute has not been set.'); } $attributeOptions = []; foreach ($this->eavAttribute->getOptions() as $option) { if ($currentOption = $this->getFilterOption($this->filter->getItems(), $option)) { $attributeOptions[$option->getValue()] = $currentOption; } elseif ($this->isShowEmptyResults()) { $attributeOptions[$option->getValue()] = $this->getUnusedOption($option); } } $attributeOptionIds = array_keys($attributeOptions); $swatches = $this->swatchHelper->getSwatchesByOptionsId($attributeOptionIds); $data = ['attribute_id' => $this->eavAttribute->getId(), 'attribute_code' => $this->eavAttribute->getAttributeCode(), 'attribute_label' => $this->eavAttribute->getStoreLabel(), 'options' => $attributeOptions, 'swatches' => $swatches]; return $data; }