{"id":615,"date":"2012-12-15T11:46:11","date_gmt":"2012-12-15T11:46:11","guid":{"rendered":"http:\/\/atheotsky.loc\/?p=9"},"modified":"2012-12-15T11:46:11","modified_gmt":"2012-12-15T11:46:11","slug":"magento-custom-add-product-to-cart","status":"publish","type":"post","link":"http:\/\/ndthanh.com\/magento-custom-add-product-to-cart\/","title":{"rendered":"Magento custom add product to cart"},"content":{"rendered":"

(happen to enterprise since 1.9 huh ?)<\/p>\n

The following code bas issue :<\/p>\n

\n\n$newProd = Mage::getModel(\u2018catalog\/product\u2019)->loadByAttribute(\u2018sku\u2019, $sku);\n\n$cart->addProduct($newProd);\n\n<\/pre>\n

The category inventory observe throws the following exception:<\/p>\n

\n\nMage::throwException(Mage::helper(\u2018cataloginventory\u2019)->__(\u2018The stock item for Product is not valid.\u2019));\n\n<\/pre>\n

The stock item object it gets from the product object is a basic Varien Object.
\n<\/p>\n

When I do the following, it behaves nicely:<\/p>\n

\n\n$newProd = Mage::getModel(\u2018catalog\/product\u2019);\n\n$newProd->load($newProd->getIdBySku($sku));\n\n$cart->addProduct($newProd);\n\n<\/pre>\n

example from equipatron :<\/p>\n

\n<pre class="c-sharp">$cart = $this->_getCart();\n\n\/*add product using ARI partstring return data*\/\n\n$postData = $this->getRequest()->getPost();\n\nif (in_array('arisku', $postData)\n\nAND in_array('ariqty', $postData)) {\n\n$productHelper = Mage::helper('wagentopartstream\/product');\n\n$product = $productHelper->findProductByAriPartNumber($postData['arisku']);\n\nif ($product) {\n\n$params = array(\n\n'qty' => $postData['ariqty'],\n\n'product' => $product->getId(),\n\n);\n\n\/*damn fix *\/\n\n$pObj = Mage::getModel('catalog\/product');\n\n$pObj->load($pObj->getIdBySku($product->getSku()));\n\n\/*end of damn fix*\/\n\n\/*add product to cart*\/\n\n$act = $cart->addProduct($pObj, $params);\n\n\/*update price from ARI*\/\n\nif (in_array('ariprice', $postData) && $postData['ariprice'] > 0) {\n\n$quote = $act->getQuote();\n\n\/*@var $quote Mage_Sales_Model_Quote*\/\n\nforeach ($quote->getAllItems() as $item) {\n\nif ($product->getId() == $item->getProductId()) {\n\n$price = $postData['ariprice'];\n\n\/*@var $item Mage_Sales_Model_Quote_Item*\/\n\n$item->setCustomPrice($price);\n\n$item->setOriginalCustomPrice($price);\n\n$quote->save();\n\n}\n\n}\n\n}\n\n\/*save cart*\/\n\n$cart->save();\n\nMage::getSingleton('checkout\/session')->setCartWasUpdated(true);\n\n}\n\nelse {\n\nMage::getSingleton('core\/session')->addNotice($this->__('Part is not available'));\n\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

Programmatically add product to cart, and fix some issues in EE version with these codes.<\/p>\n","protected":false},"author":1,"featured_media":78,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,45],"tags":[],"aioseo_notices":[],"views":7,"_links":{"self":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/615"}],"collection":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/comments?post=615"}],"version-history":[{"count":0,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/615\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/"}],"wp:attachment":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/media?parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/categories?post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/tags?post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}