2

I am using Magento 2.1.5, and I created 4 customer attribute. I need to set the condition like if any one of the fields is filled, customer session will start.

for one field I am getting the result, but I don't understand how to put this condition for 4 fields.

$customerCollection=$this->customerObj->getCollection()
    ->addAttributeToSelect("*")
    ->addAttributeToFilter("customer_attr1" ,$this->_customerSession->getCustomerId());

foreach ($customerCollection as $value) {
        array_push($arry,$value->getData("entity_id"));
    }

In this "customer_attr1" is one attribute. like same 4 other field is there "customer_attr2" "customer_attr3" "customer_attr4".

How to set a condition like if any one field is true, return true.

0

You can try like this:

$customerCollection=$this->customerObj->getCollection()
    ->addAttributeToSelect("*")
    ->addFieldToFilter(
    array("customer_attr1","customer_attr2","customer_attr3","customer_attr4"),
      array(
          array('eq' => $this->_customerSession->getCustomerId()),
          array('eq' => $this->_customerSession->getCustomerId()),
          array('eq' => $this->_customerSession->getCustomerId()),
          array('eq' => $this->_customerSession->getCustomerId())
      )
    );  

Or

$customerCollection=$this->customerObj->getCollection()
    ->addAttributeToSelect("*")
    ->addAttributeToFilter(   
    array(
          array('attribute' => 'customer_attr1', 'eq' => $this->_customerSession->getCustomerId()),
          array('attribute' => 'customer_attr2', 'eq' => $this->_customerSession->getCustomerId()),
          array('attribute' => 'customer_attr3', 'eq' => $this->_customerSession->getCustomerId()),
          array('attribute' => 'customer_attr4', 'eq' => $this->_customerSession->getCustomerId())
    )
);
  • hi, thank you for answer. after this i have to write "foreach ($customerCollection as $value) { array_push($arry,$value->getData("entity_id")); }" to return the value. is it will work? check my updated code. – sam May 15 at 13:01
  • try and check then let s see what will be the output. i believe it should still work – fmsthird May 15 at 13:26
  • its not working. returning nothing – sam May 15 at 13:50
  • try this foreach ($customerCollection as $value) { array_push($arry,$value->getData("entity_id")); print_r($array); } – fmsthird May 15 at 14:01
  • and check what you got – fmsthird May 15 at 14:02

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.