set attribuisce al 'valore di default l'uso' per un elenco di prodotti
-
16-10-2019 - |
Domanda
Voglio impostare le immagini di 'valore di default l'uso' per un elenco di prodotti, e per un elenco di vista negozio. So come si fa individualmente per ogni prodotto: setData (attributeName, false), e così posso fare un cappio sopra la mia lista di prodotti. Problema:. È davvero troppo lento
$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();
}
}
}
Così ho cerco di usare Mage :: getSingleton ( 'Catalogo / product_action') -> updateAttributes ($ prodotti, $ attrArray, $ STORE_ID); invece, che dovrebbe fare la stessa cosa, ma nel corso di un elenco di prodotti. E 'effettivamente fare qualcosa: tutte le mie immagini sono ora impostati su 'non ci sono immagini', ma non a 'Usa il valore di default' come previsto
.$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);
}
Se qualcuno qui intorno avere un'idea, potrebbe davvero aiutarmi a risparmiare un po 'di tempo! Grazie.
Soluzione
In sostanza, l'impostazione di un valore di attributo per mezzo 'utilizzare i valori predefiniti' che si deve eliminare la riga nel database per quell'attributo, per il prodotto specifico, per un negozio id.
Qui è una soluzione semplice che lo fa. Richiede alterare il database direttamente e alcune persone dire che questo è un grande 'no-no', ma funziona.
$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);
}
Questo dovrebbe essere esso. Ma nel caso in cui io sono troppo sicuro di sé e questo non funziona, back-up del database prima.