I have the following function inside my model.This checks for the device id(custom column in sales quote) in database and if found and the qoute item has same product id then just updates the qty or else create a new quote.When I use this,It simply is not going to else part.Here is my code.
public function _create(array $data)
{
$postParams = $this->getRequest()->getParams();
$product_id=$postParams['product_id'];
$qty=$postParams['qty'];
$deviceId=$postParams['device_id'];
$collection = Mage::getModel('sales/quote')->getCollection()->addFieldToFilter('device_id', $deviceId);
foreach($collection as $item)
{
$quote = Mage::getModel('sales/quote')->load($item->getEntityId());
$cartItems = $quote->getAllItems();
foreach ($cartItems as $items) {
$productId = $items->getProductId();
$qtys = $items->getQty();
if($productId==$product_id){
$qtys+=$qty;
$items->setQty($qtys);
$ks=$items->getQty();
$items->save();
}
else {
$productModel = 'hello';
}
}
}
echo json_encode(array('success' => $productId,'quoteObj' => $productModel)); die;
}
Please help.Thanks
Updated : This code now updates the quantity as well as create a new quote if i pass same product id and device id .If same pid and device id is passed ith should only update not create another one.
public function _create(array $data)
{
$postParams = $this->getRequest()->getParams();
$product_id=$postParams['product_id'];
$qty=$postParams['qty'];
$deviceId=$postParams['device_id'];
$collection = Mage::getModel('sales/quote')->getCollection()->addFieldToFilter('device_id', $deviceId);
if ($collection->count()) {
foreach($collection as $item)
{
$quote = Mage::getModel('sales/quote')->load($item->getEntityId());
$cartItems = $quote->getAllItems();
foreach ($cartItems as $items) {
$productId = $items->getProductId();
$qtys = $items->getQty();
if (! $quote->hasProductId($product_id)) {
$productModel = Mage::getSingleton('catalog/product');
$productObj = $productModel->load($product_id);
if (!$quoteObj) {
// echo 'if'; die;
$quoteObj = new Mage_Sales_Model_Quote();
}
$store_id = Mage::app()->getStore()->getId();
$storeObj = $quoteObj->getStore()->load($store_id);
$quoteObj->setStore($storeObj);
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);
$quoteItem->setQuote($quoteObj);
$quoteItem->setQty($qty);
$quoteItem->setStoreId($store_id);
$quoteObj->addItem($quoteItem);
$quoteObj->setStoreId($store_id);
$quoteObj->collectTotals();
$quoteObj->setCustomerId(null);
$quoteObj->setDeviceId($deviceId);
$quoteObj->save();
}
else{
$qtys+=$qty;
$items->setQty($qtys);
$ks=$items->getQty();
$items->save();
}
}
}
}
else{
$productModel = Mage::getSingleton('catalog/product');
$productObj = $productModel->load($product_id);
if (!$quoteObj) {
// echo 'if'; die;
$quoteObj = new Mage_Sales_Model_Quote();
}
$store_id = Mage::app()->getStore()->getId();
$storeObj = $quoteObj->getStore()->load($store_id);
$quoteObj->setStore($storeObj);
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);
$quoteItem->setQuote($quoteObj);
$quoteItem->setQty($qty);
$quoteItem->setStoreId($store_id);
$quoteObj->addItem($quoteItem);
$quoteObj->setStoreId($store_id);
$quoteObj->collectTotals();
$quoteObj->setCustomerId(null);
$quoteObj->setDeviceId($deviceId);
$quoteObj->save();
}
echo json_encode(array('success' => $ds,'quoteObj' => $ks,'quoteObj' => $dss)); die;
}