4

I am working on magento 1.9 version.I want to add a custom image field in drop down of custom options. I added a text field using following guide

http://magento.ikantam.com/qa/how-add-custom-attributes-custom-options

which is working fine.but when I add a file field then it shows in admin but not save image field value in database.

enter image description here

please help me to solve this.

4
  • You've flushed cache after changes?
    – osrecio
    Aug 26, 2014 at 20:07
  • any errors in the logs?
    – Amasty
    Aug 27, 2014 at 12:02
  • are you using any third party extension, it looks like?
    – user9467
    Aug 28, 2014 at 12:09
  • Hi guys where you want to show the images at front end .First you clear that.
    – Sourav
    Aug 30, 2014 at 6:14

2 Answers 2

2

This is because there is no code that will do this in the Magento core. The controller that does the product save action is in the file app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php. Here you will find the code snippet.

/**
 * Initialize product options
 */
if (isset($productData['options']) && !$product->getOptionsReadonly()) {
    $product->setProductOptions($productData['options']);
}

$product->setCanSaveCustomOptions(
    (bool)$this->getRequest()->getPost('affect_product_custom_options')
    && !$product->getOptionsReadonly()
);

All this is doing is setting the options on the product option and doing no image upload.

Then when you look at app/code/core/Mage/Catalog/Model/Product.php there is the function _beforeSave this function does the actual processing on options before saving them. What it does is loops through all the options and then adds them.

$this->getOptionInstance()->addOption($option);

So there is no code for uploading images or even saving them against the option itself. You will need to extend the options table to add your custom item, or use a custom model for storing option images.

Then what I would suggest is to listen to the admin event catalog_product_prepare_save. This is fired after the options are set on the product. It has the product object and the request. You can use this event to perform the actual image upload and then update the product object if you need to.

2
  • I used that event but when I print the data my image field not showing there. Aug 27, 2014 at 13:22
  • is it in the $_FILES array? Aug 27, 2014 at 13:23
1

David Manners' answer pointed in the right direction by using the catalog_product_prepare_save event. The function itself could look something like this:

/**
 * This observer is fired after a product is prepared saving an is used to store file uploads of custom options.
 * @param $observer
 */
public function catalogProductPrepareSave($observer)
{
    if(!isset($_FILES['product']) || !isset($_FILES['product']['name']) || !isset($_FILES['product']['name']['options']))
    {
        // No files are uploaded, return ...
        return;
    }

    $io = new Varien_Io_File();
    $dir = Mage::getBaseDir('media') . DS . 'options';
    $io->open(array('path' => $dir));

    /** @var Mage_Catalog_Model_Product $product */
    $product = $observer->getProduct();

    // Iterate through all file uploads:
    foreach($_FILES['product']['name']['options'] as $optionId => $values)
    {
        // Check for files:
        foreach($values['values'] as $valueId => $arr)
        {
            // Check for error code '0' (which is a successful file upload)
            if($_FILES['product']['error']['options'][$optionId]['values'][$valueId]['image'] == 0)
            {
                // Successful file upload:
                $newFileName = $arr['image'];
                $tmpFileName = $_FILES['product']['tmp_name']['options'][$optionId]['values'][$valueId]['image'];

                // Move uploaded file:
                $io->mv($tmpFileName, $dir . DS . $newFileName);

                // Store product option value of this product:
                $option = $product->getOptionById($optionId);
                $value = $option->getValueById($valueId);
                $value->setImage($newFileName);
                $value->save();
            }
        }
    }

    $io->close();

}

The template in your options screen (select.phtml) could look like this:

<td style="white-space: nowrap;"><img src="" data-image="{{image}}" width="16" height="16" />&nbsp;<input type="file" class="input-text" name="product[options][{{id}}][values][{{select_id}}][image]"></td>

And add these lines at the end of the add()-method in the JavaScript in select.phtml to make sure your thumbnails are shown in the backend:

// setup images:
$A($$('img[data-image]')).each(function(img){
    if(img && img.getAttribute)
    {
        if(img.getAttribute('data-image') == '') {
            img.style.display = 'none';
        } else {
            img.src = '/media/options/' + img.getAttribute('data-image');
        }
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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