Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate attribute dynamically in Magento

I'm trying to work out if there is a way to integrate location based filtering into Magento catalog & search pages.

To place the above in context my use case is as follows: Products have latitude and longitude attributes as they are located around the country. What I want customers to be able to do is filter the products by location relative to their own location.

I've managed to successfully implement a custom search page to achieve the location search however it lacks all the other filter/search capabilities found in Magento. The ideal scenario is for the location to be a dynamically calculated variable however I'm unsure how to go about this, or if it's even viable in Magento.

Thanks in advance for any suggestions!

like image 541
Antony Smith Avatar asked Oct 30 '22 22:10

Antony Smith


1 Answers

For starters, you can try something like this:

  1. create a product attribute distance_from_customer, default value 0, filterable in layered navigation
  2. create an observer for event catalog_product_collection_load_after, which will change each product's distance_from_customer attribute in the loaded collection based on customer's location each time you load a product collection ( in category view for example )
  3. extend the layered navigation to disable the 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, only will be changed in the product collection object before showing on frontend and the sorting / filtering option will be hidden if the application failed to get customer location data.

like image 148
bogatyrjov Avatar answered Nov 09 '22 04:11

bogatyrjov