我想将图像设置为产品列表和商店视图列表的“使用默认值”。我知道如何为每个产品单独执行此操作:setData(attributeName,false),这样我就可以对我的产品列表进行循环。问题:实在是太慢了。

$attrArray=array('thumbnail','small_image','image');
$products = array(170,171,172);
$stores = array(17,18,19);
foreach ($stores as $store_id) {
    foreach ($products as $product_id) {
        foreach ($attrArray as $attr) { 
            $product = Mage::getModel('catalog/product')
            ->load($product_id)->setStoreId($store_id)
            ->setData($attr, false)
            ->save();
        }
    }
}

所以我尝试使用 Mage::getSingleton('catalog/product_action')->updateAttributes($products, $attrArray, $store_id);相反,它应该做同样的事情,但是是在产品列表上。它实际上做了一些事情:我的所有图像现在都设置为“无图像”,但没有按预期设置为“使用默认值”。

$attrArray = array('thumbnail'=>false,'small_image'=>false,'image'=>false);
$products = array(170,171,172);
$stores = array(17,18,19);
foreach ($stores as $store_id) {
    Mage::getSingleton('catalog/product_action')
    ->updateAttributes($products, $attrArray, $store_id);
}

如果这里有人有一个想法,它确实可以帮助我节省一些时间!谢谢。

有帮助吗?

解决方案

基本上,将属性值设置为“使用默认值”意味着您必须删除数据库中该属性、特定产品和商店 ID 的行。
这是一个简单的解决方案,可以做到这一点。它需要直接更改数据库,有些人会说这是一个很大的“禁忌”,但它确实有效。

$attrArray=array('thumbnail','small_image','image');
$products = array(170,171,172);
$stores = array(17,18,19);
$productsAsString = implode(',', $products);
$storesAsString = implode(',', $stores);
//get access to the resource
$resource = Mage::getSingleton('core/resource');
//get access to the db write connection
$connection = $resource->getConnection('core_write');
//model for retrieving attributes
$eavConfig = Mage::getModel('eav/config');
$tables = array();
//get the association between attribute ids and the tables where their values are stored
//group them by table name so you will run a single query for each table
foreach ($attrArray as $attributeCode){
    $attribute = $eavConfig->getAttribute('catalog_product', $attributeCode);
    if ($attribute){
        $tableName = $resource->getTableName('catalog/product') . '_' . $attribute->getBackendType();
        $tables[$tableName][] = $attribute->getId();
    }
}
//for each table delete the attribute values in the specified store for the specified products
foreach ($tables as $tableName => $attributeIds){
    $attributeIdsAsString = implode(',', $attributeIds);
    $q = "DELETE FROM {$tableName}
                WHERE
                    attribute_id IN ({$attributeIdsAsString}) AND
                    entity_id IN ({$productsAsString}) AND
                    store_id IN ({$storesAsString})";
    $connection->query($q);
}

应该是这样。但如果我过于自信而这不起作用,请先备份您的数据库。

许可以下: CC-BY-SA归因
scroll top