6

I am trying to add products to cart programmatically having custom options of type file. Basically, there is a separate interface for some products in the shop where customer can customize the image being ordered. It's working with some external API which saves the final image on server and returns the path on the basis of which I'm going to add that into the custom option of product.

What I am trying to do is add that image into the custom option of product. Tried several methods but no success. Found this question but no answer there.

This is what I have tried so far, but it keeps giving me error that required options is missing.(That image option is required)

$path = Mage::getBaseDir();
$product_id = 1;
$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'qty' => 1,
    'options_1_file_action' => array(
    1 => array(
        'quote_path' => $image,
        'secret_key' => substr(md5(file_get_contents($path . DS . $image)), 0, 20)),
        )
    );
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

Also found this piece somewhere, but it depends upon FILES variable, so can't use it as there is no post from front.

$product_id = 1;
$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'qty' => 1,
    'options_1_file_action' => 'save_new',
    'options_1_file' => array(
        'name' => $image,
        'type' => 'application/octet-stream',
        'tmp_name' => $image
    )
);
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
  • what is the value of the $image? – MeenakshiSundaram R Sep 17 '15 at 15:06
  • $image variable contains Magento's base directory relative path to the image file, it is what I am currently able to pass when adding product to the cart. – Prateek Sep 18 '15 at 6:25
  • it does not work on magento 1.9.2.1 please help. – Sazzad Nov 20 '15 at 17:55
  • @Sazzad, It doesn't work in any Magento version. I haven't been able to find the solution for this one yet, and the project seems to be on hold as well. I'll try to figure it out when I resume that. – Prateek Nov 21 '15 at 5:49
  • @Prateek any luck? – Haris Jun 7 '16 at 9:48
2

You can follow:

$cart = Mage::getSingleton('checkout/cart');
$cart->init();

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

$paramater = array('product' => '1112',
                    'qty' => '12',
                    'form_key' => Mage::getSingleton('core/session')->getFormKey(),
                    'options' => array('option_id'=>'sub_option_id')
            );       

$request = new Varien_Object();
$request->setData($paramater);
$cart->addProduct($product, $request);
$cart->save();

If custom option is not required for this product that time this code will use. Otherwise, you change the base file of magento

0

Inside of Mage_Catalog_Model_Product_Option_Type_File (app/code/core/Mage/Catalog/Model/Product/Option/Type/File.php this is where you want to look at how your option parameters are being processed I believe) validateUserValue tries to verify the file has been uploaded, however, it will ignore that if you setSkipCheckRequiredOption() on the $product.

    // Process new uploaded file
    try {
        $this->_validateUploadedFile();
    } catch (Exception $e) {
        if ($this->getSkipCheckRequiredOption()) {
            $this->setUserValue(null);
            return $this;
        } else {
            Mage::throwException($e->getMessage());
        }
    }

So modifying the second example from your question

$product_id = 1;
$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'qty' => 1,
    'options_1_file_action' => 'save_new',
    'options_1_file' => array(
        'name' => $image,
        'type' => 'application/octet-stream',
        'tmp_name' => $image
    )
);

//--------------------------------------------------------
// Bypass uploaded image validation on the new file option
//--------------------------------------------------------
$product->setSkipCheckRequiredOption(true);

$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

That should get you past the validation against the $_FILES array, but I'm not sure it will 100% solve your problem.

  • Any justification for the downvote? – quickshiftin Aug 20 '16 at 2:10
0

Suppose the custom upload is as follows,

<form action="<?php echo Mage::getUrl('custom/index/uploadImage'); ?>" method="post" enctype="multipart/form-data">
<input accept="image/x-png,image/gif,image/jpeg" type="file" name="options_5_file" id="file-upload-po" class="required-entry"/>
<input type="submit" value="Upload Image" name="submit" id="addimage">
</form>

Then by using following way you can add to cart the product with the custom option (file) ,

public function uploadImageAction(){        
    $_product = Mage::getModel('catalog/product')->load(8);


    $cart = Mage::getSingleton('checkout/cart'); 
    $cart->init();

    $_product->setSkipCheckRequiredOption(true);

    $cart->addProduct($_product ,
            array(  'product_id' => 8,
                    'qty' => 1,
                    'options_5_file_action' => 'save_new'

                 ));

    $cart->save();

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

    $quote_itemID = Mage::getSingleton('checkout/session')
               ->getQuote()
               ->getItemsCollection()
               ->getLastItem()
                ;
    $this->_redirect('checkout/cart/configure/id/'.$quote_itemID->getId());}

Note that "options_5_file" is actual custom option field of type image in product.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.