/** * Returns Configurable Products Collection with added swatch attributes * * @param ConfigurableProduct $subject * @param Collection $result * @return Collection * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterGetUsedProductCollection(ConfigurableProductType $subject, Collection $result) { $attributeCodes = ['image']; $entityType = $result->getEntity()->getType(); foreach ($this->eavConfig->getEntityAttributeCodes($entityType) as $code) { $attribute = $this->eavConfig->getAttribute($entityType, $code); if ($this->swatchHelper->isVisualSwatch($attribute) || $this->swatchHelper->isTextSwatch($attribute)) { $attributeCodes[] = $code; } } $result->addAttributeToSelect($attributeCodes); return $result; }
/** * Save default swatch value using Swatch model instead of Eav model * * @param Attribute $attribute * @return void */ protected function saveDefaultSwatchOptionValue(Attribute $attribute) { if (!$this->swatchHelper->isSwatchAttribute($attribute)) { return; } $defaultValue = $attribute->getData('default/0'); if (!empty($defaultValue)) { /** @var \Magento\Swatches\Model\Swatch $swatch */ $swatch = $this->swatchFactory->create(); if (substr($defaultValue, 0, 6) == self::BASE_OPTION_TITLE) { $defaultValue = $this->dependencyArray[$defaultValue]; } $swatch->getResource()->saveDefaultSwatchOption($attribute->getId(), $defaultValue); } }
public function testAfterAfterSaveIsSwatchExists() { $this->abstractSource->expects($this->once())->method('getAllOptions')->willReturn($this->allOptions); $this->resource->expects($this->once())->method('saveDefaultSwatchOption')->with(self::ATTRIBUTE_ID, self::OPTION_ID); $this->swatch->expects($this->once())->method('getResource')->willReturn($this->resource); $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')->willReturn($this->optionIds); $this->attribute->expects($this->at(1))->method('getSource')->willReturn($this->abstractSource); $this->attribute->expects($this->at(2))->method('getData')->with('default/0')->willReturn($this->dependencyArray[0]); $this->attribute->expects($this->at(3))->method('getId')->willReturn(self::ATTRIBUTE_ID); $this->attribute->expects($this->at(4))->method('getData')->with('swatch/value')->willReturn([self::STORE_ID => [self::OPTION_ID => null]]); $this->attribute->expects($this->at(5))->method('getData')->with('option/delete/' . self::OPTION_ID)->willReturn(false); $this->swatchFactory->expects($this->exactly(1))->method('create')->willReturn($this->swatch); $this->swatchHelper->expects($this->exactly(2))->method('isSwatchAttribute')->with($this->attribute)->willReturn(true); $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); }