Skip to content

Commit

Permalink
Merge pull request #11 from searchspring/fix-issue-with-layout-update…
Browse files Browse the repository at this point in the history
…-attribute

fixed issue with invalid processing of option text for the layout upd…
  • Loading branch information
maheshganjalagunte authored Oct 5, 2023
2 parents efecadd + f79ae69 commit 22b88b6
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
13 changes: 11 additions & 2 deletions Model/Feed/DataProvider/Attribute/ValueProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
namespace SearchSpring\Feed\Model\Feed\DataProvider\Attribute;

use Exception;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Eav\Model\Entity\Attribute\Source\SpecificSourceInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;

Expand All @@ -35,7 +37,7 @@ class ValueProcessor
* @throws LocalizedException
* @throws Exception
*/
public function getValue(Attribute $attribute, $value)
public function getValue(Attribute $attribute, $value, Product $product)
{
$key = null;
$code = $attribute->getAttributeCode();
Expand All @@ -48,7 +50,14 @@ public function getValue(Attribute $attribute, $value)

$result = null;
if ($this->isSourceAttribute($attribute)) {
$result = $attribute->getSource()->getOptionText($value);
$source = $attribute->getSource();
if ($source instanceof SpecificSourceInterface) {
$sourceClone = clone $source;
$sourceClone->getOptionsFor($product);
$result = $sourceClone->getOptionText($value);
} else {
$result = $source->getOptionText($value);
}
} else {
$result = $value;
}
Expand Down
2 changes: 1 addition & 1 deletion Model/Feed/DataProvider/AttributesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private function getProductData(Product $product) : array
}
/** @var Attribute $attribute */
$attribute = $this->attributes[$key];
$result[$key] = $this->valueProcessor->getValue($attribute, $fieldValue);
$result[$key] = $this->valueProcessor->getValue($attribute, $fieldValue, $product);
}

return $result;
Expand Down
2 changes: 1 addition & 1 deletion Model/Feed/DataProvider/Product/GetChildProductsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function getProductData(
continue;
}

$value = $this->valueProcessor->getValue($childAttribute, $child->getData($code));
$value = $this->valueProcessor->getValue($childAttribute, $child->getData($code), $child);
if ($value != '' && !empty($value)) {
$result[$code][] = $value;
}
Expand Down
18 changes: 14 additions & 4 deletions Test/Unit/Model/Feed/DataProvider/Attribute/ValueProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace SearchSpring\Feed\Test\Unit\Model\Feed\DataProvider\Attribute;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
use Magento\Framework\Exception\LocalizedException;
Expand All @@ -39,6 +40,9 @@ public function setUp(): void
*/
public function testGetValue()
{
$productMock = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->getMock();
$attributeMock = $this->getMockBuilder(Attribute::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -51,12 +55,15 @@ public function testGetValue()

$this->assertSame(
'test',
$this->valueProcessor->getValue($attributeMock, 'test')
$this->valueProcessor->getValue($attributeMock, 'test', $productMock)
);
}

public function testGetValueOnCache()
{
$productMock = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->getMock();
$abstractSourceMock = $this->createMock(AbstractSource::class);
$attributeMock = $this->getMockBuilder(Attribute::class)
->disableOriginalConstructor()
Expand All @@ -74,8 +81,8 @@ public function testGetValueOnCache()
->method('getOptionText')
->willReturn('test_option_text');

$this->valueProcessor->getValue($attributeMock, 'test');
$this->valueProcessor->getValue($attributeMock, 'test');
$this->valueProcessor->getValue($attributeMock, 'test', $productMock);
$this->valueProcessor->getValue($attributeMock, 'test', $productMock);
}

/**
Expand All @@ -84,6 +91,9 @@ public function testGetValueOnCache()
*/
public function testGetValueException()
{
$productMock = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->getMock();
$attributeMock = $this->getMockBuilder(Attribute::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -102,6 +112,6 @@ public function testGetValueException()

$this->expectException(\Exception::class);

$this->valueProcessor->getValue($attributeMock, $attributeMock);
$this->valueProcessor->getValue($attributeMock, $attributeMock, $productMock);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "searchspring/magento2-module",
"type": "magento2-module",
"version": "2.0.0",
"version": "2.0.1",
"license": "GPL-3.0-only",
"require": {
"magento/module-customer": "^103.0"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="SearchSpring_Feed" setup_version="2.0.0">
<module name="SearchSpring_Feed" setup_version="2.0.1">
<sequence>
<module name="Magento_Store"/>
<module name="Magento_Customer"/>
Expand Down

0 comments on commit 22b88b6

Please sign in to comment.