Magento custom add product to cart

(happen to enterprise since 1.9 huh ?)

The following code bas issue :


$newProd = Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’, $sku);

$cart->addProduct($newProd);

The category inventory observe throws the following exception:


Mage::throwException(Mage::helper(‘cataloginventory’)->__(‘The stock item for Product is not valid.’));

The stock item object it gets from the product object is a basic Varien Object.

When I do the following, it behaves nicely:


$newProd = Mage::getModel(‘catalog/product’);

$newProd->load($newProd->getIdBySku($sku));

$cart->addProduct($newProd);

example from equipatron :

<pre class="c-sharp">$cart = $this->_getCart();

/*add product using ARI partstring return data*/

$postData = $this->getRequest()->getPost();

if (in_array('arisku', $postData)

AND in_array('ariqty', $postData)) {

$productHelper = Mage::helper('wagentopartstream/product');

$product = $productHelper->findProductByAriPartNumber($postData['arisku']);

if ($product) {

$params = array(

'qty' => $postData['ariqty'],

'product' => $product->getId(),

);

/*damn fix */

$pObj = Mage::getModel('catalog/product');

$pObj->load($pObj->getIdBySku($product->getSku()));

/*end of damn fix*/

/*add product to cart*/

$act = $cart->addProduct($pObj, $params);

/*update price from ARI*/

if (in_array('ariprice', $postData) && $postData['ariprice'] > 0) {

$quote = $act->getQuote();

/*@var $quote Mage_Sales_Model_Quote*/

foreach ($quote->getAllItems() as $item) {

if ($product->getId() == $item->getProductId()) {

$price = $postData['ariprice'];

/*@var $item Mage_Sales_Model_Quote_Item*/

$item->setCustomPrice($price);

$item->setOriginalCustomPrice($price);

$quote->save();

}

}

}

/*save cart*/

$cart->save();

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

}

else {

Mage::getSingleton('core/session')->addNotice($this->__('Part is not available'));

}

Leave a Comment