0

I added a new attribute to a product. The attribute is named "is_brand_new". The product shows in the catalog product view in the upselling and related slider.

But the new attribute is not in the product data. I even cleared the cache

I outputted all the data from the item

app\design\frontend\company\base\Magento_Catalog\templates\product\list\items.phtml:

if ($_item->getData("name") == "t-shirt") {
    var_dump($_item->getData());
}

But it does not contain the data for the attribute is_brand_new.

What I try to do is show an icon if the product is marked as brandnew. So I added a new checkbox attribute in the backend. Now I try to read it out and show the icon if the checkbox is enabled.

2 Answers 2

0

It looks like the block classes prepareData method is only loading a few attributes. So I had to create a helper, load the full product via the ID, then I was able to load the attribute.

Helper:

use \Magento\Catalog\Model\ProductRepository;

...

public function getProductByIdAndReturnData($id, $key)
{
    $product = $this->getProductById($id);
    if (empty($product)) { return $this; }

    $data = $product->getData($key);
    return $data;
}

public function getProductById($id)
{
    return $this->_productRepository->getById($id);
}

Template:

$isProductBrandNew = $productHelper->getProductByIdAndReturnData($id, "is_brand_new");
0

If you setup thr product attribute to be visible in front from the backend as per the screenshot below, the attribute should appear.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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