-
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 pull request #6171 from magento-tsg-csl3/2.4-develop-pr41
[TSG-CSL3] For 2.4 (pr41)
- Loading branch information
Showing
23 changed files
with
726 additions
and
146 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
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
63 changes: 63 additions & 0 deletions
63
app/code/Magento/Checkout/Test/Mftf/Test/CheckoutDifferentDefaultCountryPerStoreTest.xml
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,63 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
|
||
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd"> | ||
<test name="CheckoutDifferentDefaultCountryPerStoreTest"> | ||
<annotations> | ||
<features value="One Page Checkout"/> | ||
<stories value="Checkout via the Storefront"/> | ||
<title value="Checkout different default country per store"/> | ||
<description value="Checkout display default country per store view"/> | ||
<severity value="MAJOR"/> | ||
<testCaseId value="MC-37707"/> | ||
<useCaseId value="MC-36884"/> | ||
<group value="checkout"/> | ||
</annotations> | ||
<before> | ||
<!-- Create simple product --> | ||
<createData entity="SimpleProduct2" stepKey="createProduct"/> | ||
<!-- Create store view --> | ||
<actionGroup ref="AdminLoginActionGroup" stepKey="loginToAdminArea"/> | ||
<actionGroup ref="AdminCreateStoreViewActionGroup" stepKey="createStoreView"> | ||
<argument name="customStore" value="customStore"/> | ||
</actionGroup> | ||
<!-- Set Germany as default country for created store view --> | ||
<magentoCLI command="config:set --scope=stores --scope-code={{customStore.code}} general/country/default {{DE_Address_Berlin_Not_Default_Address.country_id}}" stepKey="changeDefaultCountry"/> | ||
</before> | ||
<after> | ||
<!--Delete product and store view--> | ||
<deleteData createDataKey="createProduct" stepKey="deleteProduct"/> | ||
<actionGroup ref="AdminDeleteStoreViewActionGroup" stepKey="deleteStoreView"> | ||
<argument name="customStore" value="customStore"/> | ||
</actionGroup> | ||
</after> | ||
<!-- Open product and add product to cart--> | ||
<actionGroup ref="OpenStoreFrontProductPageActionGroup" stepKey="openProductPage"> | ||
<argument name="productUrlKey" value="$$createProduct.custom_attributes[url_key]$$"/> | ||
</actionGroup> | ||
<actionGroup ref="StorefrontAddProductToCartActionGroup" stepKey="addProductToCart"> | ||
<argument name="product" value="$$createProduct$$"/> | ||
<argument name="productCount" value="1"/> | ||
</actionGroup> | ||
<!-- Go to cart --> | ||
<actionGroup ref="StorefrontOpenCartFromMinicartActionGroup" stepKey="openCart"/> | ||
<!-- Switch store view --> | ||
<actionGroup ref="StorefrontSwitchStoreViewActionGroup" stepKey="switchStoreViewActionGroup"> | ||
<argument name="storeView" value="customStore"/> | ||
</actionGroup> | ||
<!-- Go to checkout page --> | ||
<actionGroup ref="OpenStoreFrontCheckoutShippingPageActionGroup" stepKey="openCheckoutShippingPage"/> | ||
<!-- Grab country code from checkout page and assert value with default country for created store view --> | ||
<grabValueFrom selector="{{CheckoutShippingSection.country}}" stepKey="grabCountry"/> | ||
<assertEquals stepKey="assertCountryValue"> | ||
<actualResult type="const">$grabCountry</actualResult> | ||
<expectedResult type="string">{{DE_Address_Berlin_Not_Default_Address.country_id}}</expectedResult> | ||
</assertEquals> | ||
</test> | ||
</tests> |
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
173 changes: 173 additions & 0 deletions
173
app/code/Magento/GroupedProduct/Model/Inventory/ParentItemProcessor.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,173 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GroupedProduct\Model\Inventory; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Framework\EntityManager\MetadataPool; | ||
use Magento\GroupedProduct\Model\Product\Type\Grouped; | ||
use Magento\Catalog\Api\Data\ProductInterface as Product; | ||
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; | ||
use Magento\CatalogInventory\Api\StockItemRepositoryInterface; | ||
use Magento\CatalogInventory\Api\StockConfigurationInterface; | ||
use Magento\CatalogInventory\Observer\ParentItemProcessorInterface; | ||
use Magento\CatalogInventory\Api\Data\StockItemInterface; | ||
use Magento\GroupedProduct\Model\ResourceModel\Product\Link; | ||
use Magento\Framework\App\ResourceConnection; | ||
|
||
/** | ||
* Process parent stock item for grouped product | ||
*/ | ||
class ParentItemProcessor implements ParentItemProcessorInterface | ||
{ | ||
/** | ||
* @var Grouped | ||
*/ | ||
private $groupedType; | ||
|
||
/** | ||
* @var StockItemRepositoryInterface | ||
*/ | ||
private $stockItemRepository; | ||
|
||
/** | ||
* @var StockConfigurationInterface | ||
*/ | ||
private $stockConfiguration; | ||
|
||
/** | ||
* @var StockItemCriteriaInterfaceFactory | ||
*/ | ||
private $criteriaInterfaceFactory; | ||
|
||
/** | ||
* Product metadata pool | ||
* | ||
* @var MetadataPool | ||
*/ | ||
private $metadataPool; | ||
|
||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resource; | ||
|
||
/** | ||
* @param Grouped $groupedType | ||
* @param StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory | ||
* @param StockItemRepositoryInterface $stockItemRepository | ||
* @param StockConfigurationInterface $stockConfiguration | ||
* @param ResourceConnection $resource | ||
* @param MetadataPool $metadataPool | ||
*/ | ||
public function __construct( | ||
Grouped $groupedType, | ||
StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory, | ||
StockItemRepositoryInterface $stockItemRepository, | ||
StockConfigurationInterface $stockConfiguration, | ||
ResourceConnection $resource, | ||
MetadataPool $metadataPool | ||
) { | ||
$this->groupedType = $groupedType; | ||
$this->criteriaInterfaceFactory = $criteriaInterfaceFactory; | ||
$this->stockConfiguration = $stockConfiguration; | ||
$this->stockItemRepository = $stockItemRepository; | ||
$this->resource = $resource; | ||
$this->metadataPool = $metadataPool; | ||
} | ||
|
||
/** | ||
* Process parent products | ||
* | ||
* @param Product $product | ||
* @return void | ||
*/ | ||
public function process(Product $product) | ||
{ | ||
$parentIds = $this->getParentEntityIdsByChild($product->getId()); | ||
foreach ($parentIds as $productId) { | ||
$this->processStockForParent((int)$productId); | ||
} | ||
} | ||
|
||
/** | ||
* Change stock item for parent product depending on children stock items | ||
* | ||
* @param int $productId | ||
* @return void | ||
*/ | ||
private function processStockForParent(int $productId) | ||
{ | ||
$criteria = $this->criteriaInterfaceFactory->create(); | ||
$criteria->setScopeFilter($this->stockConfiguration->getDefaultScopeId()); | ||
$criteria->setProductsFilter($productId); | ||
$stockItemCollection = $this->stockItemRepository->getList($criteria); | ||
$allItems = $stockItemCollection->getItems(); | ||
if (empty($allItems)) { | ||
return; | ||
} | ||
$parentStockItem = array_shift($allItems); | ||
$groupedChildrenIds = $this->groupedType->getChildrenIds($productId); | ||
$criteria->setProductsFilter($groupedChildrenIds); | ||
$stockItemCollection = $this->stockItemRepository->getList($criteria); | ||
$allItems = $stockItemCollection->getItems(); | ||
|
||
$groupedChildrenIsInStock = false; | ||
|
||
foreach ($allItems as $childItem) { | ||
if ($childItem->getIsInStock() === true) { | ||
$groupedChildrenIsInStock = true; | ||
break; | ||
} | ||
} | ||
|
||
if ($this->isNeedToUpdateParent($parentStockItem, $groupedChildrenIsInStock)) { | ||
$parentStockItem->setIsInStock($groupedChildrenIsInStock); | ||
$parentStockItem->setStockStatusChangedAuto(1); | ||
$this->stockItemRepository->save($parentStockItem); | ||
} | ||
} | ||
|
||
/** | ||
* Check is parent item should be updated | ||
* | ||
* @param StockItemInterface $parentStockItem | ||
* @param bool $childrenIsInStock | ||
* @return bool | ||
*/ | ||
private function isNeedToUpdateParent(StockItemInterface $parentStockItem, bool $childrenIsInStock): bool | ||
{ | ||
return $parentStockItem->getIsInStock() !== $childrenIsInStock && | ||
($childrenIsInStock === false || $parentStockItem->getStockStatusChangedAuto()); | ||
} | ||
|
||
/** | ||
* Retrieve parent ids array by child id | ||
* | ||
* @param int $childId | ||
* @return string[] | ||
*/ | ||
private function getParentEntityIdsByChild($childId) | ||
{ | ||
$select = $this->resource->getConnection() | ||
->select() | ||
->from(['l' => $this->resource->getTableName('catalog_product_link')], []) | ||
->join( | ||
['e' => $this->resource->getTableName('catalog_product_entity')], | ||
'e.' . | ||
$this->metadataPool->getMetadata(ProductInterface::class)->getLinkField() . ' = l.product_id', | ||
['e.entity_id'] | ||
) | ||
->where('l.linked_product_id = ?', $childId) | ||
->where( | ||
'link_type_id = ?', | ||
Link::LINK_TYPE_GROUPED | ||
); | ||
|
||
return $this->resource->getConnection()->fetchCol($select); | ||
} | ||
} |
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
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.