製品のリストの「デフォルト値の使用」に属性を設定する
-
16-10-2019 - |
質問
製品のリストとストアビューのリストには、画像を「デフォルト値を使用する」ように設定したいと思います。私はそれぞれの製品で個別にそれを行う方法を知っています:setData(astributeName、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($製品、$ 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);
}
これはそれでなければなりません。しかし、私が自信過剰で、これがうまくいかない場合は、最初にデータベースをバックアップしてください。
所属していません magento.stackexchange