8

I got only one custom option, which is file type, I tried to add product to cart programmatically like this:

$logoSku = 'lg-brnd01';
$productLogo = Mage::getModel('catalog/product')->loadByAttribute('sku',$logoSku);
$logoOptions = $productLogo->getOptions();
$opts = Mage::getSingleton('catalog/product_option')->getProductOptionCollection($productLogo);
$i =0;
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $productLogo->getId(),
    'qty' => 1,
    'form_key' => Mage::getSingleton('core/session')->getFormKey(), 
);
foreach ($opts->getData() as $data) {
    $optionId = $data['option_id'];
    $image = $paths[0]['name'];
    $options =  array(
    $optionId => array(
        'type' => $paths[0]['type'],
        'title' => $image,
        'quote_path' => DS.'media'.DS.'uploads'.DS.'logo-branding'.DS.$quote_id.DS.$image,
        'order_path' =>  DS.'media'.DS.'uploads'.DS.'logo-branding'.DS.$quote_id.DS.$image,
        'fullpath' => $path.$image,
        'secret_key' => substr(md5(file_get_contents($path.$image)), 0, 20)),
    );
    $params['options_'.$optionId.'_file_action'] = 'save_new';
}
$params['options'] = $options;
print_r($params);
$request = new Varien_Object();
$request->setData($params);
try {
    $cart->addProduct($productLogo->getId(), $request);
    $cart->save();
    $i++;
} catch (Exception $e) {
    print_r($e->getMessage());
}

but I got an error like this:

Please specify the product's required option(s)

  • Can you please log the original exception in app/code/core/Mage/Catalog/Model/Product/Option/Type/File.php on line 183 and post it? – LDusan Jan 15 '18 at 12:26
0

You try to add to the card a configurable product and you did not fixe the option of this product : so may be my code will be helpful for you :

<?php

include 'app/Mage.php';

Mage::app();

// Need for start the session

Mage::getSingleton('core/session', array('name' => 'frontend'));

try {

    $product_id = '126'; // Replace id with your product id

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

    $cart = Mage::getModel('checkout/cart');

    $cart->init();

    $params = array(

        'product' => $product_id,

        'super_attribute' => array(

            525 => 100,
            //525 is the attribute id of size and 100 is the selected option value (small) of that attribute.

        ),

        'qty' => 2,

    );

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

    $cart->save();

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

    Mage::getSingleton('core/session')->addSuccess('Product added successfully');

    header('Location: ' . 'index.php/checkout/cart/');

} catch (Exception $e) {

    echo $e->getMessage();

}
| improve this answer | |

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.