Friday, 17 June 2011

Limit Number of Images Per Product In Magento

Some time we need to limit number of images for a product in our magento project we can do it
easily with some lines of code. Go to 'app\code\core\Mage\Adminhtml\controllers\Catalog\ProductController.php' and in saveAction find $data = $this->getRequest()->getPost(); under it put these line. and you are done.

$images = Mage::helper('core')->jsonDecode($data['product']['media_gallery']['images']);
       
        if($totalImages = count($images)) {

        $maxPhotoPerProduct = 3;

        if($totalImages > $maxPhotoPerProduct) {
            Mage::getSingleton('core/session')->addError($this->__('Max. number of images per product reached, extra images have been removed'));
        }

        $_allowedImages = array_slice($images, 0, $maxPhotoPerProduct);
        $_POST['product']['media_gallery']['images'] = Mage::helper('core')->jsonEncode($_allowedImages);
    }

1 comment:

  1. Not Works... the some tutorial: http://inchoo.net/ecommerce/magento/programming-magento/how-to-limit-the-number-of-allowed-images-per-single-product-in-magento/
    not works too. Can you help us?
    Thanks

    ReplyDelete