如何在可配置产品中将可配置元标题更新为简单产品的元标题

时间:2014-11-18 18:16:29

标签: php magento

编写外部脚本文件以加载与可配置产品相​​关的简单产品sku

请查看以下脚本以更新简单的产品元标题

/* Configurable product collection in $_product */

$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect(array('sku', 'name', 'meta_title'))->addFilterByRequiredOptions();

/* Making array of simple products from configurable product maping */
foreach($simple_collection as $simple_product) {
    $simpleProductList[$simple_product->getId()] = $simple_product->getSku();
}

/* updating each simple product meta title */
foreach($simpleProductList as $key => $skuValue) {
    $updPrd = Mage::getModel('catalog/product')->load($key);
    $updPrd->setMetaTitle("[configurableProductMetaTitle]");
    $updPrd->save();
}

每个可配置产品都有近400到500个简单产品。

执行脚本时,更新时间过长。

请建议如何优化代码以在最短时间内执行脚本

1 个答案:

答案 0 :(得分:2)

尝试使用以下内容:

$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$simpleCollection = $conf->getUsedProductCollection()->addAttributeToSelect(array('sku', 'name', 'meta_title'))->addFilterByRequiredOptions();

$simpleIds = $simpleCollection->getAllIds();
$metaTitle = $conf->getProduct()->getMetaTitle();
$storeId = Mage::app()->getStore()->getStoreId(); // or define whatever store id you want

Mage::getSingleton('catalog/product_action')->updateAttributes($simpleIds, array('meta_title' => $metaTitle), $storeId);

密钥方法位于 app / code / core / Mage / Catalog / Model / Product / Action.php

public function updateAttributes($productIds, $attrData, $storeId) {
    ...
}

$productIds // Array of your product ids you want to edit
$attrData // Array of data to be updated; array($attributeCode => $value)
$storeId // Id of store where you want changes to be applied

希望这会降低执行时间。

干杯!