0

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 \Learning\Js\Block\CustomOption $block
 */

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $_objectManager->get('Magento\Catalog\Model\Product')->load(3);
$customOptions = $_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);
$productTypeInstance = $_objectManager->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable');
$productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);
foreach($customOptions as $option) {
    $values = $option->getValues();
    if (empty($values)) { 
        continue;
    }

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

}
0

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 \Magento\Catalog\Model\ProductRepository
 * @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 Magento\Checkout\Block\Cart\Item\Renderer
$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.

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.