Magento 2 - How to remove the price of each bundle product option in the shopping cart, order confirmation, review your cart page, etc

magento.stackexchange https://magento.stackexchange.com/questions/319652

  •  14-04-2021
  •  | 
  •  

Question

I would like to remove the price from the bundle product options in the entire site. The place I have noticed that get displayed are:

  • Shopping Cart
  • Review Your Cart Page
  • Mini Cart
  • Confirmation Emails

enter image description here

Things I have tried: I created a new module and overrode \Magento\Bundle\Helper\Catalog\Product\Configuration:

ZEndertech\BundleExtended\Helper\Catalog\Product\Configuration.php

    <?php

namespace ZEndertech\BundleExtended\Helper\Catalog\Product;

use Magento\Catalog\Helper\Product\Configuration\ConfigurationInterface;
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
use Magento\Framework\App\Helper\AbstractHelper;

use \Magento\Bundle\Helper\Catalog\Product\Configuration as BaseConfiguration;

class Configuration extends BaseConfiguration
{
    /**
     * Get bundled selections (slections-products collection)
     *
     * Returns array of options objects.
     * Each option object will contain array of selections objects
     *
     * @param ItemInterface $item
     * @return array
     */
    public function getBundleOptions(ItemInterface $item)
    {
        $options = [];
        $product = $item->getProduct();

        /** @var \Magento\Bundle\Model\Product\Type $typeInstance */
        $typeInstance = $product->getTypeInstance();

        // get bundle options
        $optionsQuoteItemOption = $item->getOptionByCode('bundle_option_ids');
        $bundleOptionsIds = $optionsQuoteItemOption ? unserialize($optionsQuoteItemOption->getValue()) : [];
        if ($bundleOptionsIds) {
            /** @var \Magento\Bundle\Model\ResourceModel\Option\Collection $optionsCollection */
            $optionsCollection = $typeInstance->getOptionsByIds($bundleOptionsIds, $product);

            // get and add bundle selections collection
            $selectionsQuoteItemOption = $item->getOptionByCode('bundle_selection_ids');

            $bundleSelectionIds = unserialize($selectionsQuoteItemOption->getValue());

            if (!empty($bundleSelectionIds)) {
                $selectionsCollection = $typeInstance->getSelectionsByIds($bundleSelectionIds, $product);

                $bundleOptions = $optionsCollection->appendSelections($selectionsCollection, true);
                foreach ($bundleOptions as $bundleOption) {
                    if ($bundleOption->getSelections()) {
                        $option = ['label' => $bundleOption->getTitle(), 'value' => []];

                        $bundleSelections = $bundleOption->getSelections();

                        foreach ($bundleSelections as $bundleSelection) {
                            $qty = $this->getSelectionQty($product, $bundleSelection->getSelectionId()) * 1;
                            if ($qty) {
                                $option['value'][] = $qty . ' x '
                                    . $this->escaper->escapeHtml($bundleSelection->getName());
                            }
                        }

                        if ($option['value']) {
                            $options[] = $option;
                        }
                    }
                }
            }
        }

        return $options;
    }

}

I cleared the cache, but the price still appears next to the bundle options.

Was it helpful?

Solution

In alignment with what @tjons pointed out, in my di.xml for the module I created I needed to add a new entry to list the preference to pick up my Configuration.php file over the core one. After that I ran php bin/magento setup:upgrade command, reloaded the page and voila!

The di.xml file is the one below:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Bundle\Helper\Catalog\Product\Configuration" type="ZEndertech\BundleExtended\Helper\Catalog\Product\Configuration"/>
</config>

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top