0
require_once '../../app/Mage.php'; 
umask(0);  
Mage::app();

Mage::getSingleton('checkout/session');
Mage::getSingleton('customer/session')->loginById($customerID);

$params = array(
    'product' => $productIds,
    'qty'     => $p_qty,
    'price'   => $price
    );

$_product = Mage::getModel("catalog/product")->load($productIds);    

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

$cart->addProduct($_product, $params ); -->error

==>error on above line.

exception 'Mage_Core_Exception' with message 'Please specify product link(s).' in /app/Mage.php:594 
Stack trace: 
#0 /app/code/core/Mage/Checkout/Model/Cart.php(284): Mage::throwException('Please specify ...') 
#1 addcart.php(54): Mage_Checkout_Model_Cart->addProduct(Object(Mage_Catalog_Model_Product), 1) 
#2 {main}
6
  • You are trying to add a downloadable product to the cart ? Share the whole script, what is in $_product and what is in $params ?
    – Prateek
    Commented Dec 19, 2015 at 7:51
  • i just edited that question please check. Commented Dec 19, 2015 at 8:35
  • Are you trying to add one or many items?
    – SeStro
    Commented Dec 19, 2015 at 8:53
  • I just try only one item to add in magento 1.8 through php script. Commented Dec 19, 2015 at 8:54
  • Check what parameters default Magento use when dealing with the downloadable products. You'll need to replicate that. The method you are using is used for simple products without any custom options.
    – Prateek
    Commented Dec 19, 2015 at 9:06

1 Answer 1

0

Try this:

require_once '../../app/Mage.php'; 
umask(0);  
Mage::app();

Mage::getSingleton('checkout/session');
Mage::getSingleton('customer/session')->loginById($customerID);

$_product = Mage::getModel("catalog/product")->load($productIds);  

$params = array(
    'product' => $productIds,
    'qty'     => $p_qty,
    'price'   => $price
);

if ($_product->getTypeId() == 'downloadable' && $_product->getLinksPurchasedSeparately()) {
    $linksArray = array();
    $links = $_product->getTypeInstance(true)->getLinks($_product);
    foreach ($links as $link) {
        $linksArray[] = $link->getId();
    }
    $params['links'] = $linksArray;
}

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

$cart->addProduct($_product, $params );
3
  • spealing mistake here downnloadable change to downloadable and &_product to $_product .. thanks for save my days very helpful. Commented Dec 19, 2015 at 12:05
  • Book Type E-Book: 100/- Print Book: 200/- there are two link(e-book and print book) how could i identify is this e-book or print book in $link. Commented Dec 19, 2015 at 12:20
  • you could check link title: $link->getTitle();
    – SeStro
    Commented Dec 19, 2015 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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