Skip to content

Issue-3139. Visual Swatch not showing on product page when once set as Text Swatch #15316

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 4 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
37 changes: 31 additions & 6 deletions app/code/Magento/Swatches/Model/Plugin/EavAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Swatches\Model\Swatch;
use Magento\Swatches\Model\ResourceModel\Swatch as SwatchResource;

Expand Down Expand Up @@ -65,25 +66,33 @@ class EavAttribute
*/
private $serializer;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param \Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory
* @param \Magento\Swatches\Model\SwatchFactory $swatchFactory
* @param \Magento\Swatches\Helper\Data $swatchHelper
* @param Json|null $serializer
* @param SwatchResource|null $swatchResource
* @param StoreManagerInterface|null $storeManager
*/
public function __construct(
\Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory,
\Magento\Swatches\Model\SwatchFactory $swatchFactory,
\Magento\Swatches\Helper\Data $swatchHelper,
Json $serializer = null,
SwatchResource $swatchResource = null
SwatchResource $swatchResource = null,
StoreManagerInterface $storeManager = null
) {
$this->swatchCollectionFactory = $collectionFactory;
$this->swatchFactory = $swatchFactory;
$this->swatchHelper = $swatchHelper;
$this->serializer = $serializer ?: ObjectManager::getInstance()->create(Json::class);
$this->swatchResource = $swatchResource ?: ObjectManager::getInstance()->create(SwatchResource::class);
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->create(StoreManagerInterface::class);
}

/**
Expand Down Expand Up @@ -256,25 +265,41 @@ protected function saveSwatchParams(Attribute $attribute)
* Save Visual Swatch data
*
* @param Attribute $attribute
*
* @return void
*/
protected function processVisualSwatch(Attribute $attribute)
{
$swatchArray = $attribute->getData('swatch/value');

if (isset($swatchArray) && is_array($swatchArray)) {
foreach ($swatchArray as $optionId => $value) {
foreach ($swatchArray as $key => $value) {
$optionId = $key;
$storeValues = [
self::DEFAULT_STORE_ID => $value,
$this->storeManager->getStore()->getId() => '',
];

$optionId = $this->getAttributeOptionId($optionId);
$isOptionForDelete = $this->isOptionForDelete($attribute, $optionId);

if ($optionId === null || $isOptionForDelete) {
//option was deleted by button with basket
continue;
}
$swatch = $this->loadSwatchIfExists($optionId, self::DEFAULT_STORE_ID);

$swatchType = $this->determineSwatchType($value);
$defaultSwatchValue = reset($storeValues);
foreach ($storeValues as $storeId => $storeValue) {
if (!$storeValue) {
$storeValue = $defaultSwatchValue;
}

$swatch = $this->loadSwatchIfExists($optionId, $storeId);
$swatchType = $this->determineSwatchType($storeValue);

$this->saveSwatchData($swatch, $optionId, self::DEFAULT_STORE_ID, $swatchType, $value);
$this->isSwatchExists = null;
$this->saveSwatchData($swatch, $optionId, $storeId, $swatchType, $storeValue);
$this->isSwatchExists = null;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use Magento\Swatches\Model\Plugin\EavAttribute;
use Magento\Swatches\Model\Swatch;

/**
* Class EavAttributeTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class EavAttributeTest extends \PHPUnit\Framework\TestCase
{
const ATTRIBUTE_ID = 123;
Expand Down Expand Up @@ -45,6 +49,12 @@ class EavAttributeTest extends \PHPUnit\Framework\TestCase
/** @var \Magento\Swatches\Model\ResourceModel\Swatch\Collection|\PHPUnit_Framework_MockObject_MockObject */
private $collection;

/** @var \Magento\Store\Model\StoreManager|\PHPUnit_Framework_MockObject_MockObject */
private $storeManager;

/** @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject */
private $store;

/** @var array */
private $optionIds = [];

Expand All @@ -61,6 +71,8 @@ protected function setUp()
$this->swatchHelper = $this->createMock(\Magento\Swatches\Helper\Data::class);
$this->swatch = $this->createMock(\Magento\Swatches\Model\Swatch::class);
$this->resource = $this->createMock(\Magento\Swatches\Model\ResourceModel\Swatch::class);
$this->storeManager = $this->createPartialMock(\Magento\Store\Model\StoreManager::class, ['getStore']);
$this->store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getId']);
$this->collection =
$this->createMock(\Magento\Swatches\Model\ResourceModel\Swatch\Collection::class);
$this->collectionFactory = $this->createPartialMock(
Expand Down Expand Up @@ -92,6 +104,7 @@ protected function setUp()
'swatchFactory' => $this->swatchFactory,
'swatchHelper' => $this->swatchHelper,
'serializer' => $serializer,
'storeManager' => $this->storeManager
]
);

Expand Down Expand Up @@ -337,26 +350,26 @@ public function testAfterAfterSaveVisualSwatch($swatchType, $swatchValue)

$this->swatch->expects($this->once())->method('getResource')
->willReturn($this->resource);
$this->swatch->expects($this->once())->method('getId')
$this->swatch->expects($this->exactly(2))->method('getId')
->willReturn(EavAttribute::DEFAULT_STORE_ID);
$this->swatch->expects($this->once())->method('save');
$this->swatch->expects($this->exactly(4))->method('setData')
$this->swatch->expects($this->exactly(2))->method('save');
$this->swatch->expects($this->exactly(8))->method('setData')
->withConsecutive(
['option_id', self::OPTION_ID],
['store_id', EavAttribute::DEFAULT_STORE_ID],
['type', $swatchType],
['value', $swatchValue]
);

$this->collection->expects($this->exactly(2))->method('addFieldToFilter')
$this->collection->expects($this->exactly(4))->method('addFieldToFilter')
->withConsecutive(
['option_id', self::OPTION_ID],
['store_id', EavAttribute::DEFAULT_STORE_ID]
)->willReturnSelf();

$this->collection->expects($this->once())->method('getFirstItem')
$this->collection->expects($this->exactly(2))->method('getFirstItem')
->willReturn($this->swatch);
$this->collectionFactory->expects($this->once())->method('create')
$this->collectionFactory->expects($this->exactly(2))->method('create')
->willReturn($this->collection);

$this->attribute->expects($this->at(0))->method('getData')
Expand Down Expand Up @@ -384,6 +397,8 @@ public function testAfterAfterSaveVisualSwatch($swatchType, $swatchValue)
->with($this->attribute)
->willReturn(true);
$this->swatchHelper->expects($this->never())->method('isTextSwatch');
$this->storeManager->expects($this->once())->method('getStore')->willReturn($this->store);
$this->store->expects($this->once())->method('getId')->willReturn(1);

$this->eavAttribute->afterAfterSave($this->attribute);
}
Expand Down Expand Up @@ -545,6 +560,8 @@ public function testAfterAfterSaveVisualSwatchIsDelete()
$this->swatchHelper->expects($this->once())->method('isVisualSwatch')
->with($this->attribute)
->willReturn(true);
$this->storeManager->expects($this->once())->method('getStore')->willReturn($this->store);
$this->store->expects($this->once())->method('getId')->willReturn(1);
$this->swatchHelper->expects($this->never())->method('isTextSwatch');

$this->eavAttribute->afterAfterSave($this->attribute);
Expand Down