Skip to content

Correctly provide list of swatch attributes for product #11703 #12237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions app/code/Magento/Swatches/Helper/Attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Swatches\Helper;

use Magento\Framework\Serialize\SerializerInterface;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Swatches\Model\Swatch;

/**
* Provide list of swatch attributes for product.
*/
class Attribute
{
/**
* @var SerializerInterface
*/
protected $serializer;

/**
* @param SerializerInterface $serializer
*/
public function __construct(
SerializerInterface $serializer
) {
$this->serializer = $serializer;
}

/**
* Get data key which should populated to attribute entity from "additional_data" field
*
* @return array
*/
public function getEavAttributeAdditionalDataKeys()
{
return [
Swatch::SWATCH_INPUT_TYPE_KEY,
'update_product_preview_image',
'use_product_image_for_swatch'
];
}

/**
* @param AbstractAttribute $attribute
* @return $this
*/
public function assembleAdditionalDataEavAttribute(AbstractAttribute $attribute)
{
$initialAdditionalData = [];
$additionalData = (string) $attribute->getData('additional_data');
if (!empty($additionalData)) {
$additionalData = $this->serializer->unserialize($additionalData);
if (is_array($additionalData)) {
$initialAdditionalData = $additionalData;
}
}

$dataToAdd = [];
foreach ($this->getEavAttributeAdditionalDataKeys() as $key) {
$dataValue = $attribute->getData($key);
if (null !== $dataValue) {
$dataToAdd[$key] = $dataValue;
}
}
$additionalData = array_merge($initialAdditionalData, $dataToAdd);
$attribute->setData('additional_data', $this->serializer->serialize($additionalData));
return $this;
}

/**
* Populate eav attribute with additional data from "additional_data" field
*
* @param AbstractAttribute $attribute
* @return $this
*/
public function populateAdditionalDataEavAttribute(AbstractAttribute $attribute)
{
$serializedAdditionalData = $attribute->getData('additional_data');

if ($serializedAdditionalData) {
$additionalData = $this->serializer->unserialize($serializedAdditionalData);

if (isset($additionalData) && is_array($additionalData)) {
foreach ($this->getEavAttributeAdditionalDataKeys() as $key) {
if (isset($additionalData[$key])) {
$attribute->setData($key, $additionalData[$key]);
}
}
}
}

return $this;
}

/**
* Check if an attribute is Swatch
*
* @param AbstractAttribute $attribute
* @return bool
*/
public function isSwatchAttribute(AbstractAttribute $attribute)
{
$result = $this->isVisualSwatch($attribute) || $this->isTextSwatch($attribute);
return $result;
}

/**
* Is attribute Visual Swatch
*
* @param AbstractAttribute $attribute
* @return bool
*/
public function isVisualSwatch(AbstractAttribute $attribute)
{
if (!$attribute->hasData(Swatch::SWATCH_INPUT_TYPE_KEY)) {
$this->populateAdditionalDataEavAttribute($attribute);
}
return $attribute->getData(Swatch::SWATCH_INPUT_TYPE_KEY) == Swatch::SWATCH_INPUT_TYPE_VISUAL;
}

/**
* Is attribute Textual Swatch
*
* @param AbstractAttribute $attribute
* @return bool
*/
public function isTextSwatch(AbstractAttribute $attribute)
{
if (!$attribute->hasData(\Magento\Swatches\Model\Swatch::SWATCH_INPUT_TYPE_KEY)) {
$this->populateAdditionalDataEavAttribute($attribute);
}
return $attribute->getData(Swatch::SWATCH_INPUT_TYPE_KEY) == Swatch::SWATCH_INPUT_TYPE_TEXT;
}
}
72 changes: 13 additions & 59 deletions app/code/Magento/Swatches/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory as SwatchCollectionFactory;
use Magento\Swatches\Model\Swatch;
use Magento\Swatches\Model\SwatchAttributesProvider;
use Magento\Swatches\Helper\Attribute as AttributeHelper;

