Hi
In a Bundle Product under Advanced Pricing, you can choose Price View.
Here you can choose between "Price Range" and "As Low As".
I would like to make our own "As High As"
Since it can make a "Price Range" it knows the highest price.
Is this possible?
Hello @kisobrewola822,
Yes, it is possible to add a custom "As High As" option to the Price View for bundle products in Magento. This will require custom module development where you will extend the functionality of the bundle product pricing to include a new option for "As High As."
Here is the code to achieve this:
1. Create the Custom Module Structure
First, create the directory structure for your custom module:
app/code/YourVendor/ YourModule
2. Module Registration
Create the registration.php file:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'YourVendor_YourModule ', __DIR__ );
3. Module Declaration
Create the etc/module.xml file:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="YourVendor_YourModule" setup_version="1.0.0"> <sequence> <module name="Magento_Bundle"/> </sequence> </module> </config>
4. Dependency Injection
Create a di.xml file in etc/ to override the default Bundle Attribute Price View model with your custom one:
<?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\Model\Product\Attribute\Source\Price\View" type=" YourVendor\YourModule\Model\Product\Attribute\Source\Price\View"/> </config>
5. Extend the Bundle Attribute Pricing View Model
Create YourVendor\YourModule\Model\Product\Attribute\Source\Price\View.php:
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace YourVendor\YourModule\Model\Product\Attribute\Source\Price; use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory; use Magento\Framework\DB\Ddl\Table; /** * Bundle Price View Attribute Renderer * * @api * @since 100.0.2 */ class View extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource { /** * @var OptionFactory */ protected $optionFactory; /** * @param OptionFactory $optionFactory */ public function __construct(OptionFactory $optionFactory) { $this->optionFactory = $optionFactory; } /** * Get all options * * @return array */ public function getAllOptions() { if (null === $this->_options) { $this->_options = [ ['label' => __('Price Range'), 'value' => 0], ['label' => __('As Low as'), 'value' => 1], ['label' => __('As High as'), 'value' => 2], ]; } return $this->_options; } /** * Get a text for option value * * @param string|integer $value * @return string|bool */ public function getOptionText($value) { foreach ($this->getAllOptions() as $option) { if ($option['value'] == $value) { return $option['label']; } } return false; } /** * Retrieve flat column definition * * @return array */ public function getFlatColumns() { $attributeCode = $this->getAttribute()->getAttributeCode(); return [ $attributeCode => [ 'unsigned' => false, 'default' => null, 'extra' => null, 'type' => Table::TYPE_INTEGER, 'nullable' => true, 'comment' => 'Bundle Price View ' . $attributeCode . ' column', ], ]; } /** * Retrieve Select for update Attribute value in flat table * * @param int $store * @return \Magento\Framework\DB\Select|null */ public function getFlatUpdateSelect($store) { return $this->optionFactory->create()->getFlatUpdateSelect($this->getAttribute(), $store, false); } }
6. Create a Layout XML File to Reference the Custom Template
Create the directory structure if it doesn't already exist:
app/code/YourVendor/YourModule/view/frontend/layout/
Then, create or modify the layout XML file, e.g., catalog_product_view.xml:
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="product.info.bundle.price"> <action method="setTemplate"> <argument name="template" xsi:type="string">YourVendor_YourModule::product/price/final_price.phtml</argument> </action> </referenceBlock> </body> </page>
7. create template file at app/code/YourVendor/YourModule/view/frontend/templates/product/price/final_price.phtml:
<?php $idSuffix = $block->getIdSuffix() ? $block->getIdSuffix() : ''; /** @var \Magento\Bundle\Pricing\Render\FinalPriceBox $block */ /** @var \Magento\Bundle\Pricing\Price\FinalPrice $finalPriceModel */ $finalPriceModel = $block->getPrice(); $minimalPrice = $finalPriceModel->getMinimalPrice(); $maximalPrice = $finalPriceModel->getMaximalPrice(); /** ex: \Magento\Bundle\Pricing\Price\BundleRegularPrice */ /** @var \Magento\Framework\Pricing\Price\PriceInterface $regularPriceModel */ $regularPriceModel = $block->getPriceType('regular_price'); $maximalRegularPrice = $regularPriceModel->getMaximalPrice(); $minimalRegularPrice = $regularPriceModel->getMinimalPrice(); $regularPriceAttributes = [ 'display_label' => __('Regular Price'), 'price_id' => $block->getPriceId('old-price-' . $idSuffix), 'include_container' => true, 'skip_adjustments' => true ]; $renderMinimalRegularPrice = $block->renderAmount($minimalRegularPrice, $regularPriceAttributes); $renderMaximalRegularPrice = $block->renderAmount($maximalRegularPrice, $regularPriceAttributes); ?> <?php if ($block->getSaleableItem()->getPriceView() == 1) : ?> <p class="minimal-price"> <?= /* @noEscape */ $block->renderAmount($minimalPrice, [ 'display_label' => __('As low as'), 'price_id' => $block->getPriceId('from-'), 'include_container' => true ]); ?> <span class="old-price"> <?= /* @noEscape */ $renderMinimalRegularPrice ?> </span> </p> <?php elseif ($block->getSaleableItem()->getPriceView() == 2) : ?> <p class="minimal-price"> <?= /* @noEscape */ $block->renderAmount($maximalPrice, [ 'display_label' => __('As high as'), 'price_id' => $block->getPriceId('from-'), 'include_container' => true ]); ?> <span class="old-price"> <?= /* @noEscape */ $renderMaximalRegularPrice ?> </span> </p> <?php else : ?> <?php if ($block->showRangePrice()) : ?> <p class="price-from"> <?= /* @noEscape */ $block->renderAmount($minimalPrice, [ 'display_label' => __('From'), 'price_id' => $block->getPriceId('from-'), 'price_type' => 'minPrice', 'include_container' => true ]); ?> <?php if ($minimalPrice < $minimalRegularPrice) : ?> <span class="old-price"> <?= /* @noEscape */ $renderMinimalRegularPrice ?> </span> <?php endif ?> </p> <p class="price-to"> <?= /* @noEscape */ $block->renderAmount($maximalPrice, [ 'display_label' => __('To'), 'price_id' => $block->getPriceId('to-'), 'price_type' => 'maxPrice', 'include_container' => true ]); ?> <?php if ($maximalPrice < $maximalRegularPrice) : ?> <span class="old-price"> <?= /* @noEscape */ $renderMaximalRegularPrice ?> </span> <?php endif ?> </p> <?php else : ?> <?= /* @noEscape */ $block->renderAmount($minimalPrice, [ 'price_id' => $block->getPriceId('product-price-'), 'include_container' => true ]); ?> <?php if ($minimalPrice < $minimalRegularPrice) : ?> <span class="old-price"> <?= /* @noEscape */ $renderMinimalRegularPrice ?> </span> <?php endif ?> <?php endif ?> <?php endif ?>
So after this you will find new option at advance pricing price view named As High as and will show the highest value.
If the issue will be resolved, Click Kudos & Accept as a Solution.