Magento – How to Get Product Position on Category Listing Page

category-productsce-1.8.1.0

We have an online tyre store with tyres as products and car models as categories. Since tyres can fit multiple cars, each tyre is mapped to all the car models where there is a fit.

However, each car model has certain original equipment tyres (those that come fitted from the factory on brand new cars). We would like to highlight such tyres on the listing page for the car model with a ribbon-style icon. The important point is that although a tyre can fit many car models, it may not be original fit for all of them (in practice it will be an original fit for only a few of them).

The strategy we have thought is to display all original fit tyres at the beginning on the listing pages by assigning them display positions less than 100. All other tyres, which also fit the car model but are not original fit are assigned display positions greater than 100. Therefore, simply being able to get the product position configured in the admin on the listing page would be sufficient to display an icon by checking if the position is less than 100.

I haven't been able to figure out how to get the product position on the listing page. The important part is that I should be able to read the position even if the user sorts by something other than the position, such as, name, price, etc.

Best Answer

Found a solution for this on my installation of Magento CE 1.8.1.

  1. In the category, assign display positions exactly divisible by 10 to the products that are OE fit for that category.
  2. In the file app/design/{theme}/{sub-theme}/template/catalog/product/list.phtml get the product's position within the category as $_product->getCatIndexPosition().
  3. Check whether the retrieved value leaves a remainder of 0 when divided by 10. If it does, apply a watermark image to the product that indicates an original-fit tyre.

The key part was the $_product->getCatIndexPosition() call that provides access to a product's display position within a specific category.

I have tested this by changing the sort settings (sort by name, price, availability, position, etc.) and sort direction (ascending and descending) and the approach works fine in all cases.