1

I have a custom column in my sales_flat_quote_item table named as device_id. I am working on to create a api where a device id is sent and the cart available items are displayed for guest users.

$deviceId=802;
$quote = Mage::getModel('sales/quote')->load($deviceId);
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
echo $pid = $item->getProductId();
}

which is not working.Is this not the proper way ?Please help thanks

  • load returns only one(first) value – Murtuza Zabuawala Feb 17 '17 at 9:16
  • 1
    try the 3rd way that is complete good answer for you – Murtuza Zabuawala Feb 17 '17 at 9:17
1

You can try the below code:

$deviceId=802;
$collection = Mage::getModel('sales/quote_item')->setStoreId(1)->getCollection()->addFieldToFilter('device_id', array('eq' => $deviceId));
foreach($collection as $item)
{
  var_dump($item->getData());
}
  • Fatal error: Call to a member function getStoreId() on a non-object in ... app\code\core\Mage\Sales\Model\Resource\Quote\Item\Collection.php on line 67 – Melvin Feb 15 '17 at 10:58
  • I have updated the code. Please try with updated code – Prasanta Hatui Feb 15 '17 at 11:21
  • getting the same error – Melvin Feb 15 '17 at 11:34
  • I think the quote is not exists for that device. Anyway adding device_id in sales_flat_quote_item table is not a good approach. Instead add device_id in sales_flat_quote table and then use $quote = Mage::getModel('sales/quote')->getCollection()->addFieldToFilter('device_id', array('eq' => $deviceId)); to load the quote. – Prasanta Hatui Feb 15 '17 at 11:47
  • If i do that how can i compare it with the product id – Melvin Feb 15 '17 at 11:49
1

you can do, try below code tested by me

$deviceId=802;  //if your value in ales_flat_quote table
$quote = Mage::getModel('sales/quote')->load($deviceId,'device_id');//here device_id is feild in database
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
  echo $pid = $item->getProductId();
}

according your comment

$deviceId=802;
$quote_colle = Mage::getModel('sales/quote')->getCollection();
foreach($quote_colle as $quote){

  $cartItems = $quote->getAllVisibleItems();
  foreach ($cartItems as $item) {
   if($item->getDeviceId()==802){
    echo $pid = $item->getProductId();
   }
  }
}

3rd way

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$deviceId=802;
$collection = Mage::getModel('sales/quote_item')->getCollection()->addFieldToFilter('device_id',  $deviceId);
foreach($collection as $item)
{
 echo $item->getId();
}
  • {"messages":{"error":[{"code":42,"message":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sales_flat_quote.device_id' in 'where clause', query was: SELECT sales_flat_quote.* FROM sales_flat_quote WHERE (sales_flat_quote.device_id=11111) AND (store_id IN ('0'))"}]}} – Melvin Feb 15 '17 at 9:16
  • ohh for this you have this column in sales_flat_quote table not in item table – Murtuza Zabuawala Feb 15 '17 at 9:19
  • what cn be done? – Melvin Feb 15 '17 at 9:25
  • You want all product which has device_id=802 in quote ?? – Murtuza Zabuawala Feb 15 '17 at 9:29
  • yes some thing like that so if the quote from same device id has same product id then i can update quantity – Melvin Feb 15 '17 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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