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


how to update configurable meta title to simple product's meta title in configurable products

编写的外部脚本文件,用于加载与可配置产品相关的简单产品 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 个简单产品。

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

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

尝试使用这样的东西:

$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);

密钥方法位于应用程序/代码/核心/法师/目录/模型/产品/操作下.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

希望这会给你更少的执行时间。

干杯!