-
Notifications
You must be signed in to change notification settings - Fork 248
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 #165 from magento-engcom/msi-website-default-stock
Assign Website at the time of Creation to the Default Stock
- Loading branch information
Showing
15 changed files
with
536 additions
and
15 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
app/code/Magento/InventorySales/Model/DeleteSalesChannelToStockLinkInterface.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,23 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventorySales\Model; | ||
|
||
/** | ||
* Delete link between Stock and Sales Channel (Service Provider Interface - SPI) | ||
* | ||
* @api | ||
*/ | ||
interface DeleteSalesChannelToStockLinkInterface | ||
{ | ||
/** | ||
* @param string $type | ||
* @param string $code | ||
* @return void | ||
*/ | ||
public function execute(string $type, string $code); | ||
} |
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
24 changes: 24 additions & 0 deletions
24
app/code/Magento/InventorySales/Model/GetAssignedStockIdForWebsiteInterface.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,24 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventorySales\Model; | ||
|
||
/** | ||
* Get assigned Stock id for Website (Service Provider Interface - SPI) | ||
* | ||
* @api | ||
*/ | ||
interface GetAssignedStockIdForWebsiteInterface | ||
{ | ||
/** | ||
* Get assigned stock to website | ||
* | ||
* @param string $websiteCode | ||
* @return int|null | ||
*/ | ||
public function execute(string $websiteCode); | ||
} |
50 changes: 50 additions & 0 deletions
50
app/code/Magento/InventorySales/Model/ResourceModel/DeleteSalesChannelToStockLink.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\InventorySales\Model\ResourceModel; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\InventorySales\Model\DeleteSalesChannelToStockLinkInterface; | ||
use Magento\InventorySales\Setup\Operation\CreateSalesChannelTable; | ||
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface; | ||
|
||
/** | ||
* Implementation of link deleting between Stock and Sales Channels for specific db layer | ||
* | ||
* There is no additional business logic on SPI (Service Provider Interface) level so could use resource model as | ||
* SPI implementation directly | ||
*/ | ||
class DeleteSalesChannelToStockLink implements DeleteSalesChannelToStockLinkInterface | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @param ResourceConnection $resourceConnection | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resourceConnection | ||
) { | ||
$this->resourceConnection = $resourceConnection; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(string $type, string $code) | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$tableName = $this->resourceConnection->getTableName(CreateSalesChannelTable::TABLE_NAME_SALES_CHANNEL); | ||
|
||
$connection->delete($tableName, [ | ||
SalesChannelInterface::TYPE . ' = ?' => $type, | ||
SalesChannelInterface::CODE . ' = ?' => $code, | ||
]); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
app/code/Magento/InventorySales/Model/ResourceModel/GetAssignedStockIdForWebsite.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,54 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventorySales\Model\ResourceModel; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\InventorySales\Model\GetAssignedStockIdForWebsiteInterface; | ||
use Magento\InventorySales\Setup\Operation\CreateSalesChannelTable; | ||
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class GetAssignedStockIdForWebsite implements GetAssignedStockIdForWebsiteInterface | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @param ResourceConnection $resourceConnection | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resourceConnection | ||
) { | ||
$this->resourceConnection = $resourceConnection; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(string $websiteCode) | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$tableName = $this->resourceConnection->getTableName(CreateSalesChannelTable::TABLE_NAME_SALES_CHANNEL); | ||
|
||
$select = $connection->select() | ||
->from($tableName, [CreateSalesChannelTable::STOCK_ID]) | ||
->where('code = ?', $websiteCode) | ||
->where('type = ?', SalesChannelInterface::TYPE_WEBSITE); | ||
|
||
$result = $connection->fetchCol($select); | ||
|
||
if (count($result) === 0) { | ||
return null; | ||
} | ||
return reset($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
105 changes: 105 additions & 0 deletions
105
app/code/Magento/InventorySales/Observer/Website/AssignWebsiteToDefaultStock.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,105 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventorySales\Observer\Website; | ||
|
||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\InventoryApi\Api\StockRepositoryInterface; | ||
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface; | ||
use Magento\InventorySales\Model\GetAssignedStockIdForWebsiteInterface; | ||
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface; | ||
use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory; | ||
use Magento\Store\Api\Data\WebsiteInterface; | ||
use Magento\Store\Model\Website; | ||
|
||
/** | ||
* Assign the website to the default stock | ||
*/ | ||
class AssignWebsiteToDefaultStock implements ObserverInterface | ||
{ | ||
/** | ||
* @var StockRepositoryInterface | ||
*/ | ||
private $stockRepository; | ||
|
||
/** | ||
* @var DefaultStockProviderInterface | ||
*/ | ||
private $defaultStockProvider; | ||
|
||
/** | ||
* @var SalesChannelInterfaceFactory | ||
*/ | ||
private $salesChannelFactory; | ||
|
||
/** | ||
* @var GetAssignedStockIdForWebsiteInterface | ||
*/ | ||
private $getAssignedStockIdForWebsite; | ||
|
||
/** | ||
* @param StockRepositoryInterface $stockRepository | ||
* @param DefaultStockProviderInterface $defaultStockProvider | ||
* @param SalesChannelInterfaceFactory $salesChannelFactory | ||
* @param GetAssignedStockIdForWebsiteInterface $getAssignedStockIdForWebsite | ||
*/ | ||
public function __construct( | ||
StockRepositoryInterface $stockRepository, | ||
DefaultStockProviderInterface $defaultStockProvider, | ||
SalesChannelInterfaceFactory $salesChannelFactory, | ||
GetAssignedStockIdForWebsiteInterface $getAssignedStockIdForWebsite | ||
) { | ||
$this->stockRepository = $stockRepository; | ||
$this->defaultStockProvider = $defaultStockProvider; | ||
$this->salesChannelFactory = $salesChannelFactory; | ||
$this->getAssignedStockIdForWebsite = $getAssignedStockIdForWebsite; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
/** @var Website $website */ | ||
$website = $observer->getData('website'); | ||
$websiteCode = $website->getCode(); | ||
|
||
if ($websiteCode === WebsiteInterface::ADMIN_CODE) { | ||
return; | ||
} | ||
|
||
// checks is some stock already assigned to this website | ||
if ($this->getAssignedStockIdForWebsite->execute($websiteCode) !== null) { | ||
return; | ||
} | ||
|
||
$defaultStockId = $this->defaultStockProvider->getId(); | ||
$defaultStock = $this->stockRepository->get($defaultStockId); | ||
|
||
$extensionAttributes = $defaultStock->getExtensionAttributes(); | ||
$salesChannels = $extensionAttributes->getSalesChannels(); | ||
$salesChannels[] = $this->createSalesChannelByWebsiteCode($websiteCode); | ||
|
||
$extensionAttributes->setSalesChannels($salesChannels); | ||
$this->stockRepository->save($defaultStock); | ||
} | ||
|
||
/** | ||
* Create the sales channel by given website code | ||
* | ||
* @param string $websiteCode | ||
* @return SalesChannelInterface | ||
*/ | ||
private function createSalesChannelByWebsiteCode(string $websiteCode): SalesChannelInterface | ||
{ | ||
$salesChannel = $this->salesChannelFactory->create(); | ||
$salesChannel->setCode($websiteCode); | ||
$salesChannel->setType(SalesChannelInterface::TYPE_WEBSITE); | ||
return $salesChannel; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
app/code/Magento/InventorySales/Observer/Website/DeleteWebsiteToStockLink.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\InventorySales\Observer\Website; | ||
|
||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\InventorySales\Model\DeleteSalesChannelToStockLinkInterface; | ||
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface; | ||
use Magento\Store\Api\Data\WebsiteInterface; | ||
use Magento\Store\Model\Website; | ||
|
||
/** | ||
* Delete link between Stock and Website | ||
*/ | ||
class DeleteWebsiteToStockLink implements ObserverInterface | ||
{ | ||
/** | ||
* @var DeleteSalesChannelToStockLinkInterface | ||
*/ | ||
private $deleteSalesChannelToStockLink; | ||
|
||
/** | ||
* @param DeleteSalesChannelToStockLinkInterface $deleteSalesChannelToStockLink | ||
*/ | ||
public function __construct( | ||
DeleteSalesChannelToStockLinkInterface $deleteSalesChannelToStockLink | ||
) { | ||
$this->deleteSalesChannelToStockLink = $deleteSalesChannelToStockLink; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
/** @var Website $website */ | ||
$website = $observer->getData('website'); | ||
$websiteCode = $website->getCode(); | ||
|
||
if ($websiteCode === WebsiteInterface::ADMIN_CODE) { | ||
return; | ||
} | ||
$this->deleteSalesChannelToStockLink->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode); | ||
} | ||
} |
Oops, something went wrong.