-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2.4-develop' into Refactoring-AdminCreateSimpleProductZ…
…eroPriceTest
- Loading branch information
Showing
30 changed files
with
1,157 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
app/code/Magento/Bundle/Model/ProductRelationsProcessorComposite.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Bundle\Model; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
|
||
/** | ||
* Composite processor to handle bundle product relations. | ||
*/ | ||
class ProductRelationsProcessorComposite implements ProductRelationsProcessorInterface | ||
{ | ||
/** | ||
* @var ProductRelationsProcessorInterface[] | ||
*/ | ||
private $processors; | ||
|
||
/** | ||
* @param ProductRelationsProcessorInterface[] $processors | ||
*/ | ||
public function __construct(array $processors = []) | ||
{ | ||
foreach ($processors as $processor) { | ||
if (!$processor instanceof ProductRelationsProcessorInterface) { | ||
throw new \InvalidArgumentException( | ||
__('Product relations processor must implement %1.', ProductRelationsProcessorInterface::class) | ||
); | ||
} | ||
} | ||
|
||
$this->processors = $processors; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function process( | ||
ProductInterface $product, | ||
array $existingProductOptions, | ||
array $expectedProductOptions | ||
): void { | ||
foreach ($this->processors as $processor) { | ||
$processor->process($product, $existingProductOptions, $expectedProductOptions); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
app/code/Magento/Bundle/Model/ProductRelationsProcessorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Bundle\Model; | ||
|
||
/** | ||
* Processor to handle bundle product relations. | ||
*/ | ||
interface ProductRelationsProcessorInterface | ||
{ | ||
/** | ||
* Process bundle product relations. | ||
* | ||
* @param \Magento\Catalog\Api\Data\ProductInterface $product | ||
* @param array $existingProductOptions | ||
* @param array $expectedProductOptions | ||
* @return void | ||
*/ | ||
public function process( | ||
\Magento\Catalog\Api\Data\ProductInterface $product, | ||
array $existingProductOptions, | ||
array $expectedProductOptions | ||
): void; | ||
} |
149 changes: 149 additions & 0 deletions
149
app/code/Magento/Bundle/Model/QuoteRecollectProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Bundle\Model; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Framework\Reflection\TypeCaster; | ||
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; | ||
|
||
/** | ||
* Recollect quota after handle product relations. | ||
*/ | ||
class QuoteRecollectProcessor implements ProductRelationsProcessorInterface | ||
{ | ||
/** | ||
* @var TypeCaster | ||
*/ | ||
private $typeCaster; | ||
|
||
/** | ||
* @var QuoteResource | ||
*/ | ||
private $quoteResource; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $comparisonFieldsTypeMapper; | ||
|
||
/** | ||
* @param TypeCaster $typeCaster | ||
* @param QuoteResource $quoteResource | ||
* @param array $comparisonFieldsTypeMapper | ||
*/ | ||
public function __construct( | ||
TypeCaster $typeCaster, | ||
QuoteResource $quoteResource, | ||
array $comparisonFieldsTypeMapper = [] | ||
) { | ||
$this->typeCaster = $typeCaster; | ||
$this->quoteResource = $quoteResource; | ||
$this->comparisonFieldsTypeMapper = $comparisonFieldsTypeMapper; | ||
} | ||
|
||
/** | ||
* Mark quotes to recollect if product options or links are changed. | ||
* | ||
* @param ProductInterface $product | ||
* @param array $existingProductOptions | ||
* @param array $expectedProductOptions | ||
* @return void | ||
*/ | ||
public function process( | ||
ProductInterface $product, | ||
array $existingProductOptions, | ||
array $expectedProductOptions | ||
): void { | ||
if (empty($existingProductOptions)) { | ||
return; | ||
} | ||
|
||
if ($this->isProductOptionsChanged($existingProductOptions, $expectedProductOptions) | ||
|| $this->isProductLinksChanged($existingProductOptions, $expectedProductOptions) | ||
) { | ||
$this->quoteResource->markQuotesRecollect($product->getId()); | ||
} | ||
} | ||
|
||
/** | ||
* Check product options change. | ||
* | ||
* @param array $existingProductOptions | ||
* @param array $expectedProductOptions | ||
* @return bool | ||
*/ | ||
private function isProductOptionsChanged( | ||
array $existingProductOptions, | ||
array $expectedProductOptions | ||
): bool { | ||
if (count($existingProductOptions) !== count($expectedProductOptions)) { | ||
return true; | ||
} | ||
|
||
$productOptionsDiff = array_udiff( | ||
$expectedProductOptions, | ||
$existingProductOptions, | ||
function ($expectedProductOption, $existingProductOption) { | ||
if ($expectedProductOption->getOptionId() === $existingProductOption->getOptionId()) { | ||
return $expectedProductOption->getRequired() - $existingProductOption->getRequired(); | ||
} | ||
|
||
return $expectedProductOption->getOptionId() - $existingProductOption->getOptionId(); | ||
} | ||
); | ||
|
||
return (bool)count($productOptionsDiff); | ||
} | ||
|
||
/** | ||
* Check product links change. | ||
* | ||
* @param array $existingProductOptions | ||
* @param array $expectedProductOptions | ||
* @return bool | ||
*/ | ||
private function isProductLinksChanged( | ||
array $existingProductOptions, | ||
array $expectedProductOptions | ||
): bool { | ||
$existingProductLinks = $this->flattenProductLinksData($existingProductOptions); | ||
$expectedProductLinks = $this->flattenProductLinksData($expectedProductOptions); | ||
|
||
return $existingProductLinks != $expectedProductLinks; | ||
} | ||
|
||
/** | ||
* Simplify product links data. | ||
* | ||
* @param array $productOptions | ||
* @return array | ||
*/ | ||
private function flattenProductLinksData(array $productOptions): array | ||
{ | ||
return array_reduce($productOptions, function ($result, $productOption) { | ||
$optionId = $productOption->getOptionId(); | ||
$productLinks = []; | ||
foreach ($productOption->getProductLinks() as $productLink) { | ||
$productLinkData = $productLink->getData(); | ||
$productLinkFilteredData = []; | ||
foreach ($this->comparisonFieldsTypeMapper as $fieldName => $fieldType) { | ||
if (isset($productLinkData[$fieldName])) { | ||
$productLinkFilteredData[$fieldName] = $this->typeCaster->castValueToType( | ||
$productLinkData[$fieldName], | ||
$fieldType | ||
); | ||
} | ||
} | ||
$productLinks[$productLink->getId()] = $productLinkFilteredData; | ||
} | ||
$result[$optionId] = $productLinks; | ||
|
||
return $result; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.