0

Hi guys I have a small code snippet and am trying to add a product to basket in Magento via PHP.

Whatever I try (I've searched for a lot of snippets) I just get "The product could not be found." error. I have dialled my code back to a very short and simple snippet to try and get this working.

Can anyone spot what might be causing this?

require_once 'app/Mage.php';
umask(0);
Mage::init('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));

    $cart = Mage::getSingleton('checkout/cart');
    $sku="prodsku";
    $qty=1;
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
    var_dump($product);
    $cart->addProduct($product, $qty);
    $cart->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

I am using a var_dump on the product to make sure it is finding it and if I remove the line:

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

It then dumps the product information and it all looks fine.

SOLVED: This single product was not physically ADDED to the website under Product > Websites, I also have to change to load by ID rather than SKU as it was throwing up stock errors.

0

You can try bellow Code:

Mage::init('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));  
// Get customer session
$session = Mage::getSingleton('customer/session'); 
$cart = Mage::getSingleton('checkout/cart'); 
$cart->init();
$productid=1;
$qty=1;
// Add a product with custom options
$productInstance = Mage::getModel('catalog/product')->load($productid);
$param = array(
    'product' => $productInstance->getId(),
    'qty' => $qty,
);
$request = new Varien_Object();
$request->setData($param);
$cart->addProduct($productInstance, $request);

// update session
$session->setCartWasUpdated(true);

// save the cart
$cart->save();
5
  • Same error unfortunately. – KiwisTasteGood Feb 17 '17 at 12:22
  • check if the product is_in_stock? – Prasanta Hatui Feb 17 '17 at 12:25
  • ["is_in_stock"]=> NULL - I have manage stock set to no – KiwisTasteGood Feb 17 '17 at 12:29
  • what is quantity? – Prasanta Hatui Feb 17 '17 at 12:33
  • 1
    It's probably not set to the Website, for anyone digging for an answer to this years later... – seanbreeden Apr 5 '19 at 23:07
0

use below code to add product to cart

   try
{

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

    $qty=1;

    $productId='10'; //product id

    $product = Mage::getModel('catalog/product')->load($productId);
    $formKey = Mage::getSingleton('core/session')->getFormKey();

    if($product->getSku()!='')
    {
        $paramater = array(
            'product' => $productId,
            'qty' => $qty,
            'form_key' => $formKey
        );

        $cart->addProduct($product, $paramater);
        $cart->save();
    }
    else
    {
        Mage::log('product not vailable');  
    }
    $cart->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
}catch (Exception $e)
{
     Mage::log($e->getMessage());
}
4
  • Same error as before. – KiwisTasteGood Feb 17 '17 at 12:23
  • i edit my answer you check – Jigs Parmar Feb 17 '17 at 12:28
  • No error but doesn't add to cart either – KiwisTasteGood Feb 17 '17 at 12:31
  • any log print in log file?? – Jigs Parmar Feb 17 '17 at 12:35

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.