Question

I have 2 different website in my Magento. I want to store different url key for each website. How to store it programatically ?

Was it helpful?

Solution

I able to achieve this solution by this :

public function __construct (
\Magento\Catalog\Model\ResourceModel\Product $productResourceModel
) {
  $this->productResourceModel = $productResourceModel;
}

public function changeUrlByStore($sku, $storeId = 1){
  $product = $this->productFactory->create();
  $productId = $this->productResourceModel->getIdBySku($sku);
  $this->productResourceModel->load($product, $productId);

  $product->setStoreId($storeId);
  $product->setUrlKey(UNIQUE_URL_KEY);
  $this->productResourceModel->saveAttribute($product, 'url_key');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top