Related, upsell and crosssell products in Magento 2 are the product types you can use for the marketing campaign to boost online. To the watching products, the related products are the items with the similar functions, and the upsell products are the higher version than that. How about the crosssell products? They are the accompanied products for the perfect using. In this topic, you can follow how to get all of them via the code script.
Mageplaza_HelloWorld
Mageplaza_HelloWorld
BlockYou will use a block class of the module Mageplaza_HelloWorld
, then possibly inject the object of \Magento\Framework\Registry
in the constructor of the module’s block class.
app/code/Mageplaza/HelloWorld/Block/HelloWorld.php
<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
protected $_registry;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
array $data = []
)
{
$this->_registry = $registry;
parent::__construct($context, $data);
}
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function getCurrentProduct()
{
return $this->_registry->registry('current_product');
}
}
?>
In the step 2, you will apply Object Manager
for the customize of the HelloWorld
class. By this way, allow calling the current product information, then the current product’s related, upsell and crosssell products are gotten one by one.
$myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('Mageplaza\HelloWorld\Block\HelloWorld');
$currentProduct = $myBlock->getCurrentProduct();
if ($currentProduct = $myBlock->getCurrentProduct()) {
$relatedProducts = $currentProduct->getRelatedProducts();
if (!empty($relatedProducts)) {
echo 'Related Products <br />';
foreach ($relatedProducts as $relatedProduct) {
echo $relatedProduct->getSku() . '<br />';
echo $relatedProduct->getName(); //get name
echo $relatedProduct->getPrice(); //get price
echo $relatedProduct->getData(); //Show all attributes
}
}
}
$myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('Mageplaza\HelloWorld\Block\HelloWorld');
$currentProduct = $myBlock->getCurrentProduct();
if ($currentProduct = $myBlock->getCurrentProduct()) {
$upSellProducts = $currentProduct->getUpSellProducts();
if (!empty($upSellProducts)) {
echo '<h3>UpSell Products</h3>';
foreach ($upSellProducts as $upSellProduct) {
echo $upSellProduct->getSku() . '<br />';
}
}
}
$myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('Mageplaza\HelloWorld\Block\HelloWorld');
$currentProduct = $myBlock->getCurrentProduct();
if ($currentProduct = $myBlock->getCurrentProduct()) {
$crossSellProducts = $currentProduct->getCrossSellProducts();
if (!empty($crossSellProduct)) {
echo 'CrossSell Products <br />';
foreach ($crossSellProducts as $crossSellProduct) {
echo $crossSellProduct->getSku() . '<br />';
}
}
}
If that you have any queries about the article or any questions in general, use the comment section below!
Please leave comments if you have any questions, feedbacks.