0

Magento 2.4.6-p4

I am chaning the url key by using

$product->setUrlKey('newkey1234');
$product->save();

Now, when I view this product in admin, it still shows the old url key. Loading the product and printing the url key shows the new key:

var_dump($product->getUrlKey());

So, how can I also make it change in admin?

Thanks!

0

3 Answers 3

1

You can defne the storeID as well. Please do as given below :

<?php
namespace vendor\module\Block\Quote;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;
class Items extends Template {

public function __construct(
    Context $context,
    ProductRepositoryInterface $productrepo,
    ProductFactory $product,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->productrepo = $productrepo;
    $this->product = $product;
}

public function ProductUrl($sku)
{
    $product = $this->productrepo->get($sku);
    $product->setStoreId(0);//setting the value on all store view
    $product->setUrlKey('add your url key'); // Replace with your desired URL key
    $this->productrepo->save($product);
}
}
0

Just a wild guess, but maybe you're missing setUrlPath()?

$product->setUrlPath('newkey1234');
$product->setUrlKey('newkey1234');
$product->save();
0

First please run bin/magento s:up;bin/magento index:reindex and then check

if doesn't work still run this command and let me know the result, might be attribute scope issue.

select a.*
from `catalog_product_entity_varchar` a
join `eav_attribute` b
    on b.attribute_id = a.attribute_id
where b.attribute_code = 'url_key'
and a.entity_id = 12673;

change entity_id with your product id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.