Skip to content

Commit eefe2ef

Browse files
committed
Merge remote-tracking branch 'magento-l3/ACP2E-1522' into FEB132023_PR_arul
2 parents c9d1397 + fa3675a commit eefe2ef

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

app/code/Magento/Catalog/Model/ResourceModel/AbstractResource.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
abstract class AbstractResource extends \Magento\Eav\Model\Entity\AbstractEntity
2525
{
2626
/**
27-
* Store manager
27+
* Store manager to get the store information
2828
*
2929
* @var \Magento\Store\Model\StoreManagerInterface
3030
*/
3131
protected $_storeManager;
3232

3333
/**
34-
* Model factory
34+
* Model factory to create a model object
3535
*
3636
* @var \Magento\Catalog\Model\Factory
3737
*/
@@ -325,7 +325,25 @@ protected function _insertAttribute($object, $attribute, $value)
325325
*/
326326
protected function _updateAttribute($object, $attribute, $valueId, $value)
327327
{
328-
return $this->_saveAttributeValue($object, $attribute, $value);
328+
$entity = $attribute->getEntity();
329+
$row = $this->getAttributeRow($entity, $object, $attribute);
330+
$hasSingleStore = $this->_storeManager->hasSingleStore();
331+
$storeId = $hasSingleStore
332+
? $this->getDefaultStoreId()
333+
: (int) $this->_storeManager->getStore($object->getStoreId())->getId();
334+
if ($valueId > 0 && array_key_exists('store_id', $row) && $storeId === $row['store_id']) {
335+
$table = $attribute->getBackend()->getTable();
336+
$connection = $this->getConnection();
337+
$connection->update(
338+
$table,
339+
['value' => $this->_prepareValueForSave($value, $attribute)],
340+
sprintf('%s=%d', $connection->quoteIdentifier('value_id'), $valueId)
341+
);
342+
343+
return $this;
344+
} else {
345+
return $this->_saveAttributeValue($object, $attribute, $value);
346+
}
329347
}
330348

331349
/**

dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/ProductTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Catalog\Test\Fixture\Attribute as AttributeFixture;
1010
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
1111
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Store\Model\StoreManagerInterface;
1213
use Magento\TestFramework\Eav\Model\GetAttributeSetByName;
1314
use Magento\TestFramework\Fixture\AppArea;
1415
use Magento\TestFramework\Fixture\AppIsolation;
@@ -44,6 +45,11 @@ class ProductTest extends TestCase
4445
*/
4546
private $objectManager;
4647

48+
/**
49+
* @var StoreManagerInterface
50+
*/
51+
private $storeManager;
52+
4753
/**
4854
* @inheritdoc
4955
*/
@@ -53,6 +59,8 @@ protected function setUp(): void
5359

5460
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
5561
$this->model = $this->objectManager->create(Product::class);
62+
63+
$this->storeManager = $this->objectManager->create(StoreManagerInterface::class);
5664
}
5765

5866
/**
@@ -213,4 +221,57 @@ public function testChangeAttributeSet()
213221
$attribute = $this->model->getAttributeRawValue($product->getId(), $attributeCode, 1);
214222
$this->assertEmpty($attribute);
215223
}
224+
225+
/**
226+
* Test update product custom attributes
227+
*
228+
* @return void
229+
*/
230+
#[
231+
DataFixture(AttributeFixture::class, ['attribute_code' => 'first_custom_attribute']),
232+
DataFixture(AttributeFixture::class, ['attribute_code' => 'second_custom_attribute']),
233+
DataFixture(AttributeFixture::class, ['attribute_code' => 'third_custom_attribute']),
234+
DataFixture(ProductFixture::class, ['sku' => 'simple','media_gallery_entries' => [[], []]], as: 'product')
235+
]
236+
237+
public function testUpdateCustomerAttributesAutoIncrement()
238+
{
239+
$resource = $this->objectManager->get(\Magento\Framework\App\ResourceConnection::class);
240+
$connection = $resource->getConnection();
241+
$currentTableStatus = $connection->showTableStatus('catalog_product_entity_varchar');
242+
$this->storeManager->setCurrentStore('admin');
243+
$product = $this->productRepository->get('simple');
244+
$product->setCustomAttribute(
245+
'first_custom_attribute',
246+
'first attribute'
247+
);
248+
$firstAttributeSavedProduct = $this->productRepository->save($product);
249+
$currentTableStatusAfterFirstAttrSave = $connection->showTableStatus('catalog_product_entity_varchar');
250+
$this->assertSame(
251+
((int) ($currentTableStatus['Auto_increment']) + 1),
252+
(int) $currentTableStatusAfterFirstAttrSave['Auto_increment']
253+
);
254+
255+
$firstAttributeSavedProduct->setCustomAttribute(
256+
'second_custom_attribute',
257+
'second attribute'
258+
);
259+
$secondAttributeSavedProduct = $this->productRepository->save($firstAttributeSavedProduct);
260+
$currentTableStatusAfterSecondAttrSave = $connection->showTableStatus('catalog_product_entity_varchar');
261+
$this->assertSame(
262+
(((int) $currentTableStatusAfterFirstAttrSave['Auto_increment']) + 1),
263+
(int) $currentTableStatusAfterSecondAttrSave['Auto_increment']
264+
);
265+
266+
$secondAttributeSavedProduct->setCustomAttribute(
267+
'third_custom_attribute',
268+
'third attribute'
269+
);
270+
$this->productRepository->save($secondAttributeSavedProduct);
271+
$currentTableStatusAfterThirdAttrSave = $connection->showTableStatus('catalog_product_entity_varchar');
272+
$this->assertSame(
273+
(((int)$currentTableStatusAfterSecondAttrSave['Auto_increment']) + 1),
274+
(int) $currentTableStatusAfterThirdAttrSave['Auto_increment']
275+
);
276+
}
216277
}

0 commit comments

Comments
 (0)