我想在magento自定义选项中添加图片上传功能 我按照这个
https://magento.stackexchange.com/questions/33452/add-custom-image-field-for-custom-options-in-magento 但图像不是数据库中的dave
我正在尝试这个
/**
* 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();
}
请帮助