Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Magento 如何更新我的产品?_Magento_Sql Update - Fatal编程技术网

Magento 如何更新我的产品?

Magento 如何更新我的产品?,magento,sql-update,Magento,Sql Update,我想从我的脚本中更新我所有产品的元标题、元描述和元关键字 元标题=产品的名称 元描述=产品的简短描述 meta关键字是我从产品类别中输入的meta关键字 我可以循环并获得我想要进行更新的信息,除了描述中的元关键字,以及如何在循环内进行更新 谢谢 <?php require_once 'app/Mage.php'; umask(0); set_time_limit(0); // ignore php timeout ignore_user_abort(t

我想从我的脚本中更新我所有产品的元标题、元描述和元关键字

元标题=产品的名称

元描述=产品的简短描述

meta关键字是我从产品类别中输入的meta关键字

我可以循环并获得我想要进行更新的信息,除了描述中的元关键字,以及如何在循环内进行更新

谢谢

<?php require_once 'app/Mage.php';
umask(0);

set_time_limit(0);                   // ignore php timeout
ignore_user_abort(true);             // keep on going even if user pulls the plug*
while(ob_get_level())ob_end_clean(); // remove output buffers
ob_implicit_flush(true);             // output stuff directly
//error_reporting(E_ALL);

/* not Mage::run(); */
Mage::app('default');

// get product collection (All product)
$storeId = Mage::app()->getStore()->getId();
$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG);

$_productCollection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('visibility', $visibility)
    ->setStoreId($storeId)
    ->addStoreFilter($storeId);

foreach ($_productCollection as $pro) { // loop each product

   $meta_title = $pro['meta_title'];
   $meta_description = $pro['meta_description'];

   $name = $pro['name'];
   $short_description = $pro['short_description'];

    // I want to update the meta_title, meta_description, meta_keywords

    // meta_description = short_description
    // meta_title = name
    // meta_keywords = meta keywords from the category of the product

}

?>

您可以使用标准设置器轻松设置关键字和描述

在foreach循环内部,只需使用:

foreach ($_productCollection as $pro):

   $product = Mage::getModel('catalog/product')->load($pro->getId());   
   $product->setMetaTitle('Product Title')
   ->setMetaKeyword('Product keywords') 
   ->setMetaDescription('Product Description')
   ->save();

endforeach;
试试这个

foreach ($_productCollection as $pro):
    $product = Mage::getModel('catalog/product')->load($pro->getId());
    $cats = $product->getCategoryIds();
    $meta_keys = '';
    foreach ($cats as $category_id) {
    $_cat = Mage::getModel('catalog/category')->load($category_id) ;
    if($_cat->getMetaKeywords())
    $meta_keys .= $_cat->getMetaKeywords().', ';
    }
       $product->setMetaTitle($pro->getName())
       ->setMetaKeyword($meta_keys) 
       ->setMetaDescription($pro->getShortDescription())
       ->save();

    endforeach;

您可以直接更改$pro实例的属性,例如,使用标准设置器,如$pro->setMetaTitle$meta_title;等等。要保存更改,只需调用$pro->save;。