file - 在 Magento 中将产品添加到购物车时,如何设置类型为 "file"的自定义选项?

标签 file magento option cart

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

)

关于file - 在 Magento 中将产品添加到购物车时,如何设置类型为 "file"的自定义选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13801535/

相关文章:

scala - 使用Scalaz将选项列表转换为列表选项

magento - 观察者观察购物车中已删除的商品

scala - 如何使用类型级别的猫将 `Either[Error, Option[Either[Error, Account]]]` 转换为 `Either[Error, Option[Account]]`?

c - 逐行从文件读入数组

python - 使用 python 生成 pdf 文件

php - Magento - 自动重定向到配置的支付网关之一

javascript - Magento 1 - 通过布局更新 xml 代码将内联 javascript 添加到产品页面

string - 检查字符串是否为空或在Scala中不存在

java - 使用 Java 中的本地应用程序在远程计算机上打开 PDF 文件?

c# - 如何更改txt文件中的值并覆盖它