Magento:计算产品图像并更新属性

Magento: count product images and update attribute

我想统计每个产品的图像和save/update属性imagecount中的值。

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');
}

我尝试使用上面的代码获取值,但每个产品的输出始终为 1。

试试下面的代码

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();
}

您需要像下面这样更改您的代码:

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;
}