How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site. Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask ATLPROG Your Own Question
ATLPROG
ATLPROG, Computer Software Engineer
Category: Programming
Satisfied Customers: 7260
Experience:  MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
44910485
Type Your Programming Question Here...
ATLPROG is online now

I have an almost complete solution for showing Magento products

Customer Question

I have an almost complete solution for showing Magento products selectively by region.

The regions are:
UK
Europe
Americas
Middle East
Rest of World

All customers have been assigned a region, which I have made available via the variable $customerRegion.

All my Magento Products have a custom attribute 'product_region', which has region options available for multiple selection.

list.phtml should only present products that match the customer's region. I have a partially working solution:

$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter(Mage::getSingleton('catalog/layer')->getCurrentCategory())
->addAttributeToFilter('product_region',
array('eq' => Mage::getResourceModel('catalog/product')
->getAttribute('product_region')
->getSource()
->getOptionId($customerRegion)
)
)
->addAttributeToSelect('*');
If the customer's region is UK, and the product's region is UK, the filter works as expected.
If the customer's region is UK, and the product's region is UK AND Europe, the filter does not work as expected (no results are given).

How can I adjust the filter to get the desired behaviour?

I have tried using 'in' and 'like' instead of 'eq' but have not been successful.

Thank you.

P.S. An easier way to view the same problem is to look at the following code:

->addFieldToFilter(array(
array('attribute'=>'product_region','eq'=>'49')
))
Submitted: 4 years ago.
Category: Programming