12

In view page by default magento displaying lowest price of associated products.

I need to display highest price of associated products. Any one have idea where the logic is reside.how to customize this behavior.

update:

Magento\ConfigurableProduct\Pricing\Price\ConfigurablePriceResolver

/**
     * @param \Magento\Framework\Pricing\SaleableInterface|\Magento\Catalog\Model\Product $product
     * @return float
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product)
    {
        $price = null;
        foreach ($this->configurable->getUsedProducts($product) as $subProduct) {
            $productPrice = $this->priceResolver->resolvePrice($subProduct);
            $price = $price ? min($price, $productPrice) : $productPrice;
        }
        if (!$price) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('Configurable product "%1" do not have sub-products', $product->getName())
            );
        }
        return (float)$price;
    }

i'm trying to override this core file,but its not working.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <preference for="Magento\ConfigurableProduct\Pricing\Price\ConfigurablePriceResolver" type="Kensium\Catalog\Pricing\Price\ConfigurablePriceResolver" />

<?php
namespace Kensium\Catalog\Pricing\Price;
class ConfigurablePriceResolver extends \Magento\ConfigurableProduct\Pricing\Price\ConfigurablePriceResolver
{
    /**
     * @param \Magento\Framework\Pricing\SaleableInterface|\Magento\Catalog\Model\Product $product
     * @return float
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product)
    {
        $price = null;       
        $assPrice=array();
        foreach ($this->configurable->getUsedProducts($product) as $subProduct) {
            $productPrice = $this->priceResolver->resolvePrice($subProduct);
            $assPrice[]=$productPrice;
            $price = $price ? min($price, $productPrice) : $productPrice;
        }
        if (!$price) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('Configurable product "%1" do not have sub-products', $product->getName())
            );
        }
        return (float)(max($assPrice));
        //return (float)$price;
    }
}
7
  • have you want to show max price in details page? Sep 13 '16 at 9:27
  • yes in detail & listing also.when they change options at that time as usual.
    – sivakumar
    Sep 13 '16 at 9:29
  • in listing price are not changed, Have you checked, only one price is display Sep 13 '16 at 9:31
  • that is fine.customer should see max price of configurable product.
    – sivakumar
    Sep 13 '16 at 9:34
  • is this working for you? i have given example below for your requiements Sep 13 '16 at 13:29
17
+100

You have to make plugin for that to display max price inside detail page, Below is step by step module for your need,

Filepath, app/code/Vendor/Modulename/

Registration file, app/code/Vendor/Modulename/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Modulename',
    __DIR__
);

app/code/Vendor/Modulename/etc/module.xml

<?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="Vendor_Modulename" setup_version="2.0.0">
    </module>
</config>

app/code/Vendor/Modulename/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\ConfigurableProduct\Pricing\Price\ConfigurablePriceResolver">
            <plugin name="pricemaxindetail" type="Vendor\Modulename\Pricing\ConfigurablePrice"/>
    </type>
</config>

app/code/Vendor/Modulename/Pricing/ConfigurablePrice.php

Inside this file, you have to pluginize resolveprice() function

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Vendor\Modulename\Pricing;

class ConfigurablePrice
{
    protected $_moduleManager;
    protected $_jsonEncoder;
    protected $_registry;


    public function __construct(
        \Magento\Framework\Module\Manager $moduleManager,
        \Magento\Framework\Json\EncoderInterface $jsonEncoder,
        \Magento\Framework\Registry $registry,           
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,         
        \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory,    
        \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurableType,
        \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
        \Magento\CatalogInventory\Api\StockStateInterface $stockState,
        array $data = [] 
    )
    {
        $this->_moduleManager = $moduleManager;
        $this->_jsonEncoder = $jsonEncoder;
        $this->_registry = $registry;
        $this->productFactory = $productFactory;      
        $this->productRepository = $productRepository;       
        $this->_configurableType = $configurableType;        
        $this->dataObjectHelper = $dataObjectHelper;   
        $this->stockState = $stockState; 
    }

    /**
     * @param \Magento\Framework\Pricing\SaleableInterface|\Magento\Catalog\Model\Product $product
     * @return float
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function aroundResolvePrice($subject, \Closure $proceed,\Magento\Framework\Pricing\SaleableInterface $product)
    {
        $price = null; 
        //get parent product id      
        $parentId = $product['entity_id'];
        $childObj = $this->getChildProductObj($parentId);
        foreach($childObj as $childs){
            $productPrice = $childs->getPrice();
            $price = $price ? max($price, $productPrice) : $productPrice;
        }
        return $price;        
        //return (float)$proceed($product['entity_id']);
    }

     public function getProductInfo($id){    
        //get product obj using api repository...          
        if(is_numeric($id)){           
            return $this->productRepository->getById($id); 
        }else{
            return;
        } 
    }

    public function getChildProductObj($id){
        $product = $this->getProductInfo($id);
        //if quote with not proper id then return null and exit;
        if(!isset($product)){
            return;
        }

        if ($product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
            return [];
        }

        $storeId = 1;//$this->_storeManager->getStore()->getId();
        $productTypeInstance = $product->getTypeInstance();
        $productTypeInstance->setStoreFilter($storeId, $product);
        $childrenList = [];       

        foreach ($productTypeInstance->getUsedProducts($product) as $child) {
            $attributes = [];
            $isSaleable = $child->isSaleable();

            //get only in stock product info
            if($isSaleable){
                foreach ($child->getAttributes() as $attribute) {
                    $attrCode = $attribute->getAttributeCode();
                    $value = $child->getDataUsingMethod($attrCode) ?: $child->getData($attrCode);
                    if (null !== $value && $attrCode != 'entity_id') {
                        $attributes[$attrCode] = $value;
                    }
                }

                $attributes['store_id'] = $child->getStoreId();
                $attributes['id'] = $child->getId();
                /** @var \Magento\Catalog\Api\Data\ProductInterface $productDataObject */
                $productDataObject = $this->productFactory->create();
                $this->dataObjectHelper->populateWithArray(
                    $productDataObject,
                    $attributes,
                    '\Magento\Catalog\Api\Data\ProductInterface'
                );
                $childrenList[] = $productDataObject;
            }
        }

        $childConfigData = array();
        foreach($childrenList as $child){
            $childConfigData[] = $child;
        }

        return $childConfigData;
    }

}

