Hi,
I have to programmatically add a product to cart.
This product has a lot of custom options, one of them is a file and that file isn't uploaded by a form but is generated server side with mpdf lib. With the above code i can successfully add the product to the cart but always with the error "Some of the products below do not have all the required options."
$cart = Mage::getModel("checkout/cart"); $cart->init(); $params = array( "product" => $product_id, "super_attribute" => $attributes, "options" => $custom_options, "qty" => $qty, "options_".$option_pdf->getId()."_file_action" => "save_new" ); $request = new Varien_Object(); $request->setData($params); $cart->addProduct($product, $request); Mage::getSingleton('checkout/session')->setCartWasUpdated(true); $cart->save(); $url = Mage::helper('checkout/url')->getCartUrl(); $this->_redirectUrl($url);
As I said the file is generated with mpdf so, in a rude way, i tried to set $_FILES variable like this:
$_FILES['options_'.$option_pdf->getId().'_file'] = array( 'name' => $pdf_name, 'tmp_name' => '/tmp/' . $pdf_name, 'error' => 0, 'type' => "application/pdf", 'size' => filesize('/tmp/' . $pdf_name) );
with no luck.
This is the content of array $params:
array(5) { ["product"]=> int(28) ["super_attribute"]=> array(1) { [134]=> int(33) } ["options"]=> array(30) { [37]=> string(3) "257" [0]=> string(1) "0" [3]=> string(2) "21" [5]=> string(2) "37" [7]=> string(2) "53" [11]=> string(2) "85" [9]=> string(2) "69" [1]=> string(1) "5" [4]=> string(3) "228" [6]=> string(3) "230" [8]=> string(3) "232" [12]=> string(3) "236" [10]=> string(3) "234" [2]=> string(3) "226" [25]=> string(3) "197" [23]=> string(3) "181" [27]=> string(3) "213" [13]=> string(3) "101" [19]=> string(3) "149" [15]=> string(3) "117" [21]=> string(3) "165" [17]=> string(3) "133" [26]=> string(3) "250" [24]=> string(3) "248" [28]=> string(3) "252" [14]=> string(3) "238" [20]=> string(3) "244" [16]=> string(3) "240" [22]=> string(3) "246" [18]=> string(3) "242" } ["qty"]=> int(1) ["options_30_file_action"]=> string(8) "save_new" }
Could someone help me?
Best Regards
Try to debug your code with try and cache. Also check the log that any error are being displayed in that or not. If error there is any then try to solve that error.
Hi, thank you for you answer.
I've written an observer to intercept "checkout_cart_product_add_after" event to check request parameters and other info.
Logs are clean and there are no error messages.
The problem should be related to the fact that the request contains no $_FILES array and that I'm trying to set it on the controller action of my custom module.
I've tried this few lines
$_FILES['options_'.$option_pdf->getId().'_file'] = array( 'name' => $pdf['name'], 'tmp_name' => $pdf['tmp_name'], 'error' => 0, 'type' => "application/pdf", 'size' => filesize($pdf['tmp_name']) ); $uploader = new Varien_File_Uploader('options_'.$option_pdf->getId().'_file'); $uploader->setAllowedExtensions(array('pdf', 'PDF')); $uploader->setAllowRenameFiles(true); //if true, uploaded file's name will be changed, if file with the same name already exists directory. Necessary to avoid conflicts $uploader->setFilesDispersion(true); //To have a dispersion based on the original file name (as the file option does), we will have to do it manually $uploader->setAllowCreateFolders(true); //for creating the directory if not exists $extension = pathinfo(strtolower($pdf['name']), PATHINFO_EXTENSION); $fileName = Mage_Core_Model_File_Uploader::getCorrectFileName($pdf['name']); $dispersion = Mage_Core_Model_File_Uploader::getDispretionPath($fileName); // We get the dispersion manually here $fileHash = md5(file_get_contents($pdf['tmp_name'])); //here the secretkey $path = $option_pdf->getQuoteTargetDir() . $dispersion; $fullpath = Mage::getBaseDir('media') . '/custom_options/quote' . $path; $destName = $fileHash . '.' . $extension; $result = $uploader->save($fullpath, $destName);
Everything's ok until "save" method (last line of code). Save calls _move method from Varien_File_Uploader class which contains a move_uploaded_file that always return false. Permissions and path are correct but i think the function knows that $_FILES array doesn't come from a POST request.
Could you help me some way?
Please try to put your above code into the try and cache block, and print exception generated into the log file, so we can get idea what is the exact error.
Putting save in a try/catch block like this
try { $result = $uploader->save($fullpath,$destName); } catch (Mage_Core_Exception $e) { return $e->getMessage();
}
doesn't catch any exception.
First check that the Magento store files are in particular location or not?
And by default Magneto stores files in newly created directories with first 2 charcters of the file name and then stores file in it. But here more than that directories are created.
I've fixed a little mess I've done with paths but the problem remains.
I still have a move_uploaded_file that returns false.
Arguments are those:
string(11) "/tmp/ZSSXq1" string(94) "/var/www/vhosts/domain.tld/media/custom_options/quote/a/2/a2279f8f0e262962f68aa7c7c3814ce2.pdf" bool(false)
First string is existing generated file; second one is destination path of file, folders "a" and "2" are successfully created, but directory "2" is empty. Last "bool(false) is output of move_uploaded_file function.
I think there's no chance to set custom option this way.
Does anybody knows a different way to procede?
My problem is this just to add programmatically a file to a type file custom option.
I've searched on docs and google for days without success.
Everything I've tried to let magento think that the file was uploaded failed.
Is there a way to tell magento that the file already exist and that he just have to set the custom option related?
Any comment will be appreciated, thank you guys.
If directory is created sucessfully, then check if the permissions are set properly to that folder. You are getting the error for "move_uploaded_file". It means the path or the permissions are not properly given and the function can not access that file. So please check that it is ok or not.
Paths and permissions are ok. There is no error logged and no warning from move_uploaded_file.
It simply return "false".
According to php documentation: "If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE." and before "This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism)."
I think setting $_FILES array manually doesn't work.
So my question is: is there another way to programmatically set $_FILES array?
I've seen setPost() method in Mage_Core_Controller_Request_Http and tryed that:
$file_pdf['options_' . $option_pdf->getId() . '_file'] = array( 'name' => "filename.pdf", 'tmp_name' => "/tmp/jskurc", 'error' => 0, 'type' => "application/pdf", 'size' => filesize("/tmp/jskurc") ); $this->getRequest()->setPost('options_' . $option_pdf->getId() . '_file',$file_pdf['options_' . $option_pdf->getId() . '_file']);
without success.