0

We have a custom Customer Address Attribute that contains email of an existing user in the system. If this field is populated for a customer’s address, then this address appears on the list of addresses for the customer (who’s email address is assigned). These addresses appear on product list and detail pages during add to cart. Purpose is to allow store managers to have addresses for some customers so that they can place their orders if needed.

We also need to show this address for customer when he is looking at his addresses in My Stores (Stores).

We have added a collection class that extends from Mage_Customer_Model_Resource_Address_Collection and override method: setCustomerFilter.

Below is the code for the method:

if ($customer->getId()) {

            $this->addAttributeToFilter(array(
                array(
                    'attribute' => 'parent_id',
                    'in' => array($customer->getId()),
                ),
                array(
                    'attribute' => ‘store_manager',
                    'in' => array($customer->getId()),
                ),
            ));

        } else {
            $this->addAttributeToFilter('parent_id', '-1');
        }
 return $this;

Customer data is not loading, and all address for customer just got hide when this code is executed. Please suggest what I am missing.

  • It seems Or condition is not working in addAttributeToFilter. So actual addresses assigned to this customer are not displayed but those addresses where this customer is linked are displayed. Is it possible to mix addFieldToFilter and addAttributeToFilter with or condition. – F-Z Jan 27 '16 at 17:31
0

Above query works fine and returns correct results. Only issue was that custom attribute was not populated for all records so Inner join (default for addAttributeToFilter) was creating issue. Changed that to "left join" and all worked well. Updated code below:

if ($customer->getId()) {

            $this->addAttributeToFilter(array(
                array(
                    'attribute' => 'parent_id',
                    'in' => array($customer->getId()),
                ),
                array(
                    'attribute' => ‘store_manager',
                    'in' => array($customer->getId()),
                ),
            ),null,'left');

        } else {
            $this->addAttributeToFilter('parent_id', '-1');
        }
 return $this;

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.