0

I load some related products inside a foreach and want to call some attribute values of these individual products.

My current code below works fine with using $objectManager, but that's not the best way to handle this.

How can I get the value of the product attribute, without using the $objectManager ?

Code Helper/Data:

<?php
namespace MyModule\LinkedProducts\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function __construct(
        \MagePal\LinkProduct\Model\Accessory $accessory,
        \Magento\Catalog\Block\Product\AbstractProduct $abstractProduct
    ) {
        $this->accessory = $accessory;
        $this->abstractProduct = $abstractProduct;
    }

    public function getLinkedProducts()
    {
    $product = $this->abstractProduct->getProduct();    
    return $this->accessory->getAccessoryProducts($product);
    }

}

Code phtml:

<?php $linkedproducts = $this->helper('MyModule\LinkedProducts\Helper\Data');
$relatedProducts = $linkedproducts->getLinkedProducts();?>

<?php foreach ($relatedProducts as $relatedProduct) {

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $objectManager->create('Magento\Catalog\Model\Product')->load($relatedProduct->getId());
echo $_product->getData('my_attribute_value');
}?>
1

You can add one more function in your helper class to load product info and call same function in PHTML.

Updated code will be like as below:

<?php
namespace MyModule\LinkedProducts\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function __construct(
    \MagePal\LinkProduct\Model\Accessory $accessory,
    \Magento\Catalog\Block\Product\AbstractProduct $abstractProduct,
    \Magento\Catalog\Model\ProductFactory $productFactory
) {
    $this->accessory = $accessory;
    $this->abstractProduct = $abstractProduct;
    $this->productFactory = $productFactory;
}

public function getLinkedProducts()
{
    $product = $this->abstractProduct->getProduct();    
    return $this->accessory->getAccessoryProducts($product);
}

public function getProductInfo($productId)
{
    $_product = $this->productFactory->create()->load($productId);
    return $_product;
}    

}

PHTML Code:

<?php 
$linkedproducts = $this->helper('MyModule\LinkedProducts\Helper\Data');
$relatedProducts = $linkedproducts->getLinkedProducts();?>

<?php 
    foreach ($relatedProducts as $relatedProduct) {

        $_product = $linkedproducts->getProductInfo($relatedProduct->getId());
        echo $_product->getData('my_attribute_value');
    }
?>
  • Thanks a lot for your help. But it seems that something is wrong in the code. I get a blank page when editing the helper data. Are we missing something? – JGeer Feb 26 '20 at 8:37
  • By uncomment ini_set('display_errors', 1); from bootstrap.php file .. you can see error instead of blank page – Pankaj Pareek Feb 26 '20 at 8:43
  • 1
    Thanks a lot, was something small as adding a ; after the return $_product. I edit your answer, so that we have the correct code! :) – JGeer Feb 26 '20 at 8:52
  • It seems not working 100% well. The foreach is not working when loading the echo $_product->getData('my_attribute_value');. It only loads the value of the first product and displays that value for all other products as well. What are we missing here? – JGeer Feb 26 '20 at 10:10
  • 1
    I have updated the code. Can you please check – Pankaj Pareek Feb 26 '20 at 11:11
0

You can do this by add new function in your helper and call in phtml.

Helper File :

<?php
namespace MyModule\LinkedProducts\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    private $accessory;
    private $abstractProduct;
    private $productFactory;
    public function __construct(
        \MagePal\LinkProduct\Model\Accessory $accessory,
        \Magento\Catalog\Block\Product\AbstractProduct $abstractProduct,
        \Magento\Catalog\Model\ProductFactory $productFactory
    ) {
        $this->accessory = $accessory;
        $this->abstractProduct = $abstractProduct;
        $this->productFactory = $productFactory;
    }

    public function getLinkedProducts()
    {
        $product = $this->abstractProduct->getProduct();    
        return $this->accessory->getAccessoryProducts($product);
    }

    public function getAttributeInfo($productId = null)
    {
        if($productId){
            $productObj = $this->productFactory->create()->load($productId);
            return $productObj->getData('my_attribute_value');
        }
        return '';
    }

}

Phtml file :

<?php

$linkedproducts = $this->helper('MyModule\LinkedProducts\Helper\Data');
$relatedProducts = $linkedproducts->getLinkedProducts();

foreach ($relatedProducts as $relatedProduct) {
    echo $linkedproducts->getAttributeInfo($relatedProduct->getId());
}
?>

Remove generated and clean cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.