TransWikia.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!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP