I have 2 different website in my Magento. I want to store different url key for each website. How to store it programatically ?
asked Aug 23, 2019 at 8:58
1 Answer 1
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');
}
answered Aug 29, 2019 at 5:44
default