|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\InventorySales\Setup\Operation; |
| 9 | + |
| 10 | +use Magento\InventoryApi\Api\StockRepositoryInterface; |
| 11 | +use Magento\InventoryCatalog\Api\DefaultStockProviderInterface; |
| 12 | +use Magento\InventorySalesApi\Api\Data\SalesChannelInterface; |
| 13 | +use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory; |
| 14 | +use Magento\Store\Model\StoreManagerInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Assigns Main website to the Default stock |
| 18 | + */ |
| 19 | +class AssignWebsiteToDefaultStock |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var StockRepositoryInterface |
| 23 | + */ |
| 24 | + private $stockRepository; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var DefaultStockProviderInterface |
| 28 | + */ |
| 29 | + private $defaultStockProvider; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var SalesChannelInterfaceFactory |
| 33 | + */ |
| 34 | + private $salesChannelFactory; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var StoreManagerInterface |
| 38 | + */ |
| 39 | + private $storeManager; |
| 40 | + |
| 41 | + /** |
| 42 | + * @param StockRepositoryInterface $stockRepository |
| 43 | + * @param DefaultStockProviderInterface $defaultStockProvider |
| 44 | + * @param SalesChannelInterfaceFactory $salesChannelFactory |
| 45 | + * @param StoreManagerInterface $storeManager |
| 46 | + */ |
| 47 | + public function __construct( |
| 48 | + StockRepositoryInterface $stockRepository, |
| 49 | + DefaultStockProviderInterface $defaultStockProvider, |
| 50 | + SalesChannelInterfaceFactory $salesChannelFactory, |
| 51 | + StoreManagerInterface $storeManager |
| 52 | + ) { |
| 53 | + $this->stockRepository = $stockRepository; |
| 54 | + $this->defaultStockProvider = $defaultStockProvider; |
| 55 | + $this->salesChannelFactory = $salesChannelFactory; |
| 56 | + $this->storeManager = $storeManager; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @inheritdoc |
| 61 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 62 | + * @throws \Magento\Framework\Exception\CouldNotSaveException |
| 63 | + * @throws \Magento\Framework\Validation\ValidationException |
| 64 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 65 | + */ |
| 66 | + public function execute() |
| 67 | + { |
| 68 | + $websiteCode = $this->storeManager->getWebsite()->getCode(); |
| 69 | + |
| 70 | + $defaultStockId = $this->defaultStockProvider->getId(); |
| 71 | + $defaultStock = $this->stockRepository->get($defaultStockId); |
| 72 | + |
| 73 | + $extensionAttributes = $defaultStock->getExtensionAttributes(); |
| 74 | + $salesChannels = $extensionAttributes->getSalesChannels(); |
| 75 | + $salesChannels[] = $this->createSalesChannelByWebsiteCode($websiteCode); |
| 76 | + |
| 77 | + $extensionAttributes->setSalesChannels($salesChannels); |
| 78 | + $this->stockRepository->save($defaultStock); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Create the sales channel by given website code |
| 83 | + * |
| 84 | + * @param string $websiteCode |
| 85 | + * @return SalesChannelInterface |
| 86 | + */ |
| 87 | + private function createSalesChannelByWebsiteCode(string $websiteCode): SalesChannelInterface |
| 88 | + { |
| 89 | + $salesChannel = $this->salesChannelFactory->create(); |
| 90 | + $salesChannel->setCode($websiteCode); |
| 91 | + $salesChannel->setType(SalesChannelInterface::TYPE_WEBSITE); |
| 92 | + return $salesChannel; |
| 93 | + } |
| 94 | +} |
0 commit comments