cancel
Showing results for 
Search instead for 
Did you mean: 

How to get custom attribute in checkout summary (magento 2.3)

How to get custom attribute in checkout summary (magento 2.3)

I am struggling with this problem for days. 

For example. I have three products in cart, when I go to checkout section page, then I want to add some custom attributes for those products in checkout summary table. I have 'alternate price currency' attribute set which I want to add to summary. The problem is that for each product in checkout I am getting same values for that custom attribute.

Product 1 have custom attribute and it needs to be shown.

Product 2 doesnt have that attribute so it wont show in summary.


Result

Product 1 - custom attribute shown and value is 25 - CORRECT!

Product 2 - same as Product 1 - INCORRECT! - Nothing should be shown as this product doesnt have any values set to custom attribute in admin.

 

How I am fetching product?

1. $product->load($quote->getProductId());  = all products fetched in that way have custom attribute with value set.

2. $product = $quote->getProduct();  = all products fetched in that way doesnt have custom attribute value set


Problem is that for Product 2, custom attribute is shown and it is same like in Product 1.

When I inspect those elements with xdebug it says that both have that attribute with same values. If I go to admin backend and find that product 2, fields for that attribute are not set they are empty. If I have 10 products in cart, all will have that attribute with same value!

In admin backend, custom attribute values for Product 2 are not set as I said.

 

My code:

 

public function afterGetConfig(DefaultConfigProvider $subject, array $result)
    {
        $items = $result['totalsData']['items'];
        $allVendorProducts = true;
        $priceVendorTotal = 0;
        foreach ($items as $index => $item) {
            $quote = $this->checkoutSession->getQuote()->getItemById($item['item_id']);
            $quantity = $quote->getQty();
            //$productItem = $quote->getProduct();
            $productItem = $this->product->load($quote->getProductId());
            $productAttr = $productItem->getResource()>getAttribute('fl_el_alternate_price')->getFrontend()->getValue($productItem);

        if ($productAttr != false || $productAttr != null) {
            $result['quoteItemData'][$index]['detailSubtotal'] = $productAttr;
            $priceVendorTotal = $priceVendorTotal+($vendorPrice*$quantity);

        } else {
            $allVendorProducts = false    
        }

        //When all products have fl_el_alternate price set
        if ($allVendorProducts == true) {
            $result['totalsData']['hasAllVendors'] = true;
            //Set checkout summary totals value
            $result['totalsData']['subTotalPrice'] = $priceVendorTotal;
            $result['totalsData']['grandTotalPrice'] = $priceVendorTotal;
        } else {
            $result['totalsData']['hasAllVendors'] = false;
        }

        return $result;
    }