Dynamically compute an attribute in Magento

I am trying to work if there is a way to integrate location-based filtering into the Magento directory and search pages.

To put this in context, I use the following example: Products have latitude and longitude attributes because they are located throughout the country. What I want customers to be able to do is filter products by location relative to their own location.

I managed to successfully complete a custom search page to achieve a location search, but it lacks all the other filter / search features found in Magento. The ideal scenario is for the location to be a dynamically calculated variable, but I'm not sure how to do this, or even if it is viable in Magento.

Thanks in advance for any suggestions!

+5
source share
1 answer

For starters, you can try something like this:

  • create product attribute distance_from_customer , default value 0, filter in multi-level navigation
  • create an observer for the catalog_product_collection_load_after event, which will change each distance_from_customer attribute in the loaded collection based on the location of the client each time the product collection is loaded (for example, as a category)
  • extend layered navigation to disable sorting / filtering based on distance_from_customer if there is only one default value (0)

In this solution, distance_from_customer will always be 0 in the database, it will only be changed in the product collection object before displaying on the interface, and the sort / filter option will be hidden if the application cannot receive data about the client’s location.

+2
source

All Articles