Skip to content

Commit

Permalink
Merge branch '2.4-develop' into Refactoring-AdminDeleteSimpleProductTest
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/code/Magento/CatalogSearch/Test/Mftf/ActionGroup/AssertStorefrontNoResultsMessageOnSearchPageActionGroup.xml
  • Loading branch information
kate-kyzyma committed Feb 23, 2021
2 parents c5d85bf + 2e2af0a commit fb70cfb
Show file tree
Hide file tree
Showing 234 changed files with 4,901 additions and 1,198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@
</annotations>
<before>
<magentoCLI command="config:set {{ChangedCookieDomainForMainWebsiteConfigData.path}} --scope={{ChangedCookieDomainForMainWebsiteConfigData.scope}} --scope-code={{ChangedCookieDomainForMainWebsiteConfigData.scope_code}} {{ChangedCookieDomainForMainWebsiteConfigData.value}}" stepKey="changeDomainForMainWebsiteBeforeTestRun"/>
<actionGroup ref="CliCacheFlushActionGroup" stepKey="flushCacheBeforeTestRun">
<argument name="tags" value="config"/>
</actionGroup>
<comment userInput="Adding the comment to replace CliCacheFlushActionGroup action group ('cache:flush' command) for preserving Backward Compatibility" stepKey="flushCacheBeforeTestRun"/>
</before>
<after>
<magentoCLI command="config:set {{EmptyCookieDomainForMainWebsiteConfigData.path}} --scope={{EmptyCookieDomainForMainWebsiteConfigData.scope}} --scope-code={{EmptyCookieDomainForMainWebsiteConfigData.scope_code}} {{EmptyCookieDomainForMainWebsiteConfigData.value}}" stepKey="changeDomainForMainWebsiteAfterTestComplete"/>
<actionGroup ref="CliCacheFlushActionGroup" stepKey="flushCacheAfterTestComplete">
<argument name="tags" value="config"/>
</actionGroup>
<comment userInput="Adding the comment to replace CliCacheFlushActionGroup action group ('cache:flush' command) for preserving Backward Compatibility" stepKey="flushCacheAfterTestComplete"/>
</after>
<actionGroup ref="AdminLoginActionGroup" stepKey="loginAsAdmin"/>
<actionGroup ref="AssertAdminDashboardPageIsVisibleActionGroup" stepKey="seeDashboardPage"/>
Expand Down
66 changes: 58 additions & 8 deletions app/code/Magento/Bundle/Model/LinkManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;

