0

Magento 2.4.6

I added a custom product attribute next to the qty in the checkout summary following the tutorial on Add custom product attribute to checkout summary Magento 2.

That works until the second checkout step is opened, as soon as step 2 is loaded, the value disappears. Reloading makes it visible for 1-2 seconds until it disappears again. Step 1 has the same problem now.

The line in details.html looks like that:

<span class="product-item-unit" data-bind="text: $parent.unit"></span>

Any idea whats missing to make it stay?

1 Answer 1

1

Update ConfigProviderPlugin.php

<?php namespace Abbottstore\Flavor\Plugin;
  use Magento\Checkout\Model\Session as CheckoutSession;
  use Magento\Quote\Api\CartItemRepositoryInterface as QuoteItemRepository;
class ConfigProviderPlugin extends \Magento\Framework\Model\AbstractModel
{
private $checkoutSession;
private $quoteItemRepository;
protected $scopeConfig;

public function __construct(
    CheckoutSession $checkoutSession,
    QuoteItemRepository $quoteItemRepository,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
    $this->_scopeConfig = $scopeConfig;
    $this->checkoutSession = $checkoutSession;
    $this->quoteItemRepository = $quoteItemRepository;
}


public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, array $result)
{
        $quoteId = $this->checkoutSession->getQuote()->getId();            
        if ($quoteId) {            
            $itemOptionCount = count($result['totalsData']['items']);
            $quoteItems = $this->quoteItemRepository->getList($quoteId);
            $isbnOptions = array();
            foreach ($quoteItems as $index => $quoteItem) {
                $quoteItemId = $quoteItem['item_id'];
                $isbnOptions[$quoteItemId] = $quoteItem['isbn'];               
            }

            for ($i=0; $i < $itemOptionCount; $i++) {
            $quoteParentId = $result['totalsData']['items'][$i]['item_id'];
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $productId = $result['quoteItemData'][$i]['product']['entity_id'];
            $productObj = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);

            $productFlavours = $productObj->getResource()->getAttribute('flavors')->getFrontend()->getValue($productObj);         
            if($productFlavours == 'No' || $productFlavours == 'NA'){
               $productFlavours = '';
            }
            $result['quoteItemData'][$i]['flavor'] = $productFlavours;
            json_encode($result);
            }
    }
    return $result;
    }

  }

Override details.js (vendor/magento/module-checkout/view/frontend/web/js/view/summary/item/details.js) and write this code.

  define(
  [
'uiComponent'
 ],
 function (Component) {
"use strict";
var quoteItemData = window.checkoutConfig.quoteItemData;
return Component.extend({
    defaults: {
        template: 'Magento_Checkout/summary/item/details'
    },
    quoteItemData: quoteItemData,
    getValue: function(quoteItem) {
        return quoteItem.name;
    },
    getItemFlavor: function(quoteItem) {
        var itemProduct = this.getItemProduct(quoteItem.item_id);
        return itemProduct.flavor;
    },
    getItemProduct: function(item_id) {
        var itemElement = null;
        _.each(this.quoteItemData, function(element, index) {
            if (element.item_id == item_id) {
                itemElement = element;
            }
        });
        return itemElement;
    }

});
 }

 );

and in details.html add this line

  <span class="product-item-new" data-bind="text: getItemFlavor($parent)"></span>

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.