1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0"?> <config> <modules> <Fc_NextPrevProduct> <version>0.1.0</version> </Fc_NextPrevProduct> </modules> <global> <helpers> <nextprevproduct> <class>Fc_NextPrevProduct_Helper</class> </nextprevproduct> </helpers> </global> <frontend> <layout> <updates> <Fc_nextprevproduct> <file>fc/newprevproduct.xml</file> </fc_nextprevproduct> </updates> </layout> </frontend> </config> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<?php class Fc_NextPrevProduct_Helper_Data extends Mage_Core_Helper_Abstract { public function getNextProductUrl($productId, $currentCategory) { if($currentCategory){ $productId = (int)$productId; $currentCategory = $this->_getProductsPosition( $currentCategory ) ; $keys = array_flip(array_keys($currentCategory)); $values = array_keys($currentCategory); $lastProduct = end($values); //make sure that this isn't the last product in the category if($lastProduct != $productId) { $productId = $values[$keys[$productId]+1]; $product = Mage::getModel('catalog/product'); if($productId){ $product->load($productId); return $product->getProductUrl(); } } else { return false; } } return false; } public function _getProductsPosition( $a_category ) { $category = Mage::getModel('catalog/category')->load( $a_category->getId() ); $positions = Mage::getResourceModel('catalog/category')->getProductsPosition( $category ); return $positions ; } public function getPrevProductUrl($productId, $currentCategory) { if($currentCategory){ $currentCategory = $this->_getProductsPosition( $currentCategory ) ; $keys = array_flip(array_keys($currentCategory)); $values = array_keys($currentCategory); $productId = (int)$productId; //make sure this isn't the first product in the category if($values[0] != $productId) { $productId = $values[$keys[$productId]-1]; $product = Mage::getModel('catalog/product'); if($productId){ $product->load($productId); return $product->getProductUrl(); } } else { return false; } } return false; } } |
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0"?> <config> <modules> <Fc_NextPrevProduct> <active>true</active> <codePool>local</codePool> </Fc_NextPrevProduct> </modules> </config> |
Usage
The below can be used in any template file on a product page.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $nextProductUrl = Mage::helper('nextprevproduct')->getNextProductUrl(); $prevProductUrl = Mage::helper('nextprevproduct')->getPrevProductUrl(); ?> <?php if($prevProductUrl) : ?> <a href="<?php echo $prevProductUrl; ?>"><- Previous Product</a> <?php endif; ?> <?php if($nextProductUrl) : ?> <a href="<?php echo $prevProductUrl; ?>">Next Product --></a> <?php endif; ?> |