soipo
soipo

Reputation: 493

FIle upload in checkout cart Magento

I am trying to push an image to the checkout cart using $cart->addProduct($product, $request);

If I use custom options in a normal product this is how my request looks like.

  $_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"
}

As you might have noticed I am passing 2 images. Now I am trying to the same thing from my custom controller. I manage to add the product to the cart , but I cannot find the function that is responsible of saving the files to the order. Does anyone know how Magento handle this ? Thanks Soipo

Upvotes: 0

Views: 1853

Answers (1)

soipo
soipo

Reputation: 493

I have found this code on a blog ,

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

To this make sure that the array looks like this :

$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;

Upvotes: 1

Related Questions