fxx*_*fxx 5 file magento cart option
使用我自己的控制器,我将产品添加到Magento购物车.它有3个自定义选项:2个下拉选项(颜色和大小)和文件选项(设计).将产品添加到购物车的代码是
// obtain the shopping cart
$cart = Mage::getSingleton('checkout/cart');
// load the product
$product = Mage::getModel('catalog/product')
    ->load($productId);
// do some magic to obtain the select ids for color and size ($selectedSize and $selectedColor)
// ...
// define the buy request params
$params = array(
    'product'       => $productId,
    'qty'           => $quantity,
    'options'       => array(
        $customOptionSize->getId()  => $selectedSize,
        $customOptionColor->getId() => $selectedColor,
        // set the file option, but how? 
    ),
);
// add this configuration to cart
$cart->addProduct($product, $paramObject);
$cart->save();
// set the cart as updated
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
我的问题是:如何将某个文件附加到设计选项中?
该文件已经传输到服务器端(实际上是通过请求).但是,如果需要,我可以伪造上传.但到目前为止,我还没有找到关于设置文件自定义选项的单一信息来源......
我通过Magento消息来源的最佳猜测是,购买请求需要一些额外的数据(不在选项中,但在params对象中),如:option_123_file =>某事,但究竟需要什么我没想到然而.Magento来源的这一部分相当,呃,不是那么直截了当.谢谢你的帮助.
好吧终于弄清楚了。params 数组需要特殊条目来告诉带有键“options_xx_file_action”的自定义选项如何处理文件(“save_new”或“save_old”)。这看起来像:
$params = array(
    'product'       => $productId,
    'qty'           => $quantity,
    'options'       => array(
        $customOptionSize->getId()  => $selectedSize,
        $customOptionColor->getId() => $selectedColor,
    ),
    'options_'.$customOptionDesign->getId().'_file_action'=>'save_new',
);
显然,您需要将文件添加到发布请求中(通过表单或类似方式)。文件名应为“options_xx_file”。例如,就我而言,我的 $_FILES 如下所示:
Array (
[options_108_file] => Array
    (
        [name] => i-like.png
        [type] => application/octet-stream
        [tmp_name] => C:\xampp\tmp\phpAAB8.tmp
        [error] => 0
        [size] => 6369
    )
)