Expand Down Expand Up @@ -173,12 +173,11 @@ public function saveChild(
)
);
}
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$selectionModel = $this->mapProductLinkToSelectionModel(
$selectionModel = $this->mapProductLinkToBundleSelectionModel(
$selectionModel,
$linkedProduct,
$linkProductModel->getId(),
$product->getData($linkField)
$product,
(int)$linkProductModel->getId()
);

try {
Expand All @@ -202,6 +201,7 @@ public function saveChild(
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @deprecated use mapProductLinkToBundleSelectionModel
*/
protected function mapProductLinkToSelectionModel(
Selection $selectionModel,
Expand Down Expand Up @@ -239,6 +239,55 @@ protected function mapProductLinkToSelectionModel(
return $selectionModel;
}

/**
* Fill selection model with product link data.
*
* @param Selection $selectionModel
* @param LinkInterface $productLink
* @param ProductInterface $parentProduct
* @param int $linkedProductId
* @param string $linkField
* @return Selection
* @throws NoSuchEntityException
*/
private function mapProductLinkToBundleSelectionModel(
Selection $selectionModel,
LinkInterface $productLink,
ProductInterface $parentProduct,
int $linkedProductId
): Selection {
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$selectionModel->setProductId($linkedProductId);
$selectionModel->setParentProductId($parentProduct->getData($linkField));
if ($productLink->getSelectionId() !== null) {
$selectionModel->setSelectionId($productLink->getSelectionId());
}
if ($productLink->getOptionId() !== null) {
$selectionModel->setOptionId($productLink->getOptionId());
}
if ($productLink->getPosition() !== null) {
$selectionModel->setPosition($productLink->getPosition());
}
if ($productLink->getQty() !== null) {
$selectionModel->setSelectionQty($productLink->getQty());
}
if ($productLink->getPriceType() !== null) {
$selectionModel->setSelectionPriceType($productLink->getPriceType());
}
if ($productLink->getPrice() !== null) {
$selectionModel->setSelectionPriceValue($productLink->getPrice());
}
if ($productLink->getCanChangeQuantity() !== null) {
$selectionModel->setSelectionCanChangeQty($productLink->getCanChangeQuantity());
}
if ($productLink->getIsDefault() !== null) {
$selectionModel->setIsDefault($productLink->getIsDefault());
}
$selectionModel->setWebsiteId((int)$this->storeManager->getStore($parentProduct->getStoreId())->getWebsiteId());

return $selectionModel;
}

/**
* @inheritDoc
*
Expand Down Expand Up @@ -302,12 +351,13 @@ public function addChild(
}

$selectionModel = $this->bundleSelection->create();
$selectionModel = $this->mapProductLinkToSelectionModel(
$selectionModel = $this->mapProductLinkToBundleSelectionModel(
$selectionModel,
$linkedProduct,
$linkProductModel->getEntityId(),
$product->getData($linkField)
$product,
(int)$linkProductModel->getEntityId()
);

$selectionModel->setOptionId($optionId);

try {
Expand Down
22 changes: 14 additions & 8 deletions app/code/Magento/Bundle/Model/Option/SaveAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

namespace Magento\Bundle\Model\Option;

use Magento\Bundle\Api\Data\LinkInterface;
use Magento\Bundle\Api\Data\OptionInterface;
use Magento\Bundle\Model\ResourceModel\Option;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Bundle\Model\Product\Type;
use Magento\Bundle\Api\ProductLinkManagementInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;

/**
* Encapsulates logic for saving a bundle option, including coalescing the parent product's data.
Expand Down Expand Up @@ -45,12 +49,14 @@ class SaveAction
* @param MetadataPool $metadataPool
* @param Type $type
* @param ProductLinkManagementInterface $linkManagement
* @param StoreManagerInterface|null $storeManager
*/
public function __construct(
Option $optionResource,
MetadataPool $metadataPool,
Type $type,
ProductLinkManagementInterface $linkManagement
ProductLinkManagementInterface $linkManagement,
?StoreManagerInterface $storeManager = null
) {
$this->optionResource = $optionResource;
$this->metadataPool = $metadataPool;
Expand All @@ -69,7 +75,7 @@ public function __construct(
*/
public function save(ProductInterface $bundleProduct, OptionInterface $option)
{
$metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);

$option->setStoreId($bundleProduct->getStoreId());
$parentId = $bundleProduct->getData($metadata->getLinkField());
Expand Down Expand Up @@ -108,7 +114,7 @@ public function save(ProductInterface $bundleProduct, OptionInterface $option)
throw new CouldNotSaveException(__("The option couldn't be saved."), $e);
}

/** @var \Magento\Bundle\Api\Data\LinkInterface $linkedProduct */
/** @var LinkInterface $linkedProduct */
foreach ($linksToAdd as $linkedProduct) {
$this->linkManagement->addChild($bundleProduct, $option->getOptionId(), $linkedProduct);
}
Expand All @@ -121,8 +127,8 @@ public function save(ProductInterface $bundleProduct, OptionInterface $option)
/**
* Update option selections
*
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @param \Magento\Bundle\Api\Data\OptionInterface $option
* @param ProductInterface $product
* @param OptionInterface $option
* @return void
*/
private function updateOptionSelection(ProductInterface $product, OptionInterface $option)
Expand All @@ -141,7 +147,7 @@ private function updateOptionSelection(ProductInterface $product, OptionInterfac
$linksToUpdate[] = $productLink;
}
}
/** @var \Magento\Bundle\Api\Data\LinkInterface[] $linksToDelete */
/** @var LinkInterface[] $linksToDelete */
$linksToDelete = $this->compareLinks($existingLinks, $linksToUpdate);
}
foreach ($linksToUpdate as $linkedProduct) {
Expand All @@ -162,8 +168,8 @@ private function updateOptionSelection(ProductInterface $product, OptionInterfac
/**
* Compute the difference between given arrays.
*
* @param \Magento\Bundle\Api\Data\LinkInterface[] $firstArray
* @param \Magento\Bundle\Api\Data\LinkInterface[] $secondArray
* @param LinkInterface[] $firstArray
* @param LinkInterface[] $secondArray
*
* @return array
*/
Expand Down
109 changes: 109 additions & 0 deletions app/code/Magento/Bundle/Model/Quote/Item/Option.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Bundle\Model\Quote\Item;

use Magento\Bundle\Model\Product\Price;
use Magento\Bundle\Model\Product\Type;
use Magento\Catalog\Model\Product;
use Magento\Framework\Serialize\Serializer\Json;

/**
* Bundle product options model
*/
class Option
{
/**
* @var Json
*/
private $serializer;

/**
* @param Json $serializer
*/
public function __construct(
Json $serializer
) {
$this->serializer = $serializer;
}

/**
* Get selection options for provided bundle product
*
* @param Product $product
* @return array
*/
public function getSelectionOptions(Product $product): array
{
$options = [];
$bundleOptionIds = $this->getOptionValueAsArray($product, 'bundle_option_ids');
if ($bundleOptionIds) {
/** @var Type $typeInstance */
$typeInstance = $product->getTypeInstance();
$optionsCollection = $typeInstance->getOptionsByIds($bundleOptionIds, $product);
$selectionIds = $this->getOptionValueAsArray($product, 'bundle_selection_ids');

if ($selectionIds) {
$selectionsCollection = $typeInstance->getSelectionsByIds($selectionIds, $product);
$optionsCollection->appendSelections($selectionsCollection, true);

foreach ($selectionsCollection as $selection) {
$selectionId = $selection->getSelectionId();
$options[$selectionId][] = $this->getBundleSelectionAttributes($product, $selection);
}
}
}

return $options;
}

/**
* Get selection attributes for provided selection
*
* @param Product $product
* @param Product $selection
* @return array
*/
private function getBundleSelectionAttributes(Product $product, Product $selection): array
{
$selectionId = $selection->getSelectionId();
/** @var \Magento\Bundle\Model\Option $bundleOption */
$bundleOption = $selection->getOption();
/** @var Price $priceModel */
$priceModel = $product->getPriceModel();
$price = $priceModel->getSelectionFinalTotalPrice($product, $selection, 0, 1);
$customOption = $product->getCustomOption('selection_qty_' . $selectionId);
$qty = (float)($customOption ? $customOption->getValue() : 0);

return [
'code' => 'bundle_selection_attributes',
'value'=> $this->serializer->serialize(
[
'price' => $price,
'qty' => $qty,
'option_label' => $bundleOption->getTitle(),
'option_id' => $bundleOption->getId(),
]
)
];
}

/**
* Get unserialized value of custom option
*
* @param Product $product
* @param string $code
* @return array
*/
private function getOptionValueAsArray(Product $product, string $code): array
{
$option = $product->getCustomOption($code);
return $option && $option->getValue()
? $this->serializer->unserialize($option->getValue())
: [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Bundle\Model\Quote\Item\Option;

use Magento\Framework\DataObject;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Quote\Model\Quote\Item\Option\ComparatorInterface;

/**
* Bundle quote item option comparator
*/
class BundleSelectionAttributesComparator implements ComparatorInterface
{
/**
* @var Json
*/
private $serializer;

/**
* @param Json $serializer
*/
public function __construct(
Json $serializer
) {
$this->serializer = $serializer;
}

/**
* @inheritdoc
*/
public function compare(DataObject $option1, DataObject $option2): bool
{
$value1 = $option1->getValue() ? $this->serializer->unserialize($option1->getValue()) : [];
$value2 = $option2->getValue() ? $this->serializer->unserialize($option2->getValue()) : [];
$option1Id = isset($value1['option_id']) ? (int) $value1['option_id'] : null;
$option2Id = isset($value2['option_id']) ? (int) $value2['option_id'] : null;

return $option1Id === $option2Id;
}
}
Loading

0 comments on commit fb70cfb

Please sign in to comment.