Skip to content
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

Allow construction of products with custom_attributes in $data #24460

Merged
merged 4 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 10 additions & 9 deletions app/code/Magento/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,9 @@ public function __construct(
$this->mediaGalleryEntryConverterPool = $mediaGalleryEntryConverterPool;
$this->dataObjectHelper = $dataObjectHelper;
$this->joinProcessor = $joinProcessor;
$this->eavConfig = $config ?? ObjectManager::getInstance()->get(\Magento\Eav\Model\Config::class);
$this->filterCustomAttribute = $filterCustomAttribute
?? ObjectManager::getInstance()->get(FilterProductCustomAttribute::class);
parent::__construct(
$context,
$registry,
Expand All @@ -482,9 +485,6 @@ public function __construct(
$resourceCollection,
$data
);
$this->eavConfig = $config ?? ObjectManager::getInstance()->get(\Magento\Eav\Model\Config::class);
$this->filterCustomAttribute = $filterCustomAttribute
?? ObjectManager::getInstance()->get(FilterProductCustomAttribute::class);
}

/**
Expand Down Expand Up @@ -835,12 +835,13 @@ public function getStoreIds()
if (!$this->isObjectNew() && $this->_storeManager->isSingleStoreMode()) {
$websiteIds = array_keys($websiteIds);
}
foreach ($websiteIds as $websiteId) {
$websiteStores = $this->_storeManager->getWebsite($websiteId)->getStoreIds();
foreach ($websiteStores as $websiteStore) {
$storeIds []= $websiteStore;
}
}
$websiteStoreIds = array_map(
function ($websiteId): array {
return $this->_storeManager->getWebsite($websiteId)->getStoreIds();
},
$websiteIds
);
$storeIds = array_merge($storeIds, ...$websiteStoreIds);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewers: This change is only in the PR because the static tests complained about the array_merge in a loop. I didn't write that loop, but I don't mind changing it to get the tests green.

}
$this->setStoreIds($storeIds);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

namespace Magento\Catalog\Model;

use Magento\Eav\Model\Config as EavConfig;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\TestFramework\ObjectManager;

/**
* Tests product model:
Expand Down Expand Up @@ -49,8 +51,8 @@ protected function setUp()
}

/**
* @throws \Magento\Framework\Exception\FileSystemException
* @return void
* @throws \Magento\Framework\Exception\FileSystemException
*/
public static function tearDownAfterClass()
{
Expand Down Expand Up @@ -307,9 +309,9 @@ public function testIsSalable()
$this->_model = $this->productRepository->get('simple');

// fixture
$this->assertTrue((bool)$this->_model->isSalable());
$this->assertTrue((bool)$this->_model->isSaleable());
$this->assertTrue((bool)$this->_model->isAvailable());
$this->assertTrue((bool) $this->_model->isSalable());
$this->assertTrue((bool) $this->_model->isSaleable());
$this->assertTrue((bool) $this->_model->isAvailable());
$this->assertTrue($this->_model->isInStock());
}

Expand All @@ -324,9 +326,9 @@ public function testIsNotSalableWhenStatusDisabled()
$this->_model = $this->productRepository->get('simple');

$this->_model->setStatus(0);
$this->assertFalse((bool)$this->_model->isSalable());
$this->assertFalse((bool)$this->_model->isSaleable());
$this->assertFalse((bool)$this->_model->isAvailable());
$this->assertFalse((bool) $this->_model->isSalable());
$this->assertFalse((bool) $this->_model->isSaleable());
$this->assertFalse((bool) $this->_model->isAvailable());
$this->assertFalse($this->_model->isInStock());
}

Expand Down Expand Up @@ -585,7 +587,7 @@ public function testGetOptions()
continue;
}
foreach ($option->getValues() as $value) {
$this->assertEquals($expectedValue[$value->getSku()], (float)$value->getPrice());
$this->assertEquals($expectedValue[$value->getSku()], (float) $value->getPrice());
}
}
}
Expand Down Expand Up @@ -632,4 +634,40 @@ public function productWithBackordersDataProvider(): array
[1, 1, true],
];
}

public function testConstructionWithCustomAttributesMapInData()
{
$data = [
'custom_attributes' => [
'tax_class_id' => '3',
'category_ids' => '1,2'
],
];

/** @var Product $product */
$product = ObjectManager::getInstance()->create(Product::class, ['data' => $data]);
$this->assertSame($product->getCustomAttribute('tax_class_id')->getValue(), '3');
$this->assertSame($product->getCustomAttribute('category_ids')->getValue(), '1,2');
}

public function testConstructionWithCustomAttributesArrayInData()
{
$data = [
'custom_attributes' => [
[
'attribute_code' => 'tax_class_id',
'value' => '3'
],
[
'attribute_code' => 'category_ids',
'value' => '1,2'
]
],
];

/** @var Product $product */
$product = ObjectManager::getInstance()->create(Product::class, ['data' => $data]);
$this->assertSame($product->getCustomAttribute('tax_class_id')->getValue(), '3');
$this->assertSame($product->getCustomAttribute('category_ids')->getValue(), '1,2');
}
}
29 changes: 27 additions & 2 deletions lib/internal/Magento/Framework/Model/AbstractExtensibleModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ public function __construct(
}
}

/**
* Convert the custom attributes array format to map format
*
* The method \Magento\Framework\Reflection\DataObjectProcessor::buildOutputDataArray generates a custom_attributes
* array representation where each custom attribute is a sub-array with a `attribute_code and value key.
* This method maps such an array to the plain code => value map format exprected by filterCustomAttributes
*
* @param array[] $customAttributesData
* @return array
*/
private function flattenCustomAttributesArrayToMap(array $customAttributesData): array
{
return array_reduce(
$customAttributesData,
function (array $acc, array $customAttribute): array {
$acc[$customAttribute['attribute_code']] = $customAttribute['value'];
return $acc;
},
[]
);
}

/**
* Verify custom attributes set on $data and unset if not a valid custom attribute
*
Expand All @@ -85,9 +107,12 @@ protected function filterCustomAttributes($data)
if (empty($data[self::CUSTOM_ATTRIBUTES])) {
return $data;
}
$customAttributesCodes = $this->getCustomAttributesCodes();
if (isset($data[self::CUSTOM_ATTRIBUTES][0])) {
$data[self::CUSTOM_ATTRIBUTES] = $this->flattenCustomAttributesArrayToMap($data[self::CUSTOM_ATTRIBUTES]);
}
$customAttributesCodes = $this->getCustomAttributesCodes();
$data[self::CUSTOM_ATTRIBUTES] = array_intersect_key(
(array)$data[self::CUSTOM_ATTRIBUTES],
(array) $data[self::CUSTOM_ATTRIBUTES],
array_flip($customAttributesCodes)
);
foreach ($data[self::CUSTOM_ATTRIBUTES] as $code => $value) {
Expand Down