run command

php bin/magento setup:upgrade

remove var folder and check in frontend

12
  • how to get this bounty for me? Sep 14 '16 at 5:54
  • already i marked as a right answer.i don't know some times bounty functionality not awarding properly.didn't get 100 points?
    – sivakumar
    Sep 14 '16 at 6:31
  • No, i didnt get but may be after expire periods of bounty, they can get, You haven't any option for that? Sep 14 '16 at 6:32
  • no.i marked as a right answer,so you should get immediately.
    – sivakumar
    Sep 14 '16 at 6:33
  • @sivakumar you should click on "+100" to award bounty , you can check here for more info : meta.stackexchange.com/questions/16065/… Sep 16 '16 at 5:44
4

See \Magento\ConfigurableProduct\Pricing\Price\ConfigurablePriceResolver::resolvePrice. It's method resposible for calculation the price of configurable product based on child price.

You can pluginize it and implements your algorithm.

5
  • instead of min can i use max ? is it enough?
    – sivakumar
    Sep 9 '16 at 10:46
  • I have checked that ,if you can use max its not display max price, its always show min price, Sep 9 '16 at 11:02
  • @Rakesh can you look at updated question once?
    – sivakumar
    Sep 9 '16 at 11:03
  • @KAndy i'm trying to plugnize ,but how to get child prices array. i think i need to rewrite the entire ConfigurablePriceResolver class?
    – sivakumar
    Sep 9 '16 at 11:19

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.