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')
3

3 Answers 3

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>
....
4
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Nov 17, 2016 at 11:45
  • @RaphaelatDigitalPianism, I completed the answer, but I don't get why down vote point here!
    – Zinat
    Nov 17, 2016 at 11:58
  • well I'm not the one who downvoted the answer so I don't know. Sometimes people downvote but don't give any reason Nov 17, 2016 at 11:58
  • @RaphaelatDigitalPianism, the link was the solution indeed, how to keep this answer up!
    – Zinat
    Nov 17, 2016 at 12:00
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.

7
  • no. You can pass this value using js and get in html. window.ProductConfig = { 'Attrvalue': '<?php echo $block->escapeHtml($status); ?>' } Nov 16, 2016 at 6:45
  • Where to place the js? Can you edit your answer with required steps. Thanks
    – Zinat
    Nov 16, 2016 at 6:48
  • for your case you need to create plugin for \Magento\Checkout\CustomerData\DefaultItem::getItemData and extend data. After that in template web/template/minicart/item/default.html new data will be accessible Nov 16, 2016 at 7:20
  • I'm almost there, do you know how can I make condition in the html file. Like if product_attribute == "mytext" then display it?
    – Zinat
    Nov 16, 2016 at 9:08
  • <!-- ko if: product_attribute --> <!-- ko --> if not work then write condition \Magento\Checkout\CustomerData\DefaultItem::getItemData to pass value or empty Nov 16, 2016 at 9:13
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.

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.