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:
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.