/** * @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $subject * @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $result * @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ public function afterSave(\Magento\Catalog\Model\ResourceModel\Eav\Attribute $subject, \Magento\Catalog\Model\ResourceModel\Eav\Attribute $result) { if ($this->swatchHelper->isSwatchAttribute($subject)) { $this->typeList->invalidate(Block::TYPE_IDENTIFIER); $this->typeList->invalidate(Collection::TYPE_IDENTIFIER); } return $result; }
/** * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @param \Magento\LayeredNavigation\Block\Navigation\FilterRenderer $subject * @param \Closure $proceed * @param \Magento\Catalog\Model\Layer\Filter\FilterInterface $filter * @return mixed * @throws \Magento\Framework\Exception\LocalizedException */ public function aroundRender(\Magento\LayeredNavigation\Block\Navigation\FilterRenderer $subject, \Closure $proceed, \Magento\Catalog\Model\Layer\Filter\FilterInterface $filter) { if ($filter->hasAttributeModel()) { if ($this->swatchHelper->isSwatchAttribute($filter->getAttributeModel())) { return $this->layout->createBlock($this->block)->setSwatchFilter($filter)->toHtml(); } } return $proceed($filter); }
public function testAroundRenderFalse() { $attributeMock = $this->getMock('\\Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute', [], [], '', false); $this->filterMock->expects($this->atLeastOnce())->method('getAttributeModel')->willReturn($attributeMock); $this->filterMock->expects($this->once())->method('hasAttributeModel')->willReturn(true); $this->swatchHelperMock->expects($this->once())->method('isSwatchAttribute')->with($attributeMock)->willReturn(false); $result = $this->plugin->aroundRender($this->filterRendererMock, $this->closureMock, $this->filterMock); $this->assertEquals($result, $this->filterMock); }
protected function canReplaceImageWithSwatch($expected) { $this->swatchesHelperMock->expects($this->once())->method('isSwatchAttribute')->with($this->attributeMock)->willReturn($expected['isSwatchAttribute']); $this->attributeMock->expects($this->exactly($expected['getUsedInProductListing_count']))->method('getUsedInProductListing')->willReturn($expected['getUsedInProductListing']); $this->attributeMock->expects($this->exactly($expected['getIsFilterable_count']))->method('getIsFilterable')->willReturn($expected['getIsFilterable']); if ($expected['update_product_preview_image__count'] == 1) { $this->attributeMock->method('getData')->with('update_product_preview_image')->willReturn($expected['update_product_preview_image']); } }
/** * {@inheritDoc} */ protected function canRenderFilter() { $canRenderFilter = false; try { $attribute = $this->getFilter()->getAttributeModel(); $canRenderFilter = $this->swatchHelper->isSwatchAttribute($attribute); } catch (\Exception $e) { } return $canRenderFilter; }
/** * Retrieve list of attributes * * @param array $ids * @return array */ public function getAttributes($ids) { $collection = $this->collectionFactory->create(); $collection->addFieldToFilter('main_table.attribute_id', $ids); $attributes = []; foreach ($collection->getItems() as $attribute) { $attributes[] = ['id' => $attribute->getId(), 'label' => $attribute->getFrontendLabel(), 'code' => $attribute->getAttributeCode(), 'options' => $attribute->getSource()->getAllOptions(false), 'canCreateOption' => !$this->dataHelper->isSwatchAttribute($attribute)]; } return $attributes; }
public function testExecuteNullProduct() { $this->productMock->expects($this->once())->method('load')->with(59)->willReturn($this->productMock); $this->productModelFactoryMock->expects($this->once())->method('create')->willReturn($this->productMock); $this->swatchHelperMock->expects($this->once())->method('getAttributesFromConfigurable')->with($this->productMock)->willReturn([$this->attributeMock]); $this->swatchHelperMock->expects($this->once())->method('loadVariationByFallback')->with($this->productMock, ['size' => 454])->willReturn(null); $this->swatchHelperMock->expects($this->once())->method('getProductMediaGallery')->with($this->productMock)->willReturn($this->mediaGallery); $this->jsonMock->expects($this->once())->method('setData')->with($this->mediaGallery)->will($this->returnSelf()); $result = $this->controller->execute(); $this->assertInstanceOf('\\Magento\\Framework\\Controller\\Result\\Json', $result); }
/** * 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; }
public function testGetAttributes() { $ids = [1, 2, 3]; $result = [['id' => 'id', 'label' => 'label', 'code' => 'code', 'options' => ['options'], 'canCreateOption' => false]]; $this->collectionMock->expects($this->any())->method('addFieldToFilter')->with('main_table.attribute_id', $ids); $this->attributeMock->expects($this->once())->method('getId')->will($this->returnValue('id')); $this->attributeMock->expects($this->once())->method('getFrontendLabel')->will($this->returnValue('label')); $this->attributeMock->expects($this->once())->method('getAttributeCode')->will($this->returnValue('code')); $source = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\Source\\AbstractSource', [], [], '', false); $source->expects($this->once())->method('getAllOptions')->with(false)->will($this->returnValue(['options'])); $this->attributeMock->expects($this->once())->method('getSource')->will($this->returnValue($source)); $this->swatchHelper->expects($this->once())->method('isSwatchAttribute')->with($this->attributeMock)->willReturn(true); $this->assertEquals($result, $this->attributeListModel->getAttributes($ids)); }
public function testAfterGetUsedProductCollection() { $subject = $this->getMock('\\Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable', [], [], '', false); $result = $this->getMock('\\Magento\\ConfigurableProduct\\Model\\ResourceModel\\Product\\Type\\Configurable\\Product\\Collection', ['getEntity', 'addAttributeToSelect'], [], '', false); $collectionEntity = $this->getMock('\\Magento\\Eav\\Model\\Entity\\Collection\\AbstractCollection', ['getType'], [], '', false); $collectionEntity->expects($this->once())->method('getType')->willReturn('catalog'); $result->expects($this->once())->method('getEntity')->willReturn($collectionEntity); $attribute = $this->getMock('\\Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute', [], [], '', false); $this->eavConfig->expects($this->once())->method('getEntityAttributeCodes')->with('catalog')->willReturn(['size', 'color', 'swatch1']); $this->eavConfig->expects($this->exactly(3))->method('getAttribute')->willReturn($attribute); $this->swatchHelper->expects($this->exactly(3))->method('isVisualSwatch')->with($attribute)->willReturn(true); $result->expects($this->once())->method('addAttributeToSelect')->with($this->identicalTo(['image', 'size', 'color', 'swatch1']))->willReturn($result); $result = $this->pluginModel->afterGetUsedProductCollection($subject, $result); $this->assertInstanceOf('\\Magento\\ConfigurableProduct\\Model\\ResourceModel\\Product\\Type\\Configurable\\Product\\Collection', $result); }
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; }
/** * @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; }
/** * Generate Product Media array * * @param string $attributeCode * @param integer $optionId * @return array */ protected function getVariationMedia($attributeCode, $optionId) { $variationProduct = $this->swatchHelper->loadFirstVariationWithSwatchImage($this->getProduct(), [$attributeCode => $optionId]); $variationMediaArray = []; if ($variationProduct) { $variationMediaArray = ['value' => $this->getSwatchProductImage($variationProduct, 'swatch_image'), 'thumb' => $this->getSwatchProductImage($variationProduct, 'swatch_thumb')]; } return $variationMediaArray; }
/** * Generate Product Media array * * @param string $attributeCode * @param integer $optionId * @return array */ protected function getVariationMedia($attributeCode, $optionId) { $variationProduct = $this->swatchHelper->loadFirstVariationWithSwatchImage($this->getProduct(), $attributeCode, $optionId); $variationMediaArray = []; if ($variationProduct) { $variationMediaArray = ['value' => $this->getSwatchProductImage($variationProduct, Swatch::SWATCH_IMAGE_NAME), 'thumb' => $this->getSwatchProductImage($variationProduct, Swatch::SWATCH_THUMBNAIL_NAME)]; } return $variationMediaArray; }
/** * Check if we can replace original image with swatch image on catalog/category/list page * * @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute * @return bool */ protected function canReplaceImageWithSwatch($attribute) { $result = true; if (!$this->swatchHelperData->isSwatchAttribute($attribute)) { $result = false; } if (!$attribute->getUsedInProductListing() || !$attribute->getIsFilterable() || !$attribute->getData('update_product_preview_image')) { $result = false; } return $result; }
/** * @covers Magento\Swatches\Block\Product\Renderer\Listing\Configurable::getSwatchAttributesData */ public function testGetJsonSwatchUsedInProductListing() { $products = [1 => 'testA', 3 => 'testB']; $expected = ['type' => null, 'value' => 'hello', 'label' => $products[3]]; $this->prepareGetJsonSwatchConfig(); $this->configurable->setProduct($this->product); $this->swatchHelper->expects($this->once())->method('getSwatchAttributesAsArray')->with($this->product)->willReturn([1 => ['options' => $products, 'use_product_image_for_swatch' => true, 'used_in_product_listing' => true, 'attribute_code' => 'code']]); $this->swatchHelper->expects($this->once())->method('getSwatchesByOptionsId')->with([1, 3])->willReturn([3 => ['type' => $expected['type'], 'value' => $expected['value']]]); $this->jsonEncoder->expects($this->once())->method('encode'); $this->configurable->getJsonSwatchConfig(); }
/** * @dataProvider dataIsSwatchAttribute */ public function testIsSwatchAttribute($times, $swatchType, $boolResult) { $this->attributeMock->method('hasData')->with('swatch_input_type')->willReturn(true); $this->attributeMock->expects($this->exactly($times))->method('getData')->with('swatch_input_type')->willReturn($swatchType); $result = $this->swatchHelperObject->isSwatchAttribute($this->attributeMock); if ($boolResult) { $this->assertTrue($result); } else { $this->assertFalse($result); } }
public function testGetJsonSwatchConfigWithoutVisualImageType() { $this->configurable->setProduct($this->product); $this->swatchHelper->expects($this->once())->method('getSwatchAttributesAsArray')->with($this->product)->willReturn([1 => ['options' => [1 => 'testA', 3 => 'testB'], 'use_product_image_for_swatch' => true, 'attribute_code' => 'code']]); $this->swatchHelper->expects($this->once())->method('getSwatchesByOptionsId')->with([1, 3])->willReturn([3 => ['type' => Swatch::SWATCH_TYPE_VISUAL_IMAGE, 'value' => 'hello']]); $this->swatchHelper->expects($this->once())->method('loadFirstVariationWithSwatchImage')->with($this->product, ['code' => 3])->willReturn($this->product); $this->swatchMediaHelper->expects($this->exactly(2))->method('getSwatchAttributeImage')->withConsecutive(['swatch_image', 'hello'], ['swatch_thumb', 'hello'])->willReturn('/path'); $this->product->expects($this->exactly(4))->method('getData')->withConsecutive(['swatch_image'], ['image'], ['swatch_image'], ['image'])->will($this->onConsecutiveCalls(null, null, null, null)); $this->imageHelper->expects($this->never())->method('init'); $this->imageHelper->expects($this->never())->method('resize'); $this->jsonEncoder->expects($this->once())->method('encode'); $this->configurable->getJsonSwatchConfig(); }
/** * Validate that attribute options exist * * @param Attribute $attribute * @return bool * @throws InputException */ protected function validateOptions(Attribute $attribute) { $options = null; if ($this->swatchHelper->isVisualSwatch($attribute)) { $options = $attribute->getData('optionvisual'); } elseif ($this->swatchHelper->isTextSwatch($attribute)) { $options = $attribute->getData('optiontext'); } if ($options && !$this->isOptionsValid($options, $attribute)) { throw new InputException(__('Admin is a required field in the each row')); } return true; }
/** * 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); } }
/** * @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; }
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); }