AssignWebsiteToDefaultStock.php 2.9 KB

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