AnswerBun.com

How to get custom option price in cart in Magento 2

Magento Asked by Jackridder on December 3, 2020

I have some customizable options in my products. I would like the checkout/cart page to show the individual prices of each option and not just the total price. Unfortunately I am missing any approach. Similar to the picture of a question from 6 years ago:
enter image description here

But I use Magento 2.3.4 and can’t get the old code to work.

Update/Edit 2:

I have been able to collect quite a few things so far, but unfortunately I still cannot get the price. The price is 0.00 or is empty. What do I have to do to see the price of each option?
Here is my code, which gives me all options but not the prices:

<?php
/**
 * @var LearningJsBlockCustomOption $block
 */

$_objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product = $_objectManager->get('MagentoCatalogModelProduct')->load(3);
$customOptions = $_objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($product);
$productTypeInstance = $_objectManager->get('MagentoConfigurableProductModelProductTypeConfigurable');
$productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);
foreach($customOptions as $option) {
    $values = $option->getValues();
    if (empty($values)) { 
        continue;
    }

    foreach($values as $value) {
        echo $value->getTitle();
    }

}

One Answer

In overrided by your module file:

vendor/magento/module-checkout/Block/Cart/Item/Renderer.php

add new method to get full product data by its id:

/**
 * Get item product
 *
 * @return MagentoCatalogModelProductRepository
 * @codeCoverageIgnore
 */
public function getProductFull($id)
{
   return $this->productRepository->getById($id);
}

Edit app/design/frontend/Module/Theme/Magento_Checkout/templates/cart/item/default.phtml

At beginning add newly created function to get full product data for example:

//ChrisG added method in MagentoCheckoutBlockCartItemRenderer
$productFull = $this->getProductFull($product->getId());

and after line: ~48:

<dl class="item-options">
   <?php foreach ($_options as $_option) : ?>
      <?php //ChrisG Add price to option

        $optionId = $_option['option_id'];
         $option = $productFull->getOptionById($optionId);
         $itemOption = $_item->getOptionByCode('option_' . $option->getId());
         $price = false;
         $optionPrice = "";
         if($_option['option_type'] != 'area') {
            foreach ($option->getValues() as $values) {
               if ($values->getId() == $itemOption['value']) {                         
                  $price = $values['price'];
               }
            }
            if ($price !== false && $price > 0) {
               $optionPrice = ", ". $this->convertPrice($price, true); 
            }
         } ?>
         <?php $_formatedOptionValue = $block->getFormatedOptionValue($_option) ?>
            <dt><?= $block->escapeHtml($_option['label']) ?></dt>
            <dd>
            <?php if (isset($_formatedOptionValue['full_view'])): ?>
               <?= /* @escapeNotVerified */ $_formatedOptionValue['full_view'] . $optionPrice; ?>
            <?php else: ?>
               <?= $block->escapeHtml($_formatedOptionValue['value'], ['span']) . $optionPrice; ?>
            <?php endif; ?>
            </dd>
            <?php //ChrisG Add price to option ends ?>
                            
    <?php endforeach; ?>
</dl>

Custom options are based on module: itoris/dynamic-product-options

Tested on magento 2.3.2, good luck.

Answered by ChrisGru on December 3, 2020

Add your own answers!

Related Questions

Magento 2 cache_invalidate loop

2  Asked on January 29, 2021 by tjitse

     

Magento 2.2 ssh commands not executed

1  Asked on January 29, 2021 by jilco-tigchelaar

   

How to add new or customize existing top header links in Magento?

4  Asked on January 26, 2021 by tallal-hassan

     

Upload SVG files as product image

0  Asked on January 26, 2021 by francesco-terreni

         

Add custom parameter in order’s item in rest api magento 2

0  Asked on January 25, 2021 by vijay-gupta

 

Special Price suddenly not showing

1  Asked on January 23, 2021 by dickson

 

How to activate product navigation in Magento 2?

2  Asked on January 22, 2021 by morison

     

Quote with no items – add to cart error

1  Asked on January 22, 2021 by fra

     

Magento 2 get cart items in block class

1  Asked on January 20, 2021 by khooshbu-patel

   

Magento custom option text field max length

1  Asked on January 19, 2021 by k-i

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir