Skip to main content

Magento 2 add/update configure products meta title, Description programetically using custom script

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

Popular posts from this blog

Magento 2: How to use redirect URl in Router.php

Module name - **Aks_CustomUrlRouter** You have to create below listed files 1. Registration.php :: app/code/Aks/CustomUrlRouter/registration.php <?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Aks_CustomUrlRouter', __DIR__ ); ?> 2. Module.xml :: app/code/Aks/CustomUrlRouter/etc/module.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Aks_CustomUrlRouter" setup_version="2.0.0"></module> </config> 3. di.xml :: app/code/Aks/CustomUrlRouter/etc/di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

Magento 2 get category collection using specific controllers and category level using object manager

Magento 2 get category collection using object manager <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $objectManager->get('Magento\Framework\Registry') ->registry('current_category'); //get current category $requestInterface = $objectManager->get('Magento\Framework\App\RequestInterface'); //echo $routeName = $requestInterface->getRouteName(); //echo $moduleName = $requestInterface->getModuleName(); $controllerName = $requestInterface->getControllerName(); //echo $actionName = $requestInterface->getActionName(); $parcatId = $category->getId(); // current Category ID $parcategory = $objectManager->create('Magento\Catalog\Model\Category') ->load($parcatId); $parent_category = $parcategory->getParentCategory(); $subcatId = $parent_category->getId(); $subcategory = $objectManager->create('Magento\Catalog\Model\Category

Magento 2 add single product using custom script or programmatically

Magento 2 add single product programmatically <?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); $bootstrapManager = $bootstrap->getObjectManager(); $state = $bootstrapManager->get('\Magento\Framework\App\State'); $state->setAreaCode('frontend'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager $product = $objectManager->create('\Magento\Catalog\Model\Product'); $sku = 'sku'; $name = 'Sample Product'; $product->setSku($sku); // Set your sku here $product->setName($name); // Name of Product $product->setAttributeSetId(4); // Attribu