Have following code:

/** @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory */
$productCollection = $productCollectionFactory->create();
/** @var \Magento\Catalog\Model\Product $_product */
foreach($productCollection as $_product) {
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$product = $productRepository->get($_product->getSku(), true);
// if(in_array(2, $product->getWebsiteIds())):
$metaDescription = "custom string based on websiteId";

try {
    echo "saved product ".$product->getName().PHP_EOL;
} catch (\Exception $e){
    echo "couldn't save product ".$product->getName().PHP_EOL;

in console i see only messages that products were saved, but when i lookup product in backend Meta description is empty.

any other way to save product so that websiteIds will be intact ?

since if use objectmanager to load product

$objectManager = $bootstrap->getObjectManager();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load(1234);

i always get default website 1 meaning that i can't check if product is in another website.

  • Now really confused: if i add to the first part of my code : echo $product->getMetaDescription(); i do see my custom string there. but in backend or frontend i still see empty string / default value. Does it mean that value is saved for another store/website ? – Naberd Zendi Feb 25 at 16:10

wel it was indeed store problem. not sure why it was always loaded store 1. not 0 the default storeview but 1.

so solved problem by specifying store:

/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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