Skip to content

Theme information lost after set product price in php code

I changed the product price in Controller, but i found the product ‘s theme shown as the default theme but not the theme defined in “Schedule Design Update ” section

the codes is as bellows:

public function getProductBySku($sku)

{

//using MagentoCatalogModelProductRepository $productRepository
try {
$_product = $this->_productRepository->get($sku);
}
catch (Exception $e)
{
$_product = false;
}
catch (MagentoFrameworkExceptionNoSuchEntityException $e)
{
$_product = false;
}
return $_product;
}

Call this function here:
$product = $this->getProductBySku($sku);
$product->setPrice($price);
$product->save();

Then i found the product’s view page using the default theme but not the one i define in in “Schedule Design Update ” section
To solve this problem, i went to magento admin page and just saved the product again, then product view page will use the theme i expected

So who can tell what happened, why such issue was raised and how to fix it

i am using magento 2.4

Thank you