Magento – add custom image field for custom options in magento

custom-optionsmagento-1.9product

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.

Best Answer

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.

Related Topic