Magento – Get product position from category

magento-1navigationproduct

I'm implementing Prev/Next button on Product view page following Inchoo's Blog. It's working fine with Default Magento Theme but in My theme there is an issue with getting product position from Category.

In Product's view.phtml I've added code

<?php $_prev = $this->helper('retailon_productprevnext')->getPreviousProduct(); ?>
<?php $_next = $this->helper('retailon_productprevnext')->getNextProduct(); ?>
<?php if($_prev): ?><a class="product-prev" href="<?php echo $_prev;?>"><?php echo $this->__('< Previous')?></a><?php endif; ?>
<?php if($_next): ?><a class="product-next" href="<?php echo $_next;?>"><?php echo $this->__('Next >')?></a><?php endif; ?>

The Code for previous product in my Helper is like this:

     public function getPreviousProduct()
        {
            $prodId = Mage::registry('current_product')->getId();

            $catArray = Mage::registry('current_category');

            if($catArray){
var_dump($catArray); 
                $catArray = $catArray->getProductsPosition();

var_dump($catArray); 
/// Remaining code to return previous product

First dump is returning category array info, while the second one is not dumping any value.

Is there any other way using which I can get product position from category array instead of using $catArray = $catArray->getProductsPosition(); ?

Best Answer

If the first dump returns an array, I would be astonished if the second dump actually gets executed.

$catArray = $catArray->getProductsPosition();

...must give you a fatal error, because you are calling a function on a non-object. My guess: the products position data is not loaded in the current-category-registry. So what I'd try is:

$category = Mage::getModel('catalog/category')->load($catArray['entity_id']);
$positions = $category->getProductsPosition();
Zend_Debug::dump($positions);