2016-07-04 21 views
0

Ich möchte die Bilder jedes Produkts zählen und speichern/aktualisieren Sie den Wert im Attribut imagecount.Magento: Produktbilder zählen und Attribut aktualisieren

ini_set('display_errors', 'On'); 
error_reporting(E_ALL); 

require('app/Mage.php'); // this is assuming your script is located in the Magento root dir 
Mage::app(); // initiate the Magento engine 

$allProductIds = Mage::getModel('catalog/product')->getCollection()->getAllIds(); 

$products = Mage::getModel('catalog/product')->getCollection() 
->addAttributeToFilter('entity_id', array('in' => $allProductIds)); 

foreach ($products as $product) { 

    $product->getGalleryImages(); 
    $pimagecount = count($product); 
    echo count($pimagecount); 
    $product->setImagecount($pimagecount); 
    $product->getResource()->saveAttribute($product, 'imagecount'); 
} 

Ich habe versucht, die Werte mit dem obigen Code zu bekommen, aber die Ausgabe ist immer 1 für jedes Produkt.

Antwort

1

Try Code unten

ini_set('display_errors', 'On'); 
error_reporting(E_ALL); 

require('app/Mage.php'); // this is assuming your script is located in the Magento root dir 
Mage::app(); // initiate the Magento engine 

$allProductIds = Mage::getModel('catalog/product')->getCollection()->getAllIds(); 

$products = Mage::getModel('catalog/product')->getCollection() 
->addAttributeToFilter('entity_id', array('in' => $allProductIds)); 

foreach ($products as $product) { 
    $product = Mage::getModel("catalog/product")->load($product->getId()); 
    $gi = $product->getGalleryImages(); 
    $product->setImagecount(count($gi)); 
    $product->save(); 
} 
+0

hey danke für den Code. Ich bekomme immer noch 1 für alle Produkte ... – Rob

+0

Ich habe meinen Code aktualisiert, bitte überprüfen Sie noch einmal mit diesem Code. –

+0

Jetzt wird keine Nummer angezeigt. Wo ist Zählfunktion? Es ist unmöglich, die Anzahl der Bilder ohne es zu bekommen, denke ich. – Rob

0

Sie müssen Ihren Code wie folgt ändern:

ini_set('display_errors', 'On'); 
error_reporting(E_ALL); 

require('app/Mage.php'); // this is assuming your script is located in  the Magento root dir 
Mage::app(); // initiate the Magento engine 


$products = Mage::getModel('catalog/product')->getCollection(); 

foreach ($products as $product) { 
$product = Mage::getModel('catalog/product')->load($product->getId()); 
$images = $product->getData('media_gallery'); 
$pimagecount = count($images['images']); 
echo $pimagecount; 
} 
+0

hey danke für den Code. Ich bekomme immer noch 1 für alle Produkte ... – Rob