diff --git a/app/code/Magento/InventorySales/Model/ResourceModel/GetWebsiteCodeByWebsiteId.php b/app/code/Magento/InventorySales/Model/ResourceModel/GetWebsiteCodeByWebsiteId.php
new file mode 100644
index 000000000000..0ab00435ded8
--- /dev/null
+++ b/app/code/Magento/InventorySales/Model/ResourceModel/GetWebsiteCodeByWebsiteId.php
@@ -0,0 +1,44 @@
+resourceConnection = $resourceConnection;
+ }
+
+ /**
+ * @param int $websiteId
+ * @return string|null
+ */
+ public function execute(int $websiteId): ?string
+ {
+ $connection = $this->resourceConnection->getConnection();
+ $tableName = $this->resourceConnection->getTableName('store_website');
+ $selectQry = $connection->select()->from($tableName, 'code')->where('website_id = ?', $websiteId);
+
+ $result = $connection->fetchOne($selectQry);
+ return (false === $result) ? null : $result;
+ }
+}
diff --git a/app/code/Magento/InventorySales/Model/ResourceModel/UpdateSalesChannelWebsiteCode.php b/app/code/Magento/InventorySales/Model/ResourceModel/UpdateSalesChannelWebsiteCode.php
new file mode 100644
index 000000000000..6b7ebf28b60e
--- /dev/null
+++ b/app/code/Magento/InventorySales/Model/ResourceModel/UpdateSalesChannelWebsiteCode.php
@@ -0,0 +1,57 @@
+resourceConnection = $resourceConnection;
+ }
+
+ /**
+ * @param string $oldCode
+ * @param string $newCode
+ * @return void
+ */
+ public function execute(
+ string $oldCode,
+ string $newCode
+ ): void {
+ $connection = $this->resourceConnection->getConnection();
+ $tableName = $this->resourceConnection->getTableName('inventory_stock_sales_channel');
+
+ $connection->update(
+ $tableName,
+ [
+ SalesChannelInterface::CODE => $newCode,
+ ],
+ [
+ SalesChannelInterface::TYPE . ' = ?' => SalesChannelInterface::TYPE_WEBSITE,
+ SalesChannelInterface::CODE . ' = ?' => $oldCode,
+ ]
+ );
+ }
+}
diff --git a/app/code/Magento/InventorySales/Observer/Website/AssignWebsiteToDefaultStock.php b/app/code/Magento/InventorySales/Plugin/Store/Model/ResourceModel/Website/AssignWebsiteToDefaultStockPlugin.php
similarity index 76%
rename from app/code/Magento/InventorySales/Observer/Website/AssignWebsiteToDefaultStock.php
rename to app/code/Magento/InventorySales/Plugin/Store/Model/ResourceModel/Website/AssignWebsiteToDefaultStockPlugin.php
index 81803bdd214b..1986849c383b 100644
--- a/app/code/Magento/InventorySales/Observer/Website/AssignWebsiteToDefaultStock.php
+++ b/app/code/Magento/InventorySales/Plugin/Store/Model/ResourceModel/Website/AssignWebsiteToDefaultStockPlugin.php
@@ -5,22 +5,25 @@
*/
declare(strict_types=1);
-namespace Magento\InventorySales\Observer\Website;
+namespace Magento\InventorySales\Plugin\Store\Model\ResourceModel\Website;
-use Magento\Framework\Event\Observer;
-use Magento\Framework\Event\ObserverInterface;
+use Magento\Framework\Exception\CouldNotSaveException;
+use Magento\Framework\Exception\NoSuchEntityException;
+use Magento\Framework\Model\AbstractModel;
+use Magento\Framework\Validation\ValidationException;
use Magento\InventoryApi\Api\StockRepositoryInterface;
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
use Magento\InventorySalesApi\Model\GetAssignedStockIdForWebsiteInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;
use Magento\Store\Api\Data\WebsiteInterface;
+use Magento\Store\Model\ResourceModel\Website as WebsiteResourceModel;
use Magento\Store\Model\Website;
/**
* Assign the website to the default stock
*/
-class AssignWebsiteToDefaultStock implements ObserverInterface
+class AssignWebsiteToDefaultStockPlugin
{
/**
* @var StockRepositoryInterface
@@ -61,21 +64,29 @@ public function __construct(
}
/**
- * @inheritdoc
+ * @param WebsiteResourceModel $subject
+ * @param WebsiteResourceModel $result
+ * @param Website|AbstractModel $website
+ * @return WebsiteResourceModel
+ * @throws CouldNotSaveException
+ * @throws NoSuchEntityException
+ * @throws ValidationException
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
- public function execute(Observer $observer)
- {
- /** @var Website $website */
- $website = $observer->getData('website');
+ public function afterSave(
+ WebsiteResourceModel $subject,
+ WebsiteResourceModel $result,
+ AbstractModel $website
+ ) {
$websiteCode = $website->getCode();
if ($websiteCode === WebsiteInterface::ADMIN_CODE) {
- return;
+ return $result;
}
// checks is some stock already assigned to this website
if ($this->getAssignedStockIdForWebsite->execute($websiteCode) !== null) {
- return;
+ return $result;
}
$defaultStockId = $this->defaultStockProvider->getId();
@@ -87,6 +98,8 @@ public function execute(Observer $observer)
$extensionAttributes->setSalesChannels($salesChannels);
$this->stockRepository->save($defaultStock);
+
+ return $result;
}
/**
diff --git a/app/code/Magento/InventorySales/Plugin/Store/Model/ResourceModel/Website/UpdateSalesChannelWebsiteCodePlugin.php b/app/code/Magento/InventorySales/Plugin/Store/Model/ResourceModel/Website/UpdateSalesChannelWebsiteCodePlugin.php
new file mode 100644
index 000000000000..c8e6c1bf2e7e
--- /dev/null
+++ b/app/code/Magento/InventorySales/Plugin/Store/Model/ResourceModel/Website/UpdateSalesChannelWebsiteCodePlugin.php
@@ -0,0 +1,73 @@
+updateSalesChannelWebsiteCode = $updateSalesChannelWebsiteCode;
+ $this->getWebsiteCodeByWebsiteId = $getWebsiteCodeByWebsiteId;
+ }
+
+ /**
+ * @param WebsiteResourceModel $subject
+ * @param callable $proceed
+ * @param Website|AbstractModel $website
+ * @return WebsiteResourceModel
+ * @throws CouldNotSaveException
+ * @throws NoSuchEntityException
+ * @throws ValidationException
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+ */
+ public function aroundSave(
+ WebsiteResourceModel $subject,
+ callable $proceed,
+ AbstractModel $website
+ ) {
+ $newCode = $website->getCode();
+ $oldCode = null;
+
+ if (null !== $website->getId()) {
+ $oldCode = $this->getWebsiteCodeByWebsiteId->execute((int)$website->getId());
+ }
+
+ $result = $proceed($website);
+
+ if (($oldCode !== null) && ($oldCode !== WebsiteInterface::ADMIN_CODE) && ($oldCode !== $newCode)) {
+ $this->updateSalesChannelWebsiteCode->execute($oldCode, $newCode);
+ }
+ return $result;
+ }
+}
diff --git a/app/code/Magento/InventorySales/Test/Integration/Website/RenameWebsiteToStockLinkTest.php b/app/code/Magento/InventorySales/Test/Integration/Website/RenameWebsiteToStockLinkTest.php
new file mode 100644
index 000000000000..025e6342c599
--- /dev/null
+++ b/app/code/Magento/InventorySales/Test/Integration/Website/RenameWebsiteToStockLinkTest.php
@@ -0,0 +1,73 @@
+websiteFactory = Bootstrap::getObjectManager()->get(WebsiteFactory::class);
+ $this->getAssignedStockIdForWebsite = Bootstrap::getObjectManager()->get(
+ GetAssignedStockIdForWebsiteInterface::class
+ );
+ $this->websiteResource = Bootstrap::getObjectManager()->get(WebsiteResource::class);
+ }
+
+ /**
+ * @throws \Exception
+ * @magentoDbIsolation enabled
+ */
+ public function testRenameWebsiteToStockLink()
+ {
+ $oldWebsiteCode = 'old_website_code';
+ $newWebsiteCode = 'new_website_code';
+
+ /** @var Website $website */
+ $website = $this->websiteFactory->create();
+ $website->setCode($oldWebsiteCode);
+ $this->websiteResource->save($website);
+ $websiteId = $website->getId();
+
+ $website = $this->websiteFactory->create();
+ $this->websiteResource->load($website, $websiteId);
+ $website->setCode($newWebsiteCode);
+ $this->websiteResource->save($website);
+
+ self::assertNull(
+ $this->getAssignedStockIdForWebsite->execute($oldWebsiteCode),
+ 'Old website link was not removed'
+ );
+
+ self::assertNotNull(
+ $this->getAssignedStockIdForWebsite->execute($newWebsiteCode),
+ 'Website link was not renamed'
+ );
+ }
+}
diff --git a/app/code/Magento/InventorySales/etc/di.xml b/app/code/Magento/InventorySales/etc/di.xml
index 9bb3139def7b..83fa79c73e2d 100644
--- a/app/code/Magento/InventorySales/etc/di.xml
+++ b/app/code/Magento/InventorySales/etc/di.xml
@@ -146,4 +146,8 @@
+
+
+
+
diff --git a/app/code/Magento/InventorySales/etc/events.xml b/app/code/Magento/InventorySales/etc/events.xml
index c6e0f3b4f280..89a00ddb9670 100644
--- a/app/code/Magento/InventorySales/etc/events.xml
+++ b/app/code/Magento/InventorySales/etc/events.xml
@@ -6,9 +6,6 @@
*/
-->
-
-
-
diff --git a/app/code/Magento/InventorySalesAdminUi/Ui/Component/Listing/Column/SalesChannels.php b/app/code/Magento/InventorySalesAdminUi/Ui/Component/Listing/Column/SalesChannels.php
index 7ddf1748d69e..4f07ec11f3c6 100644
--- a/app/code/Magento/InventorySalesAdminUi/Ui/Component/Listing/Column/SalesChannels.php
+++ b/app/code/Magento/InventorySalesAdminUi/Ui/Component/Listing/Column/SalesChannels.php
@@ -7,6 +7,7 @@
namespace Magento\InventorySalesAdminUi\Ui\Component\Listing\Column;
+use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventorySalesAdminUi\Ui\SalesChannelNameResolverInterface;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\View\Element\UiComponentFactory;