0

Trying to expose a simple text custom attribute on the mini cart for an item. I see the item get its data from vendor/magento/module-checkout/CustomerData/DefaultItem.php. Looking at the doGetItemData() method. I simply add an element to the array and just for testing sake hard code a string in it like so:

protected function doGetItemData()
{
    $imageHelper = $this->imageHelper->init($this->getProductForThumbnail(), 'mini_cart_product_thumbnail');
    return [
        'options' => $this->getOptionList(),
        'qty' => $this->item->getQty() * 1,
        'item_id' => $this->item->getId(),
        'configure_url' => $this->getConfigureUrl(),
        'is_visible_in_site_visibility' => $this->item->getProduct()->isVisibleInSiteVisibility(),
        'product_name' => $this->item->getProduct()->getName(),
        'product_sku' => $this->item->getProduct()->getSku(),
        'product_url' => $this->getProductUrl(),
        'product_has_url' => $this->hasProductUrl(),
        'product_price' => $this->checkoutHelper->formatPrice($this->item->getCalculationPrice()),
        'product_price_value' => $this->item->getCalculationPrice(),
        'product_image' => [
            'src' => $imageHelper->getUrl(),
            'alt' => $imageHelper->getLabel(),
            'width' => $imageHelper->getWidth(),
            'height' => $imageHelper->getHeight(),
        ],
        'canApplyMsrp' => $this->msrpHelper->isShowBeforeOrderConfirm($this->item->getProduct())
            && $this->msrpHelper->isMinimalPriceLessMsrp($this->item->getProduct()),
        'product_custom_attribute' => 'testing this here'
    ];
}

After clearing cache and going to the frontend and refresh the page, in the console I use localStorage.getItem('mage-cache-storage') to see what values ko pulls from, but the new attribute is never there. Any idea why this is not coming through?

EDIT: I got this kind of working, but have a bigger issue. The attribute is not coming through, but is being set the same for all products in the cart even if the attribute is empty for one of the products. So if one product have the attribute with valid text, all the others items in the cart object have that value. Any ideas? This is my current doGetItemData code:

        $imageHelper = $this->imageHelper->init($this->getProductForThumbnail(), 'mini_cart_product_thumbnail');
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $this->product = $objectManager->get('Magento\Catalog\Model\Product')->load($this->item->getProduct()->getId());


    return [
        'options' => $this->getOptionList(),
        'qty' => $this->item->getQty() * 1,
        'item_id' => $this->item->getId(),
        'configure_url' => $this->getConfigureUrl(),
        'is_visible_in_site_visibility' => $this->item->getProduct()->isVisibleInSiteVisibility(),
        'product_name' => $this->item->getProduct()->getName(),
        'product_sku' => $this->item->getProduct()->getSku(),
        'product_url' => $this->getProductUrl(),
        'product_has_url' => $this->hasProductUrl(),
        'product_price' => $this->checkoutHelper->formatPrice($this->item->getCalculationPrice()),
        'product_price_value' => $this->item->getCalculationPrice(),
        'product_image' => [
            'src' => $imageHelper->getUrl(),
            'alt' => $imageHelper->getLabel(),
            'width' => $imageHelper->getWidth(),
            'height' => $imageHelper->getHeight(),
        ],
        'canApplyMsrp' => $this->msrpHelper->isShowBeforeOrderConfirm($this->item->getProduct())
            && $this->msrpHelper->isMinimalPriceLessMsrp($this->item->getProduct()),
        'product_custom_attribute' => $this->product->getCustomAttribute()
    ];
  • did you do a re-index? it might be more complex then that, but that would be the first thing to look at as it is required after creating a new attribute. – circlesix Aug 18 '16 at 20:43
1

I had the same needs and have solved it here:

Getting the product SKU in the header mini-cart

I needed the product SKU, but you should be able to use this method to pull in any product info.

  • I referenced that. The first step I took was I edited the DefaultItem class directly before overriding it just to get my logic right. But still never shows when I log out the localStorage object. – sudopratt Aug 17 '16 at 22:10

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.