Skip to content

Commit

Permalink
Add Magento Inventory support to Advanced Checkout and Requisition List
Browse files Browse the repository at this point in the history
  • Loading branch information
ishakhsuvarov committed Aug 2, 2019
1 parent d74a9ad commit 873ac41
Show file tree
Hide file tree
Showing 19 changed files with 1,084 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryAdvancedCheckout\Plugin\Model\IsProductInStock;

use Magento\AdvancedCheckout\Model\IsProductInStockInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\IsProductSalableInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Store\Api\WebsiteRepositoryInterface;

/**
* Provides multi-sourcing capabilities for Advanced Checkout Order By SKU feature.
*/
class ProductInStockPlugin
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var IsProductSalableInterface
*/
private $isProductSalable;

/**
* @var StockResolverInterface
*/
private $stockResolver;

/**
* @var WebsiteRepositoryInterface
*/
private $websiteRepository;

/**
* @var IsSourceItemManagementAllowedForProductTypeInterface
*/
private $isSourceItemManagementAllowedForProductType;

/**
* @param ProductRepositoryInterface $productRepository
* @param IsProductSalableInterface $isProductSalable
* @param StockResolverInterface $stockResolver
* @param WebsiteRepositoryInterface $websiteRepository
* @param IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
*/
public function __construct(
ProductRepositoryInterface $productRepository,
IsProductSalableInterface $isProductSalable,
StockResolverInterface $stockResolver,
WebsiteRepositoryInterface $websiteRepository,
IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
) {
$this->productRepository = $productRepository;
$this->isProductSalable = $isProductSalable;
$this->stockResolver = $stockResolver;
$this->websiteRepository = $websiteRepository;
$this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
}

