forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
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 magento#221 from magento-engcom/199_assign_main_we…
…bsite_to_default_stock magento#199 - Assign main website to default stock
- Loading branch information
Showing
9 changed files
with
195 additions
and
26 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventorySales\Setup; | ||
|
||
use Magento\Framework\Setup\InstallDataInterface; | ||
use Magento\Framework\Setup\ModuleDataSetupInterface; | ||
use Magento\Framework\Setup\ModuleContextInterface; | ||
use Magento\InventorySales\Setup\Operation\AssignWebsiteToDefaultStock; | ||
|
||
/** | ||
* Assigns Main website to the Default stock | ||
*/ | ||
class InstallData implements InstallDataInterface | ||
{ | ||
/** | ||
* @var AssignWebsiteToDefaultStock | ||
*/ | ||
private $assignWebsiteToDefaultStock; | ||
|
||
/** | ||
* @param AssignWebsiteToDefaultStock $assignWebsiteToDefaultStock | ||
*/ | ||
public function __construct( | ||
AssignWebsiteToDefaultStock $assignWebsiteToDefaultStock | ||
) { | ||
$this->assignWebsiteToDefaultStock = $assignWebsiteToDefaultStock; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) | ||
{ | ||
$this->assignWebsiteToDefaultStock->execute(); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
app/code/Magento/InventorySales/Setup/Operation/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,94 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventorySales\Setup\Operation; | ||
|
||
use Magento\InventoryApi\Api\StockRepositoryInterface; | ||
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface; | ||
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface; | ||
use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
/** | ||
* Assigns Main website to the Default stock | ||
*/ | ||
class AssignWebsiteToDefaultStock | ||
{ | ||
/** | ||
* @var StockRepositoryInterface | ||
*/ | ||
private $stockRepository; | ||
|
||
/** | ||
* @var DefaultStockProviderInterface | ||
*/ | ||
private $defaultStockProvider; | ||
|
||
/** | ||
* @var SalesChannelInterfaceFactory | ||
*/ | ||
private $salesChannelFactory; | ||
|
||
/** | ||
* @var StoreManagerInterface | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* @param StockRepositoryInterface $stockRepository | ||
* @param DefaultStockProviderInterface $defaultStockProvider | ||
* @param SalesChannelInterfaceFactory $salesChannelFactory | ||
* @param StoreManagerInterface $storeManager | ||
*/ | ||
public function __construct( | ||
StockRepositoryInterface $stockRepository, | ||
DefaultStockProviderInterface $defaultStockProvider, | ||
SalesChannelInterfaceFactory $salesChannelFactory, | ||
StoreManagerInterface $storeManager | ||
) { | ||
$this->stockRepository = $stockRepository; | ||
$this->defaultStockProvider = $defaultStockProvider; | ||
$this->salesChannelFactory = $salesChannelFactory; | ||
$this->storeManager = $storeManager; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* @throws \Magento\Framework\Exception\NoSuchEntityException | ||
* @throws \Magento\Framework\Exception\CouldNotSaveException | ||
* @throws \Magento\Framework\Validation\ValidationException | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
public function execute() | ||
{ | ||
$websiteCode = $this->storeManager->getWebsite()->getCode(); | ||
|
||
$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; | ||
} | ||
} |
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