3

How can I access product attribute to echo it in minicart /web/template/minicart/item/default.html Like below works for cart page templates/cart/item/default.phtml

<?php
$product = $_item->getProduct();
$product_id = $product->getId();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);
?>

...

echo $_product->getData('myattr')
Zinat
  • 2,069
  • 2
  • 28
  • 52

3 Answers3

7

I found the answer to my question in this link

DefaultItem.php

<?php

namespace Your\Module\Plugin;


use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Quote\Model\Quote\Item;

class DefaultItem
{

    protected $productRepo;

    public function __construct(ProductRepositoryInterface $productRepository)
    {
        $this->productRepo = $productRepository;
    }

    public function aroundGetItemData($subject, \Closure $proceed, Item $item)
    {
        $data = $proceed($item);

        /** @var Product $product */
        $product = $this->productRepo->getById($item->getProduct()->getId());
        $attributes = $product->getAttributes();

        $atts = [
            "product_manufacturer" => $attributes['manufacturer']->getFrontend()->getValue($product),
            "product_part_number" => $attributes['part_number']->getFrontend()->getValue($product)
        ];

        return array_merge($data, $atts);
    }
}

And add below in default.html to echo custom attributes:

...
<div data-bind="html: 'SKU#: ' + item.product_sku"></div>
<div data-bind="html: 'Manufacturer: ' + item.product_manufacturer"></div>
<div data-bind="html: 'Part #: ' + item.product_part_number"></div>
....
Zinat
  • 2,069
  • 2
  • 28
  • 52
3

You can use this

$_attributeValue = $_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);

For your case you need to create plugin for \Magento\Checkout\CustomerData\DefaultItem::getItemData and extend data.

public function getItemData(Item $item)
{
    $this->item = $item;
    return \array_merge(
        ['product_type' => $item->getProductType()],
        ['product_attribute' => $attributeValue]
        $this->doGetItemData()
    );
}

After that in template web/template/minicart/item/default.html new data will be accessible

 <strong class="product-item-name">
            <!-- ko if: product_has_url -->
            <a data-bind="attr: {href: product_url}, text: product_name"></a>
            <!-- /ko -->
            <!-- ko ifnot: product_has_url -->
                <!-- ko text: product_name --><!-- /ko -->
            <!-- /ko -->
            <label data-bind="text: product_attribute"></label>
        </strong>

refer this link for which file you have to override or this link for where you have to add product attribute value.

May be it will work.

Chirag Prajapati
  • 2,922
  • 2
  • 18
  • 42
0

In html, we can apply condition like below code:

 <if args="product_attribute == 'mytext'">
    here is your content like div 
</if>

Its Magento 2 code not knockout js, its working fine for apply conditions.

Surinder Kaur
  • 86
  • 1
  • 4