Magento 2 add/update meta values of configure products using custom script
<?php
//increase the max execution time
@ini_set('max_execution_time', -1);
//memory_limit
@ini_set('memory_limit', -1);
error_reporting(E_ALL);
ini_set('display_errors', '1');
use \Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$instance = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$product_collections = $instance->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collections = $product_collections->create()->addAttributeToFilter('type_id', 'configurable');
foreach ($collections as $product) {
echo "<pre>";
echo 'Produc Id : ' . $product->getId();
echo "<br>";
$sku = $product->getSku();
echo 'Produc SKU : ' . $sku;
echo "<br>";
$productIdBySku = $product->getIdBySku($sku);
echo "<br>";
echo 'Produc Id BY SKU : ' . $productIdBySku;
echo "<br>";
$product = $objectManager->get('\Magento\Catalog\Model\Product')->setStoreId(0)->load($productIdBySku);
//$category = $objectManager->create('Magento\Catalog\Model\CategoryFactory')->create()->setStoreId(0)->load($catId)
//echo $product->getName();
echo 'Produc Name : ' . $productTitle = $product->getName();
$metaTitle = $productTitle . ' {Content}';
$metaDescription = '{Content} ' . $productTitle . ' {Content}.';
$product->setMetaTitle($metaTitle);
$product->setMetaDescription($metaDescription);
$product->save();
echo "<pre>";
echo $product->getMetaTitle();
echo "</br>";
echo $product->getMetaDescription();
/*}*/
}
?>
Comments
Post a Comment