在结帐购物车Magento上传

时间:2013-10-02 07:03:20

标签: php magento file-upload checkout cart

我正在尝试使用$ cart-> addProduct($ product,$ request)将图片推送到结帐购物车;

如果我在普通产品中使用自定义选项,这就是我的请求的样子。

  $_FILES:
  array(2) {
  ["options_76_file"] => array(5) {
    ["name"] => string(14) "Conference.jpg"
    ["type"] => string(10) "image/jpeg"
    ["tmp_name"] => string(14) "/tmp/phpkKRgHA"
    ["error"] => int(0)
    ["size"] => int(938613)
  }
  ["options_80_file"] => array(5) {
    ["name"] => string(16) "capra-felice.jpg"
    ["type"] => string(10) "image/jpeg"
    ["tmp_name"] => string(14) "/tmp/php8SXzIk"
    ["error"] => int(0)
    ["size"] => int(93196)
  }
}

Request:
array(7) {
  ["uenc"] => string(76) "aHR0cDovL2Rldi5rd2lrd2ViLmNvbS5hdS9uc2ovYWNjZXNzc29yaWVzL3NpbmdsZXQuaHRtbA,,"
  ["product"] => string(2) "22"
  ["related_product"] => string(0) ""
  ["options_76_file_action"] => string(8) "save_new"
  ["options"] => array(1) {
    [77] => string(3) "251"
  }
  ["options_80_file_action"] => string(8) "save_new"
  ["qty"] => string(1) "1"
}

你可能已经注意到我正在传递2张图片。 现在我正在从我的自定义控制器尝试相同的事情。我设法将产品添加到购物车,但我找不到负责将文件保存到订单的功能。 有谁知道Magento如何处理这个? 谢谢 Soipo

1 个答案:

答案 0 :(得分:1)

我在博客上找到了这段代码,

// the path of the file, relative to Magento base directory.
// For example /media/image.jpg
$image = "YOURFILE.JPG";
// the ID of the product
$product_id = XXX;

$product = Mage::getModel('catalog/product')->load($product_id);

$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
'product' => $product_id,
'qty' => 1,
'options' => array(
12345 => array(
'quote_path' => $image,
'secret_key' => substr(md5(file_get_contents(Mage::getBaseDir() . $image)), 0, 20)),
)
);

$cart->addProduct($product, $params);
$cart->save();

Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

为此,请确保数组如下所示:

$title= $option['name'];
                $image = DS."media".DS."logos".DS.$title;
                $path = Mage::getBaseDir().$image;

                $imgSize = getimagesize($path);
                $size =  filesize($path);

                $array = array(
                'type' => "application/octet-stream",
                'title' => $title,
                'size' => $size ,
                'width' => $imgSize[0],
                'height' => $imgSize[1],
                'quote_path'=> $image, 
                'order_path'=> $image, 

                'secret_key' => substr(md5(file_get_contents($path)), 0, 20));

                $options[$key] = $array;