Magento: How to update Category Name via install script
How to u开发者_JS百科pdate Category Name via install script in Magento?
<?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');
// Start Despaly All Product Meta Title And Description
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$categories = Mage::getModel('catalog/category')
->getCollection()
// magic is prepared here..
->addAttributeToSelect('*')
// then the magic happens here:
->addAttributeToFilter('level', array('eq'=>4))
->load();
if (count($categories) > 0):
foreach($categories as $category):
$catId = $category->getId();
$category = Mage::getModel('catalog/category')->load($catId);
if($catId==1465):
$CategoryName = $category->getName();
$CategoryNewName = "New Name";
$category->setName($metaTitle);
$category->save();
$check = $category->getName();
echo "<pre>";
print_r($check);
echo "\n";
endif;
endforeach;
else: echo "No Results";
endif;
?>
Install scripts are part of the application so can use the same models as normal.
$category = Mage::getModel('catalog/category')->load($categoryId);
// can alternatively use loadByAtrribute('name', $oldName)
$category->setName($newName)
->save();
$installer->run("
UPDATE catalog_category_entity_varchar SET value = '[value]' WHERE value_id = [value_id]
");
Magento 2 category Attribute Value Update by Script with level of the category
<?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;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryCollection->create();
$categories->addAttributeToSelect('*');
$categories->addAttributeToFilter('level', array('eq'=>2));
$categories->load();
if (count($categories) > 0):
foreach($categories as $category):
$catId = $category->getId();
$category = $objectManager->create('Magento\Catalog\Model\CategoryFactory')->create()->setStoreId(0)->load($catId);
$childrenCategories = $category->getChildrenCategories();
if(!$childrenCategories->count()) continue;
$CategoryName = $category->getName();
$category->setLandingPage(82);
$category->setDisplayMode("PAGE");
$category->save();
$checkpage = $category->getLandingPage();
$checkmode = $category->getDisplayMode();
echo "<pre>";
print_r($CategoryName);
echo "<pre>";
print_r($checkpage);
echo "<pre>";
print_r($checkmode);
endforeach;
else: echo "No Results";
endif;
?>
精彩评论