2

I have a custom module with two controllers, one for admin and one for frontend: http://dev.magento18.com/admin/example_myadmincontroller/preview/id/2/
http://dev.magento18.com/example/mycontroller/?id=1&storeId=1

mycontrollerAction(){
  $html = Mage::helper('mymodule/helpername')->myHelperFunction();
  ...
}

myadmincontrollerAction(){
  $html = Mage::helper('mymodule/helpername')->myHelperFunction();
  ...
}   

Both controllers call the same helper. Inside helper I do:

myHelperFunction(){
  $_product = Mage::getModel('catalog/product')->load(1);
  //if product doesn't have image it will display placeholder on frontend, 
  //in backend triggers error Image file was not found.
  $imgSrc = Mage::helper('catalog/image')->init($_product, 'image');
  ...
  return $html
}

If product doesn't have image, it will display placeholder on frontend. But in backend the same helper triggers error Image file was not found.. Has anybody had the same issue, how could I pass this error and make it display placeholder also in admin?

8

try this i hade the same issue and works

Go to admin > System > Configuration > Catalog > Product Image Placeholders

and upload some Image Place Holders…

7

I came out with this simple solution:

try{
  $imgSrc = Mage::helper('catalog/image')->init($_product, 'image');
}
catch(Exception $e) {
  $imgSrc = Mage::getDesign()->getSkinUrl('images/catalog/product/placeholder/image.jpg',array('_area'=>'frontend'));  
}
3
There are 2 ways to solve this.

   1) You can upload your product placeholder images at 
         System->Configuration->Catalog->Product Image Placeholders
   2) If you want to use magento's default placeholders then
      go to 
          skin/frontend/base/default/images Or
          skin/frontend/default/default/images/
and copy the **catalog** folder and paste that folder to 
          skin/adminhtml/base/default/images/

Update 1:

For your Custom module follow following steps 1) you can create folder for placeholder images at

  media/mymodule/placeholder/placeholderImage.jpg

2) re-Write model Class

 Mage_Catalog_Model_Product_Image

3) Update the method

  public function setBaseFile($file)

Line around at 304

$baseFile = $baseDir . $file;

        if ((!$file) || (!file_exists($baseFile))) {
            throw new Exception(Mage::helper('catalog')->__('Image file was not found.'));
        }

replace with

$baseFile = $baseDir . $file;

    if ((!$file) || (!file_exists($baseFile))) {
        $module = $this->getRequest()->getModuleName();
        if($module=='mymodule'){
          $baseFile = Mage::getBaseUrl('media').'mymodule/placeholder/placeholderImage.jpg';
        }else{
          throw new Exception(Mage::helper('catalog')->__('Image file was not found.'));
        }
    }
  • 1
    Tnx but none of the ways are ok for me. I am making a module which will be used by others. I can not say them to upload placeholder images. I also can not copy the catalog folders because if it was already copied it will make error during installation and leave the site maintenance mode. – JohnyFree Sep 8 '14 at 10:28
  • Please check updated answer may b that help you solve the problem.Please let me know if any Query. – Namita sheth Sep 8 '14 at 10:57
  • 1
    Why this rewrite, I think it would be better to simple use try catch block (see my answer I just submited). – JohnyFree Sep 8 '14 at 11:28
  • yes, that is better solution. thank you. can use in future. – Namita sheth Sep 8 '14 at 11:30
1

I was needed to developed a extension and get the base_image to be more performance the flow and send information. I got the same issue, BTW, @Namita very good explication about the issue and how to fix it.

So here is a solution without any re-write class, I just created a new function checking if I'm in admin or not.

public function getProductImagePath($product){

    $isAdmin        = Mage::app()->getStore()->isAdmin();
    $imageHelper    = Mage::helper('catalog/image');

    if($isAdmin){
        $currentArea    = Mage::getDesign()->getArea();
        Mage::getDesign()->setArea(Mage_Core_Model_Design_Package::DEFAULT_AREA);
    }

    $image = $imageHelper->init($product,'small_image')->resize(135)->__toString();

    if($isAdmin){
        Mage::getDesign()->setArea($currentArea);
    }

    return $image;
}

Hope this solution help.

Best, Alejandro.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.