4

I am using the code below to get the orders when filtering using email address of the logged in user.

$order = Mage::getResourceModel('sales/order_collection')
        ->addFieldToSelect('*')
        ->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId())
        ->setOrder('created_at', 'desc');
return $order;

But if the user has not placed an order at all then $order should be NULL value or empty value right? but I am not getting an empty value which I found out after using print_r($order);.

I need empty or null value so that I can display a notification to the customer to tell him that he has not ordered anything. if there is a way to get NULL value then please explain.

  • Where do you call this? – zhartaunik May 11 '15 at 7:36
  • Inside a method in the Block file. – Ravi Chan May 11 '15 at 8:26
1

As per object OOPS concept,$order is object,so you cannot get null value from object.

In this case you can use getSize() function because it give no of record of a collection.It is give Varien collection object and it give value as false whenever a collection does not have records.

if(!$order->getSize()){
// no order available for  customer
}

How to use getSize( see at Why getSize() function is not working when apply filter

1

Do not try to reinvent the wheel. If you don't know how to do - look how it resolved by magento.

If you want to check if order exist - firstly look how does magento determines that. As example:

Mage_Adminhtml_Sales_OrderController::_initOrder()

    if (!$order->getId()) {
        $this->_getSession()->addError($this->__('This order no longer exists.'));

If I want to check any object existing - I always check it's ID and firstly check if this object exists:

if ($order && $order->getId()) {
    ...

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.