0

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;

}
2
  • 1
    each times it satisfies the if condition??? Feb 20, 2017 at 6:09
  • I am able to update the qty
    – Melvin
    Feb 20, 2017 at 6:10

1 Answer 1

1

to find if particular product is in quote

if (! $quote->hasProductId($product_id)) {
    // Product is not in quote so add your logic
}
else
{
   $item = $quote->getItemByProduct($product_id);
   $qty = $item->getQty();
   $item->setQty($qty);
   $items->save();
}

find is collection empty

if ($collection->count()) {
// Collection is not empty
}
else
{
}

After Update your 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);
        if ($collection->count()) {
            foreach($collection as $item)
            {
                $quote = Mage::getModel('sales/quote')->load($item->getEntityId());
                $cartItems = $quote->getAllItems();
                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();
                    break;
                }
                else{
                    $item = $quote->getItemByProduct($product_id);
                   $qty = $item->getQty();
                   $item->setQty($qty);
                   $items->save();
                   break;
                }


            }
        }
        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;

}
15
  • so this is working but how can i know weather collection has loaded ?$collection = Mage::getModel('sales/quote')->getCollection()->addFieldToFilter('device_id', $deviceId);
    – Melvin
    Feb 20, 2017 at 6:43
  • 1
    what do you mean by collection has loaded ? if collection is empty it will not go through foreach @Melvin Feb 20, 2017 at 6:48
  • collection is getting from device id.So if deviceid is not present,then collectionwill be empty? how will i check that ?so i can directly add device id and add product in quote
    – Melvin
    Feb 20, 2017 at 6:51
  • @Melvin find updated answer Feb 20, 2017 at 6:59
  • 1
    @Melvin Cause may be you $collection contains multiple quote means device_id can be of more than one quote Feb 20, 2017 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.