4

I have a method in block which i want to check is there any data available for a logged in customer in a custom history table.

public function checkHistryAvailable(){
    if($this->session->isLoggedIn()) {

        $customer_id = $this->session->getCustomer()->getId();
        $Rewardspointshistry = $this->rewardpointshistryFactory->create();

        $collection = $Rewardspointshistry->getCollection();

        $collection->addFieldToFilter('customer_id',$customer_id);
        if(is_null($collection)){ return 2; }else{ return 8; }
    }
}

I used is_null to check but it return the same result for two types.

7

You can check using $collection->getSize() method of collection.

if($collection->getSize() < 1){ return 2; }else{ return 8; }
0
1

You can check using

count($collection->getData())

method of collection.

if(count($collection->getData()))
{
   return true; 
} else { 
   return false; 
}

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.