Question speaks for it self. I found 2 blog posts about it. Should I create my own helper class like Here or do it right in view.phtml like here. Which would be best approach and isn't there a better one?
4 Answers
Should I create my own helper class or do it right in view.phtml?
The answer is both.
You need to create a helper and then call that class form within view.phtml
as described in the Inchoo article.
Both the links you've supplied are using the same code, but the one from Inchoo is the original post with the other one being a copy+paste job with some tiny modifications. Use the Inchoo code, it is the best way to implement this functionality on a standard site.
Take note of what the article says about layered navigation - this code will only work with standard categories that have 'Is Anchor' set to 'No'.
There is also another Previous/Next Products extension created by Branko Ajzele. You can find it on github: https://github.com/ajzele/Inchoo_Prevnext (take a look at the original Blog post)
The basic idea is to create an observer, which grabs the currently loaded product collection from product list block. So this one is actually working with layered navigation.
The inchoo code didn't work for me. This works though:
function getNextProductUrl($category, $currentProduct) {
$productPositions = $category->getProductsPosition();
asort($productPositions);
$sortedCatProductIds = array_keys($productPositions);
$currentProductCategoryIndex = @array_search($currentProduct->getId(), $sortedCatProductIds);
$nextProductIds = array_slice($sortedCatProductIds, $currentProductCategoryIndex + 1, count($sortedCatProductIds)); //+1 for next product, -1 for previous product
foreach ($nextProductIds as $productId) {
$product = Mage::getModel('catalog/product')->load($productId);
if ($product && $product->getId() && $product->isVisibleInCatalog() && $product->isVisibleInSiteVisibility()) {
return $product->getProductUrl();
}
}
return false;
}
-
Great @Willster. We were having some problems too. Did you update the Inchoo code? Or make your own solution? Would love to see complete– snh_nlJul 30, 2015 at 7:40
-
@snh_nl I came up with my own solution based on various sources. And I'm not sure what you mean by 'complete'. The above function IS complete. You need to pass it a category and the current product. Not sure what more you'd want!? And if you find an answer useful on stack exchange, it's customary to up vote it.– WillsterJul 30, 2015 at 10:00
-
Checkout the Inchoo code on github. We just added a pull request to improve. Anyway thanks.– snh_nlJul 30, 2015 at 10:44
There is an extension by aheadWorks I worked with in the past. It is not cheap, but does its job.
http://ecommerce.aheadworks.com/magento-extensions/user-experience/previous-next.html
-
3I'm not sure I'd recommend adding the bloat of an AW module (with its questionable licensing system) for the sake of a back/forwards link. It could be done with 2 lines of inline code. Mar 7, 2013 at 10:13
-
-
-
3Don't get me wrong. I'm not putting AW down - but solving functionality through bolting on extension after extension is just asking for trouble; especially for a task as simple as this Mar 8, 2013 at 14:54
-
1The tendency with Magento is to add a new extension every time you come across something that isn't standard. The AW extension is good for store owners who can't code, want a solution that works out of the box and gives them a place to customise every single variable through config fields. As @sonassi said this is a very basic task and seeing as this is primarily a coding forum I think the best answer would be one that helps developers achieve this functionality for themselves. Mar 12, 2013 at 1:08