5

使用我自己的控制器,我将产品添加到 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);

我的问题是:如何将某个文件附加到设计选项中?

该文件已经传输到服务器端(实际上是通过请求)。但是,如果需要,我可以伪造上传。但直到现在我还没有找到关于设置文件自定义选项的单一信息来源......

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.

4

1 回答 1

2

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',
);

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
    )

)
于 2012-12-15T19:05:24.313 回答