有谁知道如何限制每个可以上传到的产品的图片数量 - Magento?说我只想让每个产品留下6张图片。
谢谢,
答案 0 :(得分:0)
public function saveAction()
{
/* START: Handle the max images per product */
$data = $this->getRequest()->getPost();
$images = Mage::helper('core')->jsonDecode($data['product']['media_gallery']['images']);
if($totalImages = count($images)) {
$maxPhotoPerProduct = 5;
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);
//Return the allowed images back to the $_POST
$_POST['product']['media_gallery']['images'] = Mage::helper('core')->jsonEncode($_allowedImages);
}
/* END: Handle the max images per product */
//Zend_Debug::dump(Mage::helper('core')->jsonDecode($_POST['product']['media_gallery']['images'])); exit;
//Run standard saveAction()
parent::saveAction();
}
请注意,上面的代码来自位于“app / code / local / Mycompany / Mymodule / controllers / Adminhtml / Catalog / ProductController.php”文件中的Mycompany_Mymodule_Adminhtml_Catalog_ProductController类。
含义我使用Mymodule config.xml文件中显示的语法覆盖了默认的“app / code / core / Mage / Adminhtml / controllers / Catalog / ProductController.php”控制器。
</config>
...
<admin>
<routers>
<adminhtml>
<args>
<modules>
<sintax before="Mage_Adminhtml">Mycompany_Mymodule_Adminhtml</sintax>
</modules>
</args>
</adminhtml>
</routers>
</admin>
...
</config>