如何设置“文件"类型的自定义选项?将产品添加到Magento的购物车中时? [英] How to set a custom option of type "file" when adding a product to the cart in Magento?

查看:60
本文介绍了如何设置“文件"类型的自定义选项?将产品添加到Magento的购物车中时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用我自己的控制器,将产品添加到Magento购物车中.它具有3个自定义选项:2个下拉选项(颜色和大小)和一个文件选项(设计).将产品添加到购物车的代码是

Using my own controller, I'm adding a product to the Magento cart. It has 3 custom options: 2 dropdown options (color and size) and a file option (design). The code adding the product to the cart is

// 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);

我的问题是:如何将特定文件附加到设计选项?

My question is: How do I attach a certain file to the design option?

该文件已经传输到服务器端(实际上是通过请求).但是,如果需要的话,我可以伪造上载.但是到目前为止,我还没有找到设置文件自定义选项的唯一信息来源.

The file has already been transferred to the server side (actually via the request). I could, however, fake uploading if this would be required. But until now I have not found a single source of information on setting file custom options...

从Magento来源进行的游览中,我最好的猜测是,购买请求需要一些其他数据(不在选项中,而是在params对象中),例如:option_123_file =>某些东西,但是我确实需要做些什么还不知道. Magento资料的这一部分相当,嗯,不是那么直接.感谢您的帮助.

My best guess from a tour through the Magento sources, is that the buy request needs some additional data (not in the options, but in the params object), like: option_123_file => something, but what exactly is needed I did not figure out yet. This part of the Magento sources is rather, uhh, not so straight forward. Thanks for any help.

推荐答案

好吧,终于知道了. params数组需要特殊的输入,以使用键"options_xx_file_action"告诉自定义选项如何处理文件("save_new"或"save_old").看起来像这样:

Ok finally figured this out. The params array needs special entry to tell the custom option with the key "options_xx_file_action" what to do with a file ('save_new' or 'save_old'). This would look like:

$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看起来像:

Obviously, you will need to add the file to the post request (via form or therelike). The name of the file should be "options_xx_file". For example, in my case my $_FILES looked like:

Array (
[options_108_file] => Array
    (
        [name] => i-like.png
        [type] => application/octet-stream
        [tmp_name] => C:\xampp\tmp\phpAAB8.tmp
        [error] => 0
        [size] => 6369
    )

)

这篇关于如何设置“文件"类型的自定义选项?将产品添加到Magento的购物车中时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