Skip to content

Commit b7dfc68

Browse files
committed
Merge branch 'ACP2E-2053' of https://github.com/magento-l3/magento2ce into PR-L3-2023-07-14
2 parents eb34e34 + 10adf8c commit b7dfc68

File tree

5 files changed

+214
-2
lines changed

5 files changed

+214
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected function _isApplicableAttribute($object, $attribute)
8787
{
8888
$applyTo = $attribute->getApplyTo() ?: [];
8989
return (count($applyTo) == 0 || in_array($object->getTypeId(), $applyTo))
90-
&& $attribute->isInSet($object->getAttributeSetId());
90+
&& $attribute->isInSet($object->getAttributeSetId() ?? $this->getEntityType()->getDefaultAttributeSetId());
9191
}
9292

9393
/**
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Test\Fixture;
9+
10+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
11+
use Magento\Eav\Model\Config;
12+
use Magento\Framework\DataObject;
13+
use Magento\TestFramework\Fixture\Api\ServiceFactory;
14+
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
15+
16+
class AttributeSet extends \Magento\Eav\Test\Fixture\AttributeSet
17+
{
18+
private const ENTITY_TYPE = ProductAttributeInterface::ENTITY_TYPE_CODE;
19+
20+
public function __construct(
21+
ServiceFactory $serviceFactory,
22+
ProcessorInterface $dataProcessor,
23+
private readonly Config $eavConfig
24+
) {
25+
parent::__construct($serviceFactory, $dataProcessor);
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function apply(array $data = []): ?DataObject
32+
{
33+
return parent::apply(
34+
array_merge(
35+
[
36+
'entity_type_code' => self::ENTITY_TYPE,
37+
'skeleton_id' => $this->eavConfig->getEntityType(self::ENTITY_TYPE)->getDefaultAttributeSetId(),
38+
],
39+
$data
40+
)
41+
);
42+
}
43+
}

app/code/Magento/Eav/Model/Entity/AbstractEntity.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,16 @@ public function isPartialSave($flag = null)
553553
*/
554554
public function loadAllAttributes($object = null)
555555
{
556-
return $this->attributeLoader->loadAllAttributes($this, $object);
556+
$result = $this->attributeLoader->loadAllAttributes($this, $object);
557+
if ($object instanceof DataObject && $object->getAttributeSetId()) {
558+
$suffix = $this->getAttributesCacheSuffix($object);
559+
$this->_attrSetEntity->addSetInfo(
560+
$this->getEntityType(),
561+
$this->getAttributesByScope($suffix),
562+
$object->getAttributeSetId()
563+
);
564+
}
565+
return $result;
557566
}
558567

559568
/**
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Eav\Test\Fixture;
9+
10+
use Magento\Eav\Api\AttributeSetManagementInterface;
11+
use Magento\Eav\Api\AttributeSetRepositoryInterface;
12+
use Magento\Framework\DataObject;
13+
use Magento\TestFramework\Fixture\Api\ServiceFactory;
14+
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
15+
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
16+
17+
class AttributeSet implements RevertibleDataFixtureInterface
18+
{
19+
private const DEFAULT_DATA = [
20+
'attribute_set_id' => null,
21+
'attribute_set_name' => 'attribute_set%uniqid%',
22+
'sort_order' => 0,
23+
'entity_type_code' => null,
24+
'skeleton_id' => null,
25+
];
26+
27+
/**
28+
* @param ServiceFactory $serviceFactory
29+
* @param ProcessorInterface $dataProcessor
30+
*/
31+
public function __construct(
32+
private readonly ServiceFactory $serviceFactory,
33+
private readonly ProcessorInterface $dataProcessor
34+
) {
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
* @param array $data Parameters. Same format as AttributeSet::DEFAULT_DATA.
40+
*/
41+
public function apply(array $data = []): ?DataObject
42+
{
43+
$data = array_merge(self::DEFAULT_DATA, $data);
44+
$skeletonId = $data['skeleton_id'];
45+
$entityTypeCode = $data['entity_type_code'];
46+
unset($data['skeleton_id'], $data['entity_type_code']);
47+
$service = $this->serviceFactory->create(AttributeSetManagementInterface::class, 'create');
48+
49+
return $service->execute(
50+
[
51+
'attributeSet' => $this->dataProcessor->process($this, $data),
52+
'entityTypeCode' => $entityTypeCode,
53+
'skeletonId' => $skeletonId,
54+
]
55+
);
56+
}
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
public function revert(DataObject $data): void
62+
{
63+
$service = $this->serviceFactory->create(AttributeSetRepositoryInterface::class, 'deleteById');
64+
$service->execute(
65+
[
66+
'attributeSetId' => $data->getAttributeSetId()
67+
]
68+
);
69+
}
70+
}

dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
1212
use Magento\Catalog\Model\Product\Media\ConfigInterface;
1313
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
14+
use Magento\Catalog\Test\Fixture\AttributeSet as AttributeSetFixture;
1415
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
16+
use Magento\Customer\Model\Group;
1517
use Magento\Framework\Api\SearchCriteriaBuilder;
1618
use Magento\Framework\App\Filesystem\DirectoryList;
1719
use Magento\Framework\Exception\CouldNotSaveException;
@@ -427,6 +429,94 @@ public function testConsecutivePartialProductsUpdateInStoreView(): void
427429
$this->assertEquals($product2Store1Price, $product2->getPrice());
428430
}
429431

432+
#[
433+
AppArea('adminhtml'),
434+
DataFixture(AttributeSetFixture::class, as: 'attribute_set2'),
435+
DataFixture(
436+
ProductFixture::class,
437+
[
438+
'tier_prices' => [
439+
[
440+
'customer_group_id' => Group::NOT_LOGGED_IN_ID,
441+
'qty' => 2,
442+
'value' => 7.5
443+
]
444+
]
445+
],
446+
'product1'
447+
),
448+
DataFixture(
449+
ProductFixture::class,
450+
[
451+
'attribute_set_id' => '$attribute_set2.attribute_set_id$',
452+
'tier_prices' => [
453+
[
454+
'customer_group_id' => Group::NOT_LOGGED_IN_ID,
455+
'qty' => 4,
456+
'value' => 8
457+
]
458+
]
459+
],
460+
'product2'
461+
),
462+
]
463+
public function testConsecutiveProductsUpdateWithDifferentAttributeSets(): void
464+
{
465+
$product1 = $this->fixtures->get('product1');
466+
$product2 = $this->fixtures->get('product2');
467+
$store1 = $this->storeManager->getStore('default')->getId();
468+
$this->storeManager->setCurrentStore($store1);
469+
$product1UpdatedName = $product1->getName() . ' for default store view';
470+
$product2UpdatedName = $product2->getName() . ' for default store view';
471+
$this->productRepository->save(
472+
$this->getProductInstance(
473+
[
474+
'sku' => $product1->getSku(),
475+
'name' => $product1UpdatedName,
476+
]
477+
)
478+
);
479+
$this->productRepository->save(
480+
$this->getProductInstance(
481+
[
482+
'sku' => $product2->getSku(),
483+
'name' => $product2UpdatedName,
484+
]
485+
)
486+
);
487+
$product1 = $this->productRepository->get($product1->getSku(), true, $store1, true);
488+
$this->assertEquals($product1UpdatedName, $product1->getName());
489+
$this->assertCount(1, $product1->getTierPrices());
490+
$this->assertEquals(
491+
[
492+
'customer_group_id' => Group::NOT_LOGGED_IN_ID,
493+
'qty' => 2,
494+
'value' => 7.5
495+
],
496+
[
497+
'customer_group_id' => $product1->getTierPrices()[0]->getCustomerGroupId(),
498+
'qty' => $product1->getTierPrices()[0]->getQty(),
499+
'value' => $product1->getTierPrices()[0]->getValue()
500+
]
501+
);
502+
503+
$product2 = $this->productRepository->get($product2->getSku(), true, $store1, true);
504+
$this->assertEquals($product2UpdatedName, $product2->getName());
505+
$this->assertCount(1, $product2->getTierPrices());
506+
$this->assertEquals(
507+
[
508+
'customer_group_id' => Group::NOT_LOGGED_IN_ID,
509+
'qty' => 4,
510+
'value' => 8
511+
],
512+
[
513+
'customer_group_id' => $product2->getTierPrices()[0]->getCustomerGroupId(),
514+
'qty' => $product2->getTierPrices()[0]->getQty(),
515+
'value' => $product2->getTierPrices()[0]->getValue()
516+
]
517+
);
518+
}
519+
430520
/**
431521
* Get Simple Product Data
432522
*

0 commit comments

Comments
 (0)