/**
* Class Helper Data
Expand Down Expand Up @@ -76,30 +77,25 @@ class Data
*/
private $swatchAttributesProvider;

/**
* Data key which should populated to Attribute entity from "additional_data" field
*
* @var array
*/
protected $eavAttributeAdditionalDataKeys = [
Swatch::SWATCH_INPUT_TYPE_KEY,
'update_product_preview_image',
'use_product_image_for_swatch'
];

/**
* Serializer to/from JSON.
*
* @var Json
*/
private $serializer;

/**
* @var AttributeHelper
*/
protected $attributeHelper;

/**
* @param CollectionFactory $productCollectionFactory
* @param ProductRepositoryInterface $productRepository
* @param StoreManagerInterface $storeManager
* @param SwatchCollectionFactory $swatchCollectionFactory
* @param Image $imageHelper
* @param AttributeHelper $attributeHelper
* @param Json|null $serializer
* @param SwatchAttributesProvider $swatchAttributesProvider
*/
Expand All @@ -109,6 +105,7 @@ public function __construct(
StoreManagerInterface $storeManager,
SwatchCollectionFactory $swatchCollectionFactory,
Image $imageHelper,
AttributeHelper $attributeHelper,
Json $serializer = null,
SwatchAttributesProvider $swatchAttributesProvider = null
) {
Expand All @@ -120,6 +117,7 @@ public function __construct(
$this->serializer = $serializer ?: ObjectManager::getInstance()->create(Json::class);
$this->swatchAttributesProvider = $swatchAttributesProvider
?: ObjectManager::getInstance()->get(SwatchAttributesProvider::class);
$this->attributeHelper = $attributeHelper;
}

/**
Expand All @@ -128,44 +126,7 @@ public function __construct(
*/
public function assembleAdditionalDataEavAttribute(Attribute $attribute)
{
$initialAdditionalData = [];
$additionalData = (string) $attribute->getData('additional_data');
if (!empty($additionalData)) {
$additionalData = $this->serializer->unserialize($additionalData);
if (is_array($additionalData)) {
$initialAdditionalData = $additionalData;
}
}

$dataToAdd = [];
foreach ($this->eavAttributeAdditionalDataKeys as $key) {
$dataValue = $attribute->getData($key);
if (null !== $dataValue) {
$dataToAdd[$key] = $dataValue;
}
}
$additionalData = array_merge($initialAdditionalData, $dataToAdd);
$attribute->setData('additional_data', $this->serializer->serialize($additionalData));
return $this;
}

/**
* @param Attribute $attribute
* @return $this
*/
private function populateAdditionalDataEavAttribute(Attribute $attribute)
{
$serializedAdditionalData = $attribute->getData('additional_data');
if ($serializedAdditionalData) {
$additionalData = $this->serializer->unserialize($serializedAdditionalData);
if (isset($additionalData) && is_array($additionalData)) {
foreach ($this->eavAttributeAdditionalDataKeys as $key) {
if (isset($additionalData[$key])) {
$attribute->setData($key, $additionalData[$key]);
}
}
}
}
$this->attributeHelper->assembleAdditionalDataEavAttribute($attribute);
return $this;
}

Expand Down Expand Up @@ -527,8 +488,7 @@ public function isProductHasSwatch(Product $product)
*/
public function isSwatchAttribute(Attribute $attribute)
{
$result = $this->isVisualSwatch($attribute) || $this->isTextSwatch($attribute);
return $result;
return $this->attributeHelper->isSwatchAttribute($attribute);
}

/**
Expand All @@ -539,10 +499,7 @@ public function isSwatchAttribute(Attribute $attribute)
*/
public function isVisualSwatch(Attribute $attribute)
{
if (!$attribute->hasData(Swatch::SWATCH_INPUT_TYPE_KEY)) {
$this->populateAdditionalDataEavAttribute($attribute);
}
return $attribute->getData(Swatch::SWATCH_INPUT_TYPE_KEY) == Swatch::SWATCH_INPUT_TYPE_VISUAL;
return $this->attributeHelper->isVisualSwatch($attribute);
}

/**
Expand All @@ -553,10 +510,7 @@ public function isVisualSwatch(Attribute $attribute)
*/
public function isTextSwatch(Attribute $attribute)
{
if (!$attribute->hasData(Swatch::SWATCH_INPUT_TYPE_KEY)) {
$this->populateAdditionalDataEavAttribute($attribute);
}
return $attribute->getData(Swatch::SWATCH_INPUT_TYPE_KEY) == Swatch::SWATCH_INPUT_TYPE_TEXT;
return $this->attributeHelper->isTextSwatch($attribute);
}

/**
Expand Down
19 changes: 16 additions & 3 deletions app/code/Magento/Swatches/Model/SwatchAttributesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Catalog\Model\Product;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute;
use Magento\Swatches\Helper\Attribute as AttributeHelper;

/**
* Provide list of swatch attributes for product.
Expand All @@ -30,16 +31,24 @@ class SwatchAttributesProvider
*/
private $attributesPerProduct;

/**
* @var AttributeHelper
*/
protected $attributeHelper;

/**
* @param Configurable $typeConfigurable
* @param SwatchAttributeCodes $swatchAttributeCodes
* @param AttributeHelper $attributeHelper
*/
public function __construct(
Configurable $typeConfigurable,
SwatchAttributeCodes $swatchAttributeCodes
SwatchAttributeCodes $swatchAttributeCodes,
AttributeHelper $attributeHelper
) {
$this->typeConfigurable = $typeConfigurable;
$this->swatchAttributeCodes = $swatchAttributeCodes;
$this->attributeHelper = $attributeHelper;
}

/**
Expand All @@ -61,8 +70,12 @@ public function provide(Product $product)
$swatchAttributes = [];
foreach ($configurableAttributes as $configurableAttribute) {
if (array_key_exists($configurableAttribute->getAttributeId(), $swatchAttributeCodeMap)) {
$swatchAttributes[$configurableAttribute->getAttributeId()]
= $configurableAttribute->getProductAttribute();
$productAttribute = $configurableAttribute->getProductAttribute();

// Check if product attribute is actually an swatch attribute
if ($productAttribute && $this->attributeHelper->isSwatchAttribute($productAttribute)) {
$swatchAttributes[$configurableAttribute->getAttributeId()] = $productAttribute;
}
}
}
$this->attributesPerProduct[$product->getId()] = $swatchAttributes;
Expand Down
10 changes: 10 additions & 0 deletions app/code/Magento/Swatches/Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class DataTest extends \PHPUnit\Framework\TestCase
*/
private $swatchAttributesProvider;

/**
* @var \Magento\Swatches\Helper\Attribute|\PHPUnit_Framework_MockObject_MockObject
*/
private $attributeHelper;

protected function setUp()
{
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand Down Expand Up @@ -115,6 +120,10 @@ protected function setUp()
$this->swatchAttributesProvider = $this->getMockBuilder(SwatchAttributesProvider::class)
->disableOriginalConstructor()
->getMock();
$this->attributeHelper = $this->objectManager->getObject(
\Magento\Swatches\Helper\Attribute::class,
['serializer' => $serializer]
);

$this->swatchHelperObject = $this->objectManager->getObject(
\Magento\Swatches\Helper\Data::class,
Expand All @@ -127,6 +136,7 @@ protected function setUp()
'imageHelper' => $this->imageHelperMock,
'serializer' => $serializer,
'swatchAttributesProvider' => $this->swatchAttributesProvider,
'attributeHelper' => $this->attributeHelper
]
);
$this->objectManager->setBackwardCompatibleProperty(
Expand Down
Loading