/**
* Get is product out of stock for given Product id in a given Website id in MSI context.
*
* @param IsProductInStockInterface $subject
* @param callable $proceed
* @param int $productId
* @param int $websiteId
* @return bool
* @throws NoSuchEntityException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundExecute(
IsProductInStockInterface $subject,
callable $proceed,
int $productId,
int $websiteId
): bool {
$product = $this->productRepository->getById($productId);
if (!$this->isSourceItemManagementAllowedForProductType->execute($product->getTypeId())) {
return $proceed($productId, $websiteId);
}
$website = $this->websiteRepository->getById($websiteId);
$stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $website->getCode());

return $this->isProductSalable->execute($product->getSku(), $stock->getStockId());
}
}
5 changes: 5 additions & 0 deletions InventoryAdvancedCheckout/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Magento_InventoryAdvancedCheckout

## Overview

The Magento_InventoryAdvancedCheckout module adds multi-sourcing capabilities to the AdvancedCheckout module allowing Order By SKU feature to work correctly with multiple inventory sources enabled.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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="AddBundleDynamicFromCustomStockBySkuFromCustomerAccountTest" extends="AddBundleDynamicProductBySkuFromCustomerAccountTest">
<annotations>
<description value="Add bundle dynamic product by sku from customer account on Additional Stock."/>
<skip>
<issueId value="https://github.com/magento/inventory/issues/1487"/>
</skip>
<group value="msi"/>
<group value="multi_mode"/>
</annotations>
<before>
<!--Create additional stock ans source.-->
<createData entity="_minimalSource" stepKey="additionalSource" before="additionalStock"/>
<createData entity="BasicMsiStockWithMainWebsite1" stepKey="additionalStock" before="stockSourceLink"/>
<!--Link additional source with stock.-->
<createData entity="SourceStockLinked1" stepKey="stockSourceLink" before="createSimpleProduct">
<requiredEntity createDataKey="additionalStock"/>
<requiredEntity createDataKey="additionalSource"/>
</createData>
<!--Assign create product to additional stock and set qty.-->
<actionGroup ref="LoginAsAdmin" stepKey="loginToAdminPanel" after="createSimpleProduct"/>
<amOnPage url="{{AdminProductEditPage.url($$createSimpleProduct.id$$)}}" stepKey="openProductEditPageToSetQty" after="loginToAdminPanel"/>
<actionGroup ref="AssignSourceToProductActionGroup" stepKey="assignSourceToCreatedProduct" after="openProductEditPageToSetQty">
<argument name="sourceCode" value="$$additionalSource.source[source_code]$$"/>
</actionGroup>
<fillField selector="{{AdminProductSourcesGrid.rowQty('1')}}" userInput="100" stepKey="setProductQuantity" after="assignSourceToCreatedProduct"/>
<click selector="{{AdminProductFormActionSection.saveButton}}" stepKey="saveProduct" after="setProductQuantity"/>
</before>
<after>
<!--Assign Default Stock to Default Website.-->
<actionGroup ref="AssignWebsiteToStockActionGroup" stepKey="assignMainWebsiteToDefaultStock" before="disableCreatedSource">
<argument name="stockName" value="{{_defaultStock.name}}"/>
<argument name="websiteName" value="{{_defaultWebsite.name}}"/>
</actionGroup>
<!--Disable additional source.-->
<actionGroup ref="DisableSourceActionGroup" stepKey="disableCreatedSource" before="logout">
<argument name="sourceCode" value="$$additionalSource.source[source_code]$$"/>
</actionGroup>
<actionGroup ref="logout" stepKey="logout" before="deleteStock"/>
<deleteData createDataKey="additionalStock" stepKey="deleteStock" before="deleteSimpleProduct"/>
</after>
</test>
</tests>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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="AddConfigurableFromCustomStockBySkuFromCustomerAccountTest" extends="AddConfigurableProductBySkuFromCustomerAccountTest">
<annotations>
<description value="Add configurable product by sku from customer account, Additional Stock."/>
<group value="msi"/>
<group value="multi_mode"/>
</annotations>

<before>
<!--Create additional stock ans source.-->
<createData entity="_minimalSource" stepKey="additionalSource" before="additionalStock"/>
<createData entity="BasicMsiStockWithMainWebsite1" stepKey="additionalStock" before="stockSourceLink"/>
<!--Link additional source with stock.-->
<createData entity="SourceStockLinked1" stepKey="stockSourceLink" before="createConfigChildProductOne">
<requiredEntity createDataKey="additionalStock"/>
<requiredEntity createDataKey="additionalSource"/>
</createData>

<!--Assign product variation one to additional stock and set qty.-->
<actionGroup ref="LoginAsAdmin" stepKey="loginToAdminPanel" after="createConfigProductAddChildTwo"/>
<amOnPage url="{{AdminProductEditPage.url($$createConfigChildProductOne.id$$)}}" stepKey="openProductVariationOneEditPageToSetQty" after="loginToAdminPanel"/>
<actionGroup ref="AssignSourceToProductActionGroup" stepKey="assignSourceToProductVariationOne" after="openProductVariationOneEditPageToSetQty">
<argument name="sourceCode" value="$$additionalSource.source[source_code]$$"/>
</actionGroup>
<fillField selector="{{AdminProductSourcesGrid.rowQty('1')}}" userInput="1000" stepKey="setProductVariationOneQuantity" after="assignSourceToProductVariationOne"/>
<click selector="{{AdminProductFormActionSection.saveButton}}" stepKey="saveProductVariationOne" after="setProductVariationOneQuantity"/>
<!--Assign product variation two to additional stock and set qty.-->
<amOnPage url="{{AdminProductEditPage.url($$createConfigChildProductTwo.id$$)}}" stepKey="openProductVariationTwoEditPageToSetQty" after="saveProductVariationOne"/>
<actionGroup ref="AssignSourceToProductActionGroup" stepKey="assignSourceToProductVariationTwo" after="openProductVariationTwoEditPageToSetQty">
<argument name="sourceCode" value="$$additionalSource.source[source_code]$$"/>
</actionGroup>
<fillField selector="{{AdminProductSourcesGrid.rowQty('1')}}" userInput="100" stepKey="setProductVariationTwoQuantity" after="assignSourceToProductVariationTwo"/>
<click selector="{{AdminProductFormActionSection.saveButton}}" stepKey="saveProductVariationTwo" after="setProductVariationTwoQuantity"/>
</before>
<after>
<!--Assign Default Stock to Default Website.-->
<actionGroup ref="AssignWebsiteToStockActionGroup" stepKey="assignMainWebsiteToDefaultStock" before="disableCreatedSource">
<argument name="stockName" value="{{_defaultStock.name}}"/>
<argument name="websiteName" value="{{_defaultWebsite.name}}"/>
</actionGroup>
<!--Disable additional source.-->
<actionGroup ref="DisableSourceActionGroup" stepKey="disableCreatedSource" before="logout">
<argument name="sourceCode" value="$$additionalSource.source[source_code]$$"/>
</actionGroup>
<actionGroup ref="logout" stepKey="logout" before="deleteStock"/>
<deleteData createDataKey="additionalStock" stepKey="deleteStock" before="deleteCustomer"/>
</after>
</test>
</tests>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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="AddSimpleFromCustomStockBySkuFromCustomerAccountTest" extends="AddSimpleProductBySkuFromCustomerAccountTest">
<annotations>
<description value="Add simple product by sku from customer account on additional stock."/>
<group value="msi"/>
<group value="multi_mode"/>
</annotations>
<before>
<!--Create additional stock ans source.-->
<createData entity="_minimalSource" stepKey="additionalSource" before="additionalStock"/>
<createData entity="BasicMsiStockWithMainWebsite1" stepKey="additionalStock" before="stockSourceLink"/>
<!--Link additional source with stock.-->
<createData entity="SourceStockLinked1" stepKey="stockSourceLink" before="createSimpleProduct">
<requiredEntity createDataKey="additionalStock"/>
<requiredEntity createDataKey="additionalSource"/>
</createData>
<!--Assign create product to additional stock and set qty.-->
<actionGroup ref="LoginAsAdmin" stepKey="loginToAdminPanel" after="createSimpleProduct"/>
<amOnPage url="{{AdminProductEditPage.url($$createSimpleProduct.id$$)}}" stepKey="openProductEditPageToSetQty" after="loginToAdminPanel"/>
<actionGroup ref="AssignSourceToProductActionGroup" stepKey="assignSourceToCreatedProduct" after="openProductEditPageToSetQty">
<argument name="sourceCode" value="$$additionalSource.source[source_code]$$"/>
</actionGroup>
<fillField selector="{{AdminProductSourcesGrid.rowQty('1')}}" userInput="100" stepKey="setProductQuantity" after="assignSourceToCreatedProduct"/>
<click selector="{{AdminProductFormActionSection.saveButton}}" stepKey="saveProduct" after="setProductQuantity"/>
</before>
<after>
<!--Assign Default Stock to Default Website.-->
<actionGroup ref="AssignWebsiteToStockActionGroup" stepKey="assignMainWebsiteToDefaultStock" before="disableCreatedSource">
<argument name="stockName" value="{{_defaultStock.name}}"/>
<argument name="websiteName" value="{{_defaultWebsite.name}}"/>
</actionGroup>
<!--Disable additional source.-->
<actionGroup ref="DisableSourceActionGroup" stepKey="disableCreatedSource" before="logout">
<argument name="sourceCode" value="$$additionalSource.source[source_code]$$"/>
</actionGroup>
<actionGroup ref="logout" stepKey="logout" before="deleteStock"/>
<deleteData createDataKey="additionalStock" stepKey="deleteStock" before="deleteSimpleProduct"/>
</after>
</test>
</tests>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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="AddSimpleOutOfStockFromCustomStockBySkuFromCustomerAccountTest" extends="AddSimpleOutOfStockProductBySkuFromCustomerAccountTest">
<annotations>
<stories value="Add Product By SKU from custom stock"/>
<title value="Add simple out of stock product by sku from customer account test, additional stock."/>
<group value="msi"/>
<group value="multi_mode"/>
</annotations>
<before>
<!--Create additional stock ans source.-->
<createData entity="_minimalSource" stepKey="additionalSource" before="additionalStock"/>
<createData entity="BasicMsiStockWithMainWebsite1" stepKey="additionalStock" before="stockSourceLink"/>
<!--Link additional source with stock.-->
<createData entity="SourceStockLinked1" stepKey="stockSourceLink" before="createSimpleProduct">
<requiredEntity createDataKey="additionalStock"/>
<requiredEntity createDataKey="additionalSource"/>
</createData>
<!--Assign create product to additional stock and set qty.-->
<actionGroup ref="LoginAsAdmin" stepKey="loginToAdminPanel" after="createSimpleProduct"/>
<amOnPage url="{{AdminProductEditPage.url($$createSimpleProduct.id$$)}}" stepKey="openProductEditPageToSetQty" after="loginToAdminPanel"/>
<actionGroup ref="AssignSourceToProductActionGroup" stepKey="assignSourceToCreatedProduct" after="openProductEditPageToSetQty">
<argument name="sourceCode" value="$$additionalSource.source[source_code]$$"/>
</actionGroup>
<fillField selector="{{AdminProductSourcesGrid.rowQty('1')}}" userInput="0" stepKey="setProductQuantity" after="assignSourceToCreatedProduct"/>
<click selector="{{AdminProductFormActionSection.saveButton}}" stepKey="saveProduct" after="setProductQuantity"/>
</before>
<after>
<!--Assign Default Stock to Default Website.-->
<actionGroup ref="AssignWebsiteToStockActionGroup" stepKey="assignMainWebsiteToDefaultStock" before="disableCreatedSource">
<argument name="stockName" value="{{_defaultStock.name}}"/>
<argument name="websiteName" value="{{_defaultWebsite.name}}"/>
</actionGroup>
<!--Disable additional source.-->
<actionGroup ref="DisableSourceActionGroup" stepKey="disableCreatedSource" before="logout">
<argument name="sourceCode" value="$$additionalSource.source[source_code]$$"/>
</actionGroup>
<actionGroup ref="logout" stepKey="logout" before="deleteStock"/>
<deleteData createDataKey="additionalStock" stepKey="deleteStock" before="deleteSimpleProduct"/>
</after>
</test>
</tests>
28 changes: 28 additions & 0 deletions InventoryAdvancedCheckout/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "magento/module-inventory-advanced-checkout",
"description": "N/A",
"require": {
"php": "~7.1.3||~7.2.0||~7.3.0",
"magento/framework": "*",
"magento/module-catalog": "*",
"magento/module-store": "*",
"magento/module-inventory-sales-api": "*",
"magento/module-inventory-configuration-api": "*"
},
"suggest": {
"magento/module-advanced-checkout": "*"
},
"type": "magento2-module",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\InventoryAdvancedCheckout\\": ""
}
}
}
12 changes: 12 additions & 0 deletions InventoryAdvancedCheckout/etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\AdvancedCheckout\Model\IsProductInStockInterface">
<plugin name="inventory_advanced_checkout_is_in_stock" type="Magento\InventoryAdvancedCheckout\Plugin\Model\IsProductInStock\ProductInStockPlugin"/>
</type>
</config>
10 changes: 10 additions & 0 deletions InventoryAdvancedCheckout/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_InventoryAdvancedCheckout" />
</config>
12 changes: 12 additions & 0 deletions InventoryAdvancedCheckout/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Magento_InventoryAdvancedCheckout',
__DIR__
);
Loading

0 comments on commit 873ac41

Please sign in to comment.