How to activate product navigation in magento 2?Layered NavigationCant edit structure of top navigation menuSystem errors with Navigation BlockAdd “My Ordes” to My Account NavigationHow to customize navigation in Magento 2?Magento 2 Navigation - Products CategoriesMagento Navigation Menu - Sub NavigationHow to move cart in navigation sectionhow I can activate ReCAPTCHAMagento 2 CE : Add child navigation items to navigation menu
Does splitting a potentially monolithic application into several smaller ones help prevent bugs?
What Happens when Passenger Refuses to Fly Boeing 737 Max?
Life insurance that covers only simultaneous/dual deaths
Can infringement of a trademark be pursued for using a company's name in a sentence?
Best approach to update all entries in a list that is paginated?
"However" used in a conditional clause?
What is the definition of "Natural Selection"?
Word for a person who has no opinion about whether god exists
Is King K. Rool's down throw to up-special a true combo?
Force user to remove USB token
Potentiometer like component
Question about partial fractions with irreducible quadratic factors
What has been your most complicated TikZ drawing?
Can you reject a postdoc offer after the PI has paid a large sum for flights/accommodation for your visit?
Why must traveling waves have the same amplitude to form a standing wave?
How is the Swiss post e-voting system supposed to work, and how was it wrong?
Silly Sally's Movie
How do anti-virus programs start at Windows boot?
Why do Australian milk farmers need to protest supermarkets' milk price?
Make a transparent 448*448 image
Is it true that real estate prices mainly go up?
Can someone explain this Mudra being done by Ramakrishna Paramhansa in Samadhi?
Should we release the security issues we found in our product as CVE or we can just update those on weekly release notes?
Touchscreen-controlled dentist office snowman collector game
How to activate product navigation in magento 2?
Layered NavigationCant edit structure of top navigation menuSystem errors with Navigation BlockAdd “My Ordes” to My Account NavigationHow to customize navigation in Magento 2?Magento 2 Navigation - Products CategoriesMagento Navigation Menu - Sub NavigationHow to move cart in navigation sectionhow I can activate ReCAPTCHAMagento 2 CE : Add child navigation items to navigation menu
Magento 2 product navigation (next and previous buttons) in the product details page are not working for me. I cannot find any settings to enable this feature. I explored the template to magento_home/vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml
and found the following script-
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<?php /* @var $block MagentoCatalogBlockProductViewAbstractView */?>
<?php $_product = $block->getProduct() ?>
<?php if ($block->displayProductStockStatus()): ?>
<?php if ($_product->isAvailable()): ?>
<div class="stock available" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
<span><?php /* @escapeNotVerified */ echo __('In stock') ?></span>
</div>
<?php else: ?>
<div class="stock unavailable" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
<span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span>
</div>
<?php endif; ?>
<?php endif; ?>
However, it is taking me nowhere. How to enable Next, and Previous buttons to the product details page to explore next and previous products?
I have found few tutorials on it, as this and this but they are all for Magento 1.x. I am confused whether this feature is available for Magento 2. Please confirm if you have implemented this feature successfully.
magento2 navigation
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
Magento 2 product navigation (next and previous buttons) in the product details page are not working for me. I cannot find any settings to enable this feature. I explored the template to magento_home/vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml
and found the following script-
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<?php /* @var $block MagentoCatalogBlockProductViewAbstractView */?>
<?php $_product = $block->getProduct() ?>
<?php if ($block->displayProductStockStatus()): ?>
<?php if ($_product->isAvailable()): ?>
<div class="stock available" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
<span><?php /* @escapeNotVerified */ echo __('In stock') ?></span>
</div>
<?php else: ?>
<div class="stock unavailable" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
<span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span>
</div>
<?php endif; ?>
<?php endif; ?>
However, it is taking me nowhere. How to enable Next, and Previous buttons to the product details page to explore next and previous products?
I have found few tutorials on it, as this and this but they are all for Magento 1.x. I am confused whether this feature is available for Magento 2. Please confirm if you have implemented this feature successfully.
magento2 navigation
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
which version are you using? in the current 2.0 Branch this is not present: github.com/magento/magento2/blob/2.0/app/code/Magento/Catalog/…. Maybe this feature never made it into the stable release (yet?)
– David Verholen
May 11 '16 at 17:13
I am using Magento-CE-2.0.2. It has this code.
– Morison
May 11 '16 at 17:19
thats weird. This is the file in version 2.0.2 from the github repo: github.com/magento/magento2/blob/2.0.2/app/code/Magento/Catalog/…. It's still possible that there is different code in the composer repo (maybe due to a bug in the build process from magento) but its most likely that this feature did not (yet) make it into the final release.
– David Verholen
May 11 '16 at 17:25
I copy-paste the whole code from magento_2.0.2/vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml . I downloaded the Magento-CE-2.0.2+sample_data-2016-01-28-02-53-25.tar.bz2 version from Magento site.
– Morison
May 11 '16 at 17:34
add a comment |
Magento 2 product navigation (next and previous buttons) in the product details page are not working for me. I cannot find any settings to enable this feature. I explored the template to magento_home/vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml
and found the following script-
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<?php /* @var $block MagentoCatalogBlockProductViewAbstractView */?>
<?php $_product = $block->getProduct() ?>
<?php if ($block->displayProductStockStatus()): ?>
<?php if ($_product->isAvailable()): ?>
<div class="stock available" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
<span><?php /* @escapeNotVerified */ echo __('In stock') ?></span>
</div>
<?php else: ?>
<div class="stock unavailable" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
<span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span>
</div>
<?php endif; ?>
<?php endif; ?>
However, it is taking me nowhere. How to enable Next, and Previous buttons to the product details page to explore next and previous products?
I have found few tutorials on it, as this and this but they are all for Magento 1.x. I am confused whether this feature is available for Magento 2. Please confirm if you have implemented this feature successfully.
magento2 navigation
Magento 2 product navigation (next and previous buttons) in the product details page are not working for me. I cannot find any settings to enable this feature. I explored the template to magento_home/vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml
and found the following script-
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<?php /* @var $block MagentoCatalogBlockProductViewAbstractView */?>
<?php $_product = $block->getProduct() ?>
<?php if ($block->displayProductStockStatus()): ?>
<?php if ($_product->isAvailable()): ?>
<div class="stock available" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
<span><?php /* @escapeNotVerified */ echo __('In stock') ?></span>
</div>
<?php else: ?>
<div class="stock unavailable" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
<span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span>
</div>
<?php endif; ?>
<?php endif; ?>
However, it is taking me nowhere. How to enable Next, and Previous buttons to the product details page to explore next and previous products?
I have found few tutorials on it, as this and this but they are all for Magento 1.x. I am confused whether this feature is available for Magento 2. Please confirm if you have implemented this feature successfully.
magento2 navigation
magento2 navigation
edited May 11 '16 at 20:07
Marius♦
167k28319683
167k28319683
asked May 11 '16 at 16:52
MorisonMorison
991
991
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
which version are you using? in the current 2.0 Branch this is not present: github.com/magento/magento2/blob/2.0/app/code/Magento/Catalog/…. Maybe this feature never made it into the stable release (yet?)
– David Verholen
May 11 '16 at 17:13
I am using Magento-CE-2.0.2. It has this code.
– Morison
May 11 '16 at 17:19
thats weird. This is the file in version 2.0.2 from the github repo: github.com/magento/magento2/blob/2.0.2/app/code/Magento/Catalog/…. It's still possible that there is different code in the composer repo (maybe due to a bug in the build process from magento) but its most likely that this feature did not (yet) make it into the final release.
– David Verholen
May 11 '16 at 17:25
I copy-paste the whole code from magento_2.0.2/vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml . I downloaded the Magento-CE-2.0.2+sample_data-2016-01-28-02-53-25.tar.bz2 version from Magento site.
– Morison
May 11 '16 at 17:34
add a comment |
which version are you using? in the current 2.0 Branch this is not present: github.com/magento/magento2/blob/2.0/app/code/Magento/Catalog/…. Maybe this feature never made it into the stable release (yet?)
– David Verholen
May 11 '16 at 17:13
I am using Magento-CE-2.0.2. It has this code.
– Morison
May 11 '16 at 17:19
thats weird. This is the file in version 2.0.2 from the github repo: github.com/magento/magento2/blob/2.0.2/app/code/Magento/Catalog/…. It's still possible that there is different code in the composer repo (maybe due to a bug in the build process from magento) but its most likely that this feature did not (yet) make it into the final release.
– David Verholen
May 11 '16 at 17:25
I copy-paste the whole code from magento_2.0.2/vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml . I downloaded the Magento-CE-2.0.2+sample_data-2016-01-28-02-53-25.tar.bz2 version from Magento site.
– Morison
May 11 '16 at 17:34
which version are you using? in the current 2.0 Branch this is not present: github.com/magento/magento2/blob/2.0/app/code/Magento/Catalog/…. Maybe this feature never made it into the stable release (yet?)
– David Verholen
May 11 '16 at 17:13
which version are you using? in the current 2.0 Branch this is not present: github.com/magento/magento2/blob/2.0/app/code/Magento/Catalog/…. Maybe this feature never made it into the stable release (yet?)
– David Verholen
May 11 '16 at 17:13
I am using Magento-CE-2.0.2. It has this code.
– Morison
May 11 '16 at 17:19
I am using Magento-CE-2.0.2. It has this code.
– Morison
May 11 '16 at 17:19
thats weird. This is the file in version 2.0.2 from the github repo: github.com/magento/magento2/blob/2.0.2/app/code/Magento/Catalog/…. It's still possible that there is different code in the composer repo (maybe due to a bug in the build process from magento) but its most likely that this feature did not (yet) make it into the final release.
– David Verholen
May 11 '16 at 17:25
thats weird. This is the file in version 2.0.2 from the github repo: github.com/magento/magento2/blob/2.0.2/app/code/Magento/Catalog/…. It's still possible that there is different code in the composer repo (maybe due to a bug in the build process from magento) but its most likely that this feature did not (yet) make it into the final release.
– David Verholen
May 11 '16 at 17:25
I copy-paste the whole code from magento_2.0.2/vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml . I downloaded the Magento-CE-2.0.2+sample_data-2016-01-28-02-53-25.tar.bz2 version from Magento site.
– Morison
May 11 '16 at 17:34
I copy-paste the whole code from magento_2.0.2/vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml . I downloaded the Magento-CE-2.0.2+sample_data-2016-01-28-02-53-25.tar.bz2 version from Magento site.
– Morison
May 11 '16 at 17:34
add a comment |
2 Answers
2
active
oldest
votes
Here is a way you could do this. The trick seems to be using the API instead of using the Object Manager. With the object manager you can call a 'create' method to pull in the product object and load the product with the product id, but so far most advise warns against this method. Using the API you can call the getById()
method on the object and get back a unique product object each time you call it. Theoretically with should preform better then using the object manager, and if the object manager goes away at some point, you aren't re-writing code (Alan Storm goes into the issues with the object manager here: Magento 2 Object Manager).
Doing it any other way you will run into Magento's automatic singleton design and will never be able to get the link url for each product, as the first product loaded by ID with set that value and it will persist in the page.
Here is the block (in a module you would put this in vender-namespace/module-name/Block/Product.php)
<?php
namespace vender-namespace/module-nameBlock;
use MagentoFrameworkViewElementTemplate;
class Product extends Template
public function __construct(
TemplateContext $context,
MagentoCatalogBlockProductViewabstractView $product,
MagentoCatalogApiProductRepositoryInterface $productModel,
array $data = []
)
parent::__construct($context, $data);
$this->_product = $product;
$this->_productModel = $productModel;
protected function _prepareLayout()
return parent::_prepareLayout();
protected function _getCurrentProduct()
return $this->_product->getProduct();
protected function _getCurrentCategory()
$currentCategories = $this->_getCurrentProduct()->getCategoryCollection();
foreach ($currentCategories as $currentCategory)
return $currentCategory;
protected function _getProductById($id)
$_product = $this->_productModel->getById($id);
return $_product;
public function getPreviousProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollectionIds = $this->_getCurrentCategory()->getProductCollection()->getAllIds();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = array_search($_currentProductId, $_productCollectionIds);
$_previousProductId = $_productCollectionIds[$_currentProductCollectionId - 1];
if ($_previousProductId)
return $_previousProductId;
return false;
return false;
public function getNextProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollectionIds = $this->_getCurrentCategory()->getProductCollection()->getAllIds();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = array_search($_currentProductId, $_productCollectionIds);
$_nextProductId = $_productCollectionIds[$_currentProductCollectionId + 1];
$_product = $this->_productModel;
if ($_nextProductId)
return $_nextProductId;
return false;
return false;
public function getPreviousProductUrl()
$_previousProductUrl = $this->_getProductById($this->getPreviousProductId())->getProductUrl();
return $_previousProductUrl;
public function getNextProductUrl()
$_nextProductUrl = $this->_getProductById($this->getNextProductId())->getProductUrl();
return $_nextProductUrl;
public function getPreviousProductName()
$_previousProductName = $this->_getProductById($this->getPreviousProductId())->getName();
return $_previousProductName;
public function getNextProductName()
$_nextProductName = $this->_getProductById($this->getNextProductId())->getName();
return $_nextProductName;
public function getPreviousProductImage()
$_previousProductImage = $this->_getProductById($this->getPreviousProductId())->getImage();
return $_previousProductImage;
public function getNextProductImage()
$_nextProductImage = $this->_getProductById($this->getNextProductId())->getImage();
return $_nextProductImage;
For displaying this on the front end you need to make a .xml file that will display the template on the product page top (in a module you would put this in vender-namespace/module-name/view/frontend/layout/catalog_product_view.xml)
<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="vender-namespace/module-nameBlockProduct" name="custom.product.pagination" template="vender-namespace/module-name/::product/product-pagination.phtml" before="-" />
</referenceContainer>
</body>
</page>
And then in your phtml file (in a module you would put this in vender-namespace/module-name/view/frontend/template/product/product-pagination.phtml)
<?php
$previousProductUrl = $block->getPreviousProductUrl();
$nextProductUrl = $block->getNextProductUrl();
$previousProductName = $block->getPreviousProductName();
$nextProductName = $block->getNextProductName();
$previousProductImage = $block->getPreviousProductImage();
$nextProductImage = $block->getNextProductImage();
$previousImage = $this->getBaseUrl() . 'pub/media/catalog/product' . $previousProductImage;
$nextImage = $this->getBaseUrl() . 'pub/media/catalog/product' . $nextProductImage;
?>
<div class="product-pagination">
<div class="previous-product" style="float: left;">
<a href="<?php echo $previousProductUrl; ?>">
<img src="<?php echo $previousImage; ?>" alt="<?php echo $previousProductName; ?>" height="75" width="75" />
<p><?php echo $previousProductName; ?></p>
<button type="submit" title="Previous Product" class="action primary to-prev-product">
<span>← prev</span>
</button>
</a>
</div>
<div class="next-product" style="float: right;">
<a href="<?php echo $nextProductUrl; ?>">
<img src="<?php echo $nextImage; ?>" alt="<?php echo $nextProductName; ?>" height="75" width="75" />
<p><?php echo $nextProductName; ?></p>
<button type="submit" title="Next Product" class="action primary to-next-product">
<span>next →</span>
</button>
</a>
</div>
</div>
Of course there is a lot more you can do with this as far as theme integration and styling. But for a proof of concept, this will get you there.
add a comment |
Just improved circlesix's block code to handle first and last items in the category and name ordered navigation (instead of product ids)
namespace XXXXXXModulesBlock;
use MagentoFrameworkViewElementTemplate;
class ProductNextPrevious extends Template
public function __construct(
TemplateContext $context,
MagentoCatalogBlockProductViewabstractView $product,
MagentoCatalogApiProductRepositoryInterface $productModel,
array $data = []
)
parent::__construct($context, $data);
$this->_product = $product;
$this->_productModel = $productModel;
protected function _prepareLayout()
return parent::_prepareLayout();
protected function _getCurrentProduct()
return $this->_product->getProduct();
protected function _getCurrentCategory()
$currentCategories = $this->_getCurrentProduct()->getCategoryCollection();
foreach ($currentCategories as $currentCategory)
return $currentCategory;
protected function _getProductById($id)
$_product = $this->_productModel->getById($id);
return $_product;
public function getPreviousProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollection = $this->_getCurrentCategory()->getProductCollection()
->addAttributeToSort('name')
->getData();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = 0;
foreach ($_productCollection as $item)
if ( $item['entity_id'] == $_currentProductId )
break;
$_currentProductCollectionId ++;
if ($_currentProductCollectionId == 0 )
return false;
$_previousProductId = $_productCollection[$_currentProductCollectionId - 1]['entity_id'];
if ($_previousProductId)
return $_previousProductId;
return false;
return false;
public function getNextProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollection = $this->_getCurrentCategory()
->getProductCollection()
->addAttributeToSort('name')
->getData();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = 0;
foreach ($_productCollection as $item)
if ( $item['entity_id'] == $_currentProductId )
break;
$_currentProductCollectionId ++;
// last element
if ( $_currentProductCollectionId == ( count ( $_productCollection) - 1 ) )
return false;
$_nextProductId = $_productCollection[$_currentProductCollectionId + 1]['entity_id'];
$_product = $this->_productModel;
if ($_nextProductId)
return $_nextProductId;
return false;
return false;
public function getPreviousProductUrl()
if ( ! $productId = $this->getPreviousProductId() )
return false;
if (! $product = $this->_getProductById($productId) )
return false;
$_previousProductUrl = $product ->getProductUrl();
return $_previousProductUrl;
public function getNextProductUrl()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductUrl = $this->_getProductById($productId)->getProductUrl();
return $_nextProductUrl;
public function getPreviousProductName()
if ( ! $productId = $this->getPreviousProductId() )
return false;
$_previousProductName = $this->_getProductById($productId )->getName();
return $_previousProductName;
public function getNextProductName()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductName = $this->_getProductById($productId)->getName();
return $_nextProductName;
public function getPreviousProductImage()
if ( ! $productId = $this->getPreviousProductId() )
return false;
$_previousProductImage = $this->_getProductById($productId)->getImage();
return $_previousProductImage;
public function getNextProductImage()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductImage = $this->_getProductById($productId )->getImage();
return $_nextProductImage;
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f114953%2fhow-to-activate-product-navigation-in-magento-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a way you could do this. The trick seems to be using the API instead of using the Object Manager. With the object manager you can call a 'create' method to pull in the product object and load the product with the product id, but so far most advise warns against this method. Using the API you can call the getById()
method on the object and get back a unique product object each time you call it. Theoretically with should preform better then using the object manager, and if the object manager goes away at some point, you aren't re-writing code (Alan Storm goes into the issues with the object manager here: Magento 2 Object Manager).
Doing it any other way you will run into Magento's automatic singleton design and will never be able to get the link url for each product, as the first product loaded by ID with set that value and it will persist in the page.
Here is the block (in a module you would put this in vender-namespace/module-name/Block/Product.php)
<?php
namespace vender-namespace/module-nameBlock;
use MagentoFrameworkViewElementTemplate;
class Product extends Template
public function __construct(
TemplateContext $context,
MagentoCatalogBlockProductViewabstractView $product,
MagentoCatalogApiProductRepositoryInterface $productModel,
array $data = []
)
parent::__construct($context, $data);
$this->_product = $product;
$this->_productModel = $productModel;
protected function _prepareLayout()
return parent::_prepareLayout();
protected function _getCurrentProduct()
return $this->_product->getProduct();
protected function _getCurrentCategory()
$currentCategories = $this->_getCurrentProduct()->getCategoryCollection();
foreach ($currentCategories as $currentCategory)
return $currentCategory;
protected function _getProductById($id)
$_product = $this->_productModel->getById($id);
return $_product;
public function getPreviousProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollectionIds = $this->_getCurrentCategory()->getProductCollection()->getAllIds();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = array_search($_currentProductId, $_productCollectionIds);
$_previousProductId = $_productCollectionIds[$_currentProductCollectionId - 1];
if ($_previousProductId)
return $_previousProductId;
return false;
return false;
public function getNextProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollectionIds = $this->_getCurrentCategory()->getProductCollection()->getAllIds();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = array_search($_currentProductId, $_productCollectionIds);
$_nextProductId = $_productCollectionIds[$_currentProductCollectionId + 1];
$_product = $this->_productModel;
if ($_nextProductId)
return $_nextProductId;
return false;
return false;
public function getPreviousProductUrl()
$_previousProductUrl = $this->_getProductById($this->getPreviousProductId())->getProductUrl();
return $_previousProductUrl;
public function getNextProductUrl()
$_nextProductUrl = $this->_getProductById($this->getNextProductId())->getProductUrl();
return $_nextProductUrl;
public function getPreviousProductName()
$_previousProductName = $this->_getProductById($this->getPreviousProductId())->getName();
return $_previousProductName;
public function getNextProductName()
$_nextProductName = $this->_getProductById($this->getNextProductId())->getName();
return $_nextProductName;
public function getPreviousProductImage()
$_previousProductImage = $this->_getProductById($this->getPreviousProductId())->getImage();
return $_previousProductImage;
public function getNextProductImage()
$_nextProductImage = $this->_getProductById($this->getNextProductId())->getImage();
return $_nextProductImage;
For displaying this on the front end you need to make a .xml file that will display the template on the product page top (in a module you would put this in vender-namespace/module-name/view/frontend/layout/catalog_product_view.xml)
<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="vender-namespace/module-nameBlockProduct" name="custom.product.pagination" template="vender-namespace/module-name/::product/product-pagination.phtml" before="-" />
</referenceContainer>
</body>
</page>
And then in your phtml file (in a module you would put this in vender-namespace/module-name/view/frontend/template/product/product-pagination.phtml)
<?php
$previousProductUrl = $block->getPreviousProductUrl();
$nextProductUrl = $block->getNextProductUrl();
$previousProductName = $block->getPreviousProductName();
$nextProductName = $block->getNextProductName();
$previousProductImage = $block->getPreviousProductImage();
$nextProductImage = $block->getNextProductImage();
$previousImage = $this->getBaseUrl() . 'pub/media/catalog/product' . $previousProductImage;
$nextImage = $this->getBaseUrl() . 'pub/media/catalog/product' . $nextProductImage;
?>
<div class="product-pagination">
<div class="previous-product" style="float: left;">
<a href="<?php echo $previousProductUrl; ?>">
<img src="<?php echo $previousImage; ?>" alt="<?php echo $previousProductName; ?>" height="75" width="75" />
<p><?php echo $previousProductName; ?></p>
<button type="submit" title="Previous Product" class="action primary to-prev-product">
<span>← prev</span>
</button>
</a>
</div>
<div class="next-product" style="float: right;">
<a href="<?php echo $nextProductUrl; ?>">
<img src="<?php echo $nextImage; ?>" alt="<?php echo $nextProductName; ?>" height="75" width="75" />
<p><?php echo $nextProductName; ?></p>
<button type="submit" title="Next Product" class="action primary to-next-product">
<span>next →</span>
</button>
</a>
</div>
</div>
Of course there is a lot more you can do with this as far as theme integration and styling. But for a proof of concept, this will get you there.
add a comment |
Here is a way you could do this. The trick seems to be using the API instead of using the Object Manager. With the object manager you can call a 'create' method to pull in the product object and load the product with the product id, but so far most advise warns against this method. Using the API you can call the getById()
method on the object and get back a unique product object each time you call it. Theoretically with should preform better then using the object manager, and if the object manager goes away at some point, you aren't re-writing code (Alan Storm goes into the issues with the object manager here: Magento 2 Object Manager).
Doing it any other way you will run into Magento's automatic singleton design and will never be able to get the link url for each product, as the first product loaded by ID with set that value and it will persist in the page.
Here is the block (in a module you would put this in vender-namespace/module-name/Block/Product.php)
<?php
namespace vender-namespace/module-nameBlock;
use MagentoFrameworkViewElementTemplate;
class Product extends Template
public function __construct(
TemplateContext $context,
MagentoCatalogBlockProductViewabstractView $product,
MagentoCatalogApiProductRepositoryInterface $productModel,
array $data = []
)
parent::__construct($context, $data);
$this->_product = $product;
$this->_productModel = $productModel;
protected function _prepareLayout()
return parent::_prepareLayout();
protected function _getCurrentProduct()
return $this->_product->getProduct();
protected function _getCurrentCategory()
$currentCategories = $this->_getCurrentProduct()->getCategoryCollection();
foreach ($currentCategories as $currentCategory)
return $currentCategory;
protected function _getProductById($id)
$_product = $this->_productModel->getById($id);
return $_product;
public function getPreviousProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollectionIds = $this->_getCurrentCategory()->getProductCollection()->getAllIds();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = array_search($_currentProductId, $_productCollectionIds);
$_previousProductId = $_productCollectionIds[$_currentProductCollectionId - 1];
if ($_previousProductId)
return $_previousProductId;
return false;
return false;
public function getNextProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollectionIds = $this->_getCurrentCategory()->getProductCollection()->getAllIds();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = array_search($_currentProductId, $_productCollectionIds);
$_nextProductId = $_productCollectionIds[$_currentProductCollectionId + 1];
$_product = $this->_productModel;
if ($_nextProductId)
return $_nextProductId;
return false;
return false;
public function getPreviousProductUrl()
$_previousProductUrl = $this->_getProductById($this->getPreviousProductId())->getProductUrl();
return $_previousProductUrl;
public function getNextProductUrl()
$_nextProductUrl = $this->_getProductById($this->getNextProductId())->getProductUrl();
return $_nextProductUrl;
public function getPreviousProductName()
$_previousProductName = $this->_getProductById($this->getPreviousProductId())->getName();
return $_previousProductName;
public function getNextProductName()
$_nextProductName = $this->_getProductById($this->getNextProductId())->getName();
return $_nextProductName;
public function getPreviousProductImage()
$_previousProductImage = $this->_getProductById($this->getPreviousProductId())->getImage();
return $_previousProductImage;
public function getNextProductImage()
$_nextProductImage = $this->_getProductById($this->getNextProductId())->getImage();
return $_nextProductImage;
For displaying this on the front end you need to make a .xml file that will display the template on the product page top (in a module you would put this in vender-namespace/module-name/view/frontend/layout/catalog_product_view.xml)
<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="vender-namespace/module-nameBlockProduct" name="custom.product.pagination" template="vender-namespace/module-name/::product/product-pagination.phtml" before="-" />
</referenceContainer>
</body>
</page>
And then in your phtml file (in a module you would put this in vender-namespace/module-name/view/frontend/template/product/product-pagination.phtml)
<?php
$previousProductUrl = $block->getPreviousProductUrl();
$nextProductUrl = $block->getNextProductUrl();
$previousProductName = $block->getPreviousProductName();
$nextProductName = $block->getNextProductName();
$previousProductImage = $block->getPreviousProductImage();
$nextProductImage = $block->getNextProductImage();
$previousImage = $this->getBaseUrl() . 'pub/media/catalog/product' . $previousProductImage;
$nextImage = $this->getBaseUrl() . 'pub/media/catalog/product' . $nextProductImage;
?>
<div class="product-pagination">
<div class="previous-product" style="float: left;">
<a href="<?php echo $previousProductUrl; ?>">
<img src="<?php echo $previousImage; ?>" alt="<?php echo $previousProductName; ?>" height="75" width="75" />
<p><?php echo $previousProductName; ?></p>
<button type="submit" title="Previous Product" class="action primary to-prev-product">
<span>← prev</span>
</button>
</a>
</div>
<div class="next-product" style="float: right;">
<a href="<?php echo $nextProductUrl; ?>">
<img src="<?php echo $nextImage; ?>" alt="<?php echo $nextProductName; ?>" height="75" width="75" />
<p><?php echo $nextProductName; ?></p>
<button type="submit" title="Next Product" class="action primary to-next-product">
<span>next →</span>
</button>
</a>
</div>
</div>
Of course there is a lot more you can do with this as far as theme integration and styling. But for a proof of concept, this will get you there.
add a comment |
Here is a way you could do this. The trick seems to be using the API instead of using the Object Manager. With the object manager you can call a 'create' method to pull in the product object and load the product with the product id, but so far most advise warns against this method. Using the API you can call the getById()
method on the object and get back a unique product object each time you call it. Theoretically with should preform better then using the object manager, and if the object manager goes away at some point, you aren't re-writing code (Alan Storm goes into the issues with the object manager here: Magento 2 Object Manager).
Doing it any other way you will run into Magento's automatic singleton design and will never be able to get the link url for each product, as the first product loaded by ID with set that value and it will persist in the page.
Here is the block (in a module you would put this in vender-namespace/module-name/Block/Product.php)
<?php
namespace vender-namespace/module-nameBlock;
use MagentoFrameworkViewElementTemplate;
class Product extends Template
public function __construct(
TemplateContext $context,
MagentoCatalogBlockProductViewabstractView $product,
MagentoCatalogApiProductRepositoryInterface $productModel,
array $data = []
)
parent::__construct($context, $data);
$this->_product = $product;
$this->_productModel = $productModel;
protected function _prepareLayout()
return parent::_prepareLayout();
protected function _getCurrentProduct()
return $this->_product->getProduct();
protected function _getCurrentCategory()
$currentCategories = $this->_getCurrentProduct()->getCategoryCollection();
foreach ($currentCategories as $currentCategory)
return $currentCategory;
protected function _getProductById($id)
$_product = $this->_productModel->getById($id);
return $_product;
public function getPreviousProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollectionIds = $this->_getCurrentCategory()->getProductCollection()->getAllIds();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = array_search($_currentProductId, $_productCollectionIds);
$_previousProductId = $_productCollectionIds[$_currentProductCollectionId - 1];
if ($_previousProductId)
return $_previousProductId;
return false;
return false;
public function getNextProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollectionIds = $this->_getCurrentCategory()->getProductCollection()->getAllIds();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = array_search($_currentProductId, $_productCollectionIds);
$_nextProductId = $_productCollectionIds[$_currentProductCollectionId + 1];
$_product = $this->_productModel;
if ($_nextProductId)
return $_nextProductId;
return false;
return false;
public function getPreviousProductUrl()
$_previousProductUrl = $this->_getProductById($this->getPreviousProductId())->getProductUrl();
return $_previousProductUrl;
public function getNextProductUrl()
$_nextProductUrl = $this->_getProductById($this->getNextProductId())->getProductUrl();
return $_nextProductUrl;
public function getPreviousProductName()
$_previousProductName = $this->_getProductById($this->getPreviousProductId())->getName();
return $_previousProductName;
public function getNextProductName()
$_nextProductName = $this->_getProductById($this->getNextProductId())->getName();
return $_nextProductName;
public function getPreviousProductImage()
$_previousProductImage = $this->_getProductById($this->getPreviousProductId())->getImage();
return $_previousProductImage;
public function getNextProductImage()
$_nextProductImage = $this->_getProductById($this->getNextProductId())->getImage();
return $_nextProductImage;
For displaying this on the front end you need to make a .xml file that will display the template on the product page top (in a module you would put this in vender-namespace/module-name/view/frontend/layout/catalog_product_view.xml)
<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="vender-namespace/module-nameBlockProduct" name="custom.product.pagination" template="vender-namespace/module-name/::product/product-pagination.phtml" before="-" />
</referenceContainer>
</body>
</page>
And then in your phtml file (in a module you would put this in vender-namespace/module-name/view/frontend/template/product/product-pagination.phtml)
<?php
$previousProductUrl = $block->getPreviousProductUrl();
$nextProductUrl = $block->getNextProductUrl();
$previousProductName = $block->getPreviousProductName();
$nextProductName = $block->getNextProductName();
$previousProductImage = $block->getPreviousProductImage();
$nextProductImage = $block->getNextProductImage();
$previousImage = $this->getBaseUrl() . 'pub/media/catalog/product' . $previousProductImage;
$nextImage = $this->getBaseUrl() . 'pub/media/catalog/product' . $nextProductImage;
?>
<div class="product-pagination">
<div class="previous-product" style="float: left;">
<a href="<?php echo $previousProductUrl; ?>">
<img src="<?php echo $previousImage; ?>" alt="<?php echo $previousProductName; ?>" height="75" width="75" />
<p><?php echo $previousProductName; ?></p>
<button type="submit" title="Previous Product" class="action primary to-prev-product">
<span>← prev</span>
</button>
</a>
</div>
<div class="next-product" style="float: right;">
<a href="<?php echo $nextProductUrl; ?>">
<img src="<?php echo $nextImage; ?>" alt="<?php echo $nextProductName; ?>" height="75" width="75" />
<p><?php echo $nextProductName; ?></p>
<button type="submit" title="Next Product" class="action primary to-next-product">
<span>next →</span>
</button>
</a>
</div>
</div>
Of course there is a lot more you can do with this as far as theme integration and styling. But for a proof of concept, this will get you there.
Here is a way you could do this. The trick seems to be using the API instead of using the Object Manager. With the object manager you can call a 'create' method to pull in the product object and load the product with the product id, but so far most advise warns against this method. Using the API you can call the getById()
method on the object and get back a unique product object each time you call it. Theoretically with should preform better then using the object manager, and if the object manager goes away at some point, you aren't re-writing code (Alan Storm goes into the issues with the object manager here: Magento 2 Object Manager).
Doing it any other way you will run into Magento's automatic singleton design and will never be able to get the link url for each product, as the first product loaded by ID with set that value and it will persist in the page.
Here is the block (in a module you would put this in vender-namespace/module-name/Block/Product.php)
<?php
namespace vender-namespace/module-nameBlock;
use MagentoFrameworkViewElementTemplate;
class Product extends Template
public function __construct(
TemplateContext $context,
MagentoCatalogBlockProductViewabstractView $product,
MagentoCatalogApiProductRepositoryInterface $productModel,
array $data = []
)
parent::__construct($context, $data);
$this->_product = $product;
$this->_productModel = $productModel;
protected function _prepareLayout()
return parent::_prepareLayout();
protected function _getCurrentProduct()
return $this->_product->getProduct();
protected function _getCurrentCategory()
$currentCategories = $this->_getCurrentProduct()->getCategoryCollection();
foreach ($currentCategories as $currentCategory)
return $currentCategory;
protected function _getProductById($id)
$_product = $this->_productModel->getById($id);
return $_product;
public function getPreviousProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollectionIds = $this->_getCurrentCategory()->getProductCollection()->getAllIds();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = array_search($_currentProductId, $_productCollectionIds);
$_previousProductId = $_productCollectionIds[$_currentProductCollectionId - 1];
if ($_previousProductId)
return $_previousProductId;
return false;
return false;
public function getNextProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollectionIds = $this->_getCurrentCategory()->getProductCollection()->getAllIds();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = array_search($_currentProductId, $_productCollectionIds);
$_nextProductId = $_productCollectionIds[$_currentProductCollectionId + 1];
$_product = $this->_productModel;
if ($_nextProductId)
return $_nextProductId;
return false;
return false;
public function getPreviousProductUrl()
$_previousProductUrl = $this->_getProductById($this->getPreviousProductId())->getProductUrl();
return $_previousProductUrl;
public function getNextProductUrl()
$_nextProductUrl = $this->_getProductById($this->getNextProductId())->getProductUrl();
return $_nextProductUrl;
public function getPreviousProductName()
$_previousProductName = $this->_getProductById($this->getPreviousProductId())->getName();
return $_previousProductName;
public function getNextProductName()
$_nextProductName = $this->_getProductById($this->getNextProductId())->getName();
return $_nextProductName;
public function getPreviousProductImage()
$_previousProductImage = $this->_getProductById($this->getPreviousProductId())->getImage();
return $_previousProductImage;
public function getNextProductImage()
$_nextProductImage = $this->_getProductById($this->getNextProductId())->getImage();
return $_nextProductImage;
For displaying this on the front end you need to make a .xml file that will display the template on the product page top (in a module you would put this in vender-namespace/module-name/view/frontend/layout/catalog_product_view.xml)
<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="vender-namespace/module-nameBlockProduct" name="custom.product.pagination" template="vender-namespace/module-name/::product/product-pagination.phtml" before="-" />
</referenceContainer>
</body>
</page>
And then in your phtml file (in a module you would put this in vender-namespace/module-name/view/frontend/template/product/product-pagination.phtml)
<?php
$previousProductUrl = $block->getPreviousProductUrl();
$nextProductUrl = $block->getNextProductUrl();
$previousProductName = $block->getPreviousProductName();
$nextProductName = $block->getNextProductName();
$previousProductImage = $block->getPreviousProductImage();
$nextProductImage = $block->getNextProductImage();
$previousImage = $this->getBaseUrl() . 'pub/media/catalog/product' . $previousProductImage;
$nextImage = $this->getBaseUrl() . 'pub/media/catalog/product' . $nextProductImage;
?>
<div class="product-pagination">
<div class="previous-product" style="float: left;">
<a href="<?php echo $previousProductUrl; ?>">
<img src="<?php echo $previousImage; ?>" alt="<?php echo $previousProductName; ?>" height="75" width="75" />
<p><?php echo $previousProductName; ?></p>
<button type="submit" title="Previous Product" class="action primary to-prev-product">
<span>← prev</span>
</button>
</a>
</div>
<div class="next-product" style="float: right;">
<a href="<?php echo $nextProductUrl; ?>">
<img src="<?php echo $nextImage; ?>" alt="<?php echo $nextProductName; ?>" height="75" width="75" />
<p><?php echo $nextProductName; ?></p>
<button type="submit" title="Next Product" class="action primary to-next-product">
<span>next →</span>
</button>
</a>
</div>
</div>
Of course there is a lot more you can do with this as far as theme integration and styling. But for a proof of concept, this will get you there.
answered May 17 '16 at 17:54
circlesixcirclesix
2,94721547
2,94721547
add a comment |
add a comment |
Just improved circlesix's block code to handle first and last items in the category and name ordered navigation (instead of product ids)
namespace XXXXXXModulesBlock;
use MagentoFrameworkViewElementTemplate;
class ProductNextPrevious extends Template
public function __construct(
TemplateContext $context,
MagentoCatalogBlockProductViewabstractView $product,
MagentoCatalogApiProductRepositoryInterface $productModel,
array $data = []
)
parent::__construct($context, $data);
$this->_product = $product;
$this->_productModel = $productModel;
protected function _prepareLayout()
return parent::_prepareLayout();
protected function _getCurrentProduct()
return $this->_product->getProduct();
protected function _getCurrentCategory()
$currentCategories = $this->_getCurrentProduct()->getCategoryCollection();
foreach ($currentCategories as $currentCategory)
return $currentCategory;
protected function _getProductById($id)
$_product = $this->_productModel->getById($id);
return $_product;
public function getPreviousProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollection = $this->_getCurrentCategory()->getProductCollection()
->addAttributeToSort('name')
->getData();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = 0;
foreach ($_productCollection as $item)
if ( $item['entity_id'] == $_currentProductId )
break;
$_currentProductCollectionId ++;
if ($_currentProductCollectionId == 0 )
return false;
$_previousProductId = $_productCollection[$_currentProductCollectionId - 1]['entity_id'];
if ($_previousProductId)
return $_previousProductId;
return false;
return false;
public function getNextProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollection = $this->_getCurrentCategory()
->getProductCollection()
->addAttributeToSort('name')
->getData();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = 0;
foreach ($_productCollection as $item)
if ( $item['entity_id'] == $_currentProductId )
break;
$_currentProductCollectionId ++;
// last element
if ( $_currentProductCollectionId == ( count ( $_productCollection) - 1 ) )
return false;
$_nextProductId = $_productCollection[$_currentProductCollectionId + 1]['entity_id'];
$_product = $this->_productModel;
if ($_nextProductId)
return $_nextProductId;
return false;
return false;
public function getPreviousProductUrl()
if ( ! $productId = $this->getPreviousProductId() )
return false;
if (! $product = $this->_getProductById($productId) )
return false;
$_previousProductUrl = $product ->getProductUrl();
return $_previousProductUrl;
public function getNextProductUrl()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductUrl = $this->_getProductById($productId)->getProductUrl();
return $_nextProductUrl;
public function getPreviousProductName()
if ( ! $productId = $this->getPreviousProductId() )
return false;
$_previousProductName = $this->_getProductById($productId )->getName();
return $_previousProductName;
public function getNextProductName()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductName = $this->_getProductById($productId)->getName();
return $_nextProductName;
public function getPreviousProductImage()
if ( ! $productId = $this->getPreviousProductId() )
return false;
$_previousProductImage = $this->_getProductById($productId)->getImage();
return $_previousProductImage;
public function getNextProductImage()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductImage = $this->_getProductById($productId )->getImage();
return $_nextProductImage;
add a comment |
Just improved circlesix's block code to handle first and last items in the category and name ordered navigation (instead of product ids)
namespace XXXXXXModulesBlock;
use MagentoFrameworkViewElementTemplate;
class ProductNextPrevious extends Template
public function __construct(
TemplateContext $context,
MagentoCatalogBlockProductViewabstractView $product,
MagentoCatalogApiProductRepositoryInterface $productModel,
array $data = []
)
parent::__construct($context, $data);
$this->_product = $product;
$this->_productModel = $productModel;
protected function _prepareLayout()
return parent::_prepareLayout();
protected function _getCurrentProduct()
return $this->_product->getProduct();
protected function _getCurrentCategory()
$currentCategories = $this->_getCurrentProduct()->getCategoryCollection();
foreach ($currentCategories as $currentCategory)
return $currentCategory;
protected function _getProductById($id)
$_product = $this->_productModel->getById($id);
return $_product;
public function getPreviousProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollection = $this->_getCurrentCategory()->getProductCollection()
->addAttributeToSort('name')
->getData();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = 0;
foreach ($_productCollection as $item)
if ( $item['entity_id'] == $_currentProductId )
break;
$_currentProductCollectionId ++;
if ($_currentProductCollectionId == 0 )
return false;
$_previousProductId = $_productCollection[$_currentProductCollectionId - 1]['entity_id'];
if ($_previousProductId)
return $_previousProductId;
return false;
return false;
public function getNextProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollection = $this->_getCurrentCategory()
->getProductCollection()
->addAttributeToSort('name')
->getData();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = 0;
foreach ($_productCollection as $item)
if ( $item['entity_id'] == $_currentProductId )
break;
$_currentProductCollectionId ++;
// last element
if ( $_currentProductCollectionId == ( count ( $_productCollection) - 1 ) )
return false;
$_nextProductId = $_productCollection[$_currentProductCollectionId + 1]['entity_id'];
$_product = $this->_productModel;
if ($_nextProductId)
return $_nextProductId;
return false;
return false;
public function getPreviousProductUrl()
if ( ! $productId = $this->getPreviousProductId() )
return false;
if (! $product = $this->_getProductById($productId) )
return false;
$_previousProductUrl = $product ->getProductUrl();
return $_previousProductUrl;
public function getNextProductUrl()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductUrl = $this->_getProductById($productId)->getProductUrl();
return $_nextProductUrl;
public function getPreviousProductName()
if ( ! $productId = $this->getPreviousProductId() )
return false;
$_previousProductName = $this->_getProductById($productId )->getName();
return $_previousProductName;
public function getNextProductName()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductName = $this->_getProductById($productId)->getName();
return $_nextProductName;
public function getPreviousProductImage()
if ( ! $productId = $this->getPreviousProductId() )
return false;
$_previousProductImage = $this->_getProductById($productId)->getImage();
return $_previousProductImage;
public function getNextProductImage()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductImage = $this->_getProductById($productId )->getImage();
return $_nextProductImage;
add a comment |
Just improved circlesix's block code to handle first and last items in the category and name ordered navigation (instead of product ids)
namespace XXXXXXModulesBlock;
use MagentoFrameworkViewElementTemplate;
class ProductNextPrevious extends Template
public function __construct(
TemplateContext $context,
MagentoCatalogBlockProductViewabstractView $product,
MagentoCatalogApiProductRepositoryInterface $productModel,
array $data = []
)
parent::__construct($context, $data);
$this->_product = $product;
$this->_productModel = $productModel;
protected function _prepareLayout()
return parent::_prepareLayout();
protected function _getCurrentProduct()
return $this->_product->getProduct();
protected function _getCurrentCategory()
$currentCategories = $this->_getCurrentProduct()->getCategoryCollection();
foreach ($currentCategories as $currentCategory)
return $currentCategory;
protected function _getProductById($id)
$_product = $this->_productModel->getById($id);
return $_product;
public function getPreviousProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollection = $this->_getCurrentCategory()->getProductCollection()
->addAttributeToSort('name')
->getData();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = 0;
foreach ($_productCollection as $item)
if ( $item['entity_id'] == $_currentProductId )
break;
$_currentProductCollectionId ++;
if ($_currentProductCollectionId == 0 )
return false;
$_previousProductId = $_productCollection[$_currentProductCollectionId - 1]['entity_id'];
if ($_previousProductId)
return $_previousProductId;
return false;
return false;
public function getNextProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollection = $this->_getCurrentCategory()
->getProductCollection()
->addAttributeToSort('name')
->getData();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = 0;
foreach ($_productCollection as $item)
if ( $item['entity_id'] == $_currentProductId )
break;
$_currentProductCollectionId ++;
// last element
if ( $_currentProductCollectionId == ( count ( $_productCollection) - 1 ) )
return false;
$_nextProductId = $_productCollection[$_currentProductCollectionId + 1]['entity_id'];
$_product = $this->_productModel;
if ($_nextProductId)
return $_nextProductId;
return false;
return false;
public function getPreviousProductUrl()
if ( ! $productId = $this->getPreviousProductId() )
return false;
if (! $product = $this->_getProductById($productId) )
return false;
$_previousProductUrl = $product ->getProductUrl();
return $_previousProductUrl;
public function getNextProductUrl()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductUrl = $this->_getProductById($productId)->getProductUrl();
return $_nextProductUrl;
public function getPreviousProductName()
if ( ! $productId = $this->getPreviousProductId() )
return false;
$_previousProductName = $this->_getProductById($productId )->getName();
return $_previousProductName;
public function getNextProductName()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductName = $this->_getProductById($productId)->getName();
return $_nextProductName;
public function getPreviousProductImage()
if ( ! $productId = $this->getPreviousProductId() )
return false;
$_previousProductImage = $this->_getProductById($productId)->getImage();
return $_previousProductImage;
public function getNextProductImage()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductImage = $this->_getProductById($productId )->getImage();
return $_nextProductImage;
Just improved circlesix's block code to handle first and last items in the category and name ordered navigation (instead of product ids)
namespace XXXXXXModulesBlock;
use MagentoFrameworkViewElementTemplate;
class ProductNextPrevious extends Template
public function __construct(
TemplateContext $context,
MagentoCatalogBlockProductViewabstractView $product,
MagentoCatalogApiProductRepositoryInterface $productModel,
array $data = []
)
parent::__construct($context, $data);
$this->_product = $product;
$this->_productModel = $productModel;
protected function _prepareLayout()
return parent::_prepareLayout();
protected function _getCurrentProduct()
return $this->_product->getProduct();
protected function _getCurrentCategory()
$currentCategories = $this->_getCurrentProduct()->getCategoryCollection();
foreach ($currentCategories as $currentCategory)
return $currentCategory;
protected function _getProductById($id)
$_product = $this->_productModel->getById($id);
return $_product;
public function getPreviousProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollection = $this->_getCurrentCategory()->getProductCollection()
->addAttributeToSort('name')
->getData();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = 0;
foreach ($_productCollection as $item)
if ( $item['entity_id'] == $_currentProductId )
break;
$_currentProductCollectionId ++;
if ($_currentProductCollectionId == 0 )
return false;
$_previousProductId = $_productCollection[$_currentProductCollectionId - 1]['entity_id'];
if ($_previousProductId)
return $_previousProductId;
return false;
return false;
public function getNextProductId()
$_categoryArray = $this->_getCurrentCategory();
$_productCollection = $this->_getCurrentCategory()
->getProductCollection()
->addAttributeToSort('name')
->getData();
if ($_categoryArray)
$_currentProductId = $this->_getCurrentProduct()->getId();
$_currentProductCollectionId = 0;
foreach ($_productCollection as $item)
if ( $item['entity_id'] == $_currentProductId )
break;
$_currentProductCollectionId ++;
// last element
if ( $_currentProductCollectionId == ( count ( $_productCollection) - 1 ) )
return false;
$_nextProductId = $_productCollection[$_currentProductCollectionId + 1]['entity_id'];
$_product = $this->_productModel;
if ($_nextProductId)
return $_nextProductId;
return false;
return false;
public function getPreviousProductUrl()
if ( ! $productId = $this->getPreviousProductId() )
return false;
if (! $product = $this->_getProductById($productId) )
return false;
$_previousProductUrl = $product ->getProductUrl();
return $_previousProductUrl;
public function getNextProductUrl()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductUrl = $this->_getProductById($productId)->getProductUrl();
return $_nextProductUrl;
public function getPreviousProductName()
if ( ! $productId = $this->getPreviousProductId() )
return false;
$_previousProductName = $this->_getProductById($productId )->getName();
return $_previousProductName;
public function getNextProductName()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductName = $this->_getProductById($productId)->getName();
return $_nextProductName;
public function getPreviousProductImage()
if ( ! $productId = $this->getPreviousProductId() )
return false;
$_previousProductImage = $this->_getProductById($productId)->getImage();
return $_previousProductImage;
public function getNextProductImage()
if ( ! $productId = $this->getNextProductId() )
return false;
$_nextProductImage = $this->_getProductById($productId )->getImage();
return $_nextProductImage;
answered Feb 25 '17 at 8:53
Mike BaharMike Bahar
511
511
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f114953%2fhow-to-activate-product-navigation-in-magento-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
which version are you using? in the current 2.0 Branch this is not present: github.com/magento/magento2/blob/2.0/app/code/Magento/Catalog/…. Maybe this feature never made it into the stable release (yet?)
– David Verholen
May 11 '16 at 17:13
I am using Magento-CE-2.0.2. It has this code.
– Morison
May 11 '16 at 17:19
thats weird. This is the file in version 2.0.2 from the github repo: github.com/magento/magento2/blob/2.0.2/app/code/Magento/Catalog/…. It's still possible that there is different code in the composer repo (maybe due to a bug in the build process from magento) but its most likely that this feature did not (yet) make it into the final release.
– David Verholen
May 11 '16 at 17:25
I copy-paste the whole code from magento_2.0.2/vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml . I downloaded the Magento-CE-2.0.2+sample_data-2016-01-28-02-53-25.tar.bz2 version from Magento site.
– Morison
May 11 '16 at 17:34