How to set a custom file type option when adding a product to the cart in Magento?

Using my own controller, I add the product to the Magento basket. It has 3 configurable options: 2 drop-down lists (color and size) and a file option (design). The code that adds the product to the cart,

// 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 question is: How can I attach a specific file to a design option?

The file has already been transferred to the server (actually through the request). I could, however, fake the download if necessary. But so far I have not found a single source of information about setting up custom file parameters ...

My best guess from the tour through Magento sources is that additional data is required for the purchase request (not in the parameters, but in the params object), for example: option_123_file => something, but I didn’t understand what exactly was needed . This part of Magento's sources is most likely not so straightforward. Thanks for any help.

+4
source share
1 answer

Ok, finally figured it out. The params array requires a special entry to specify a user option with the option_xx_file_action key, what to do with the file (save_new or save_old). It will look like this:

 $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 send request (via the form or there). The file name should be "options_xx_file". For example, in my case, my $ _FILES looked like this:

 Array ( [options_108_file] => Array ( [name] => i-like.png [type] => application/octet-stream [tmp_name] => C:\xampp\tmp\phpAAB8.tmp [error] => 0 [size] => 6369 ) ) 
+2
source

All Articles