magento如何获得可配置产品的关联产品最低价格?


11

默认情况下,在视图页面中magento显示关联产品的最低价格。

我需要显示关联产品的最高价格。任何人都知道逻辑驻留在哪里。如何自定义此行为。

更新:

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;
    }

我正在尝试覆盖此核心文件,但无法正常工作。

<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;
    }
}

您是否要在详细信息页面中显示最高价格?
Rakesh Jesadiya

是的,详细信息并列出。当他们在那时照常更改选项时。
sivakumar

上市价格没有变化,您是否已检查过,仅显示一个价格
Rakesh Jesadiya

没关系。客户应该看到可配置产品的最高价格。
sivakumar

这对你有用吗?我在下面给出了您的要求的示例
Rakesh Jesadiya

Answers:


15

您必须为此制作插件以在详细信息页面中显示最高价格,以下是您需要的逐步模块,

文件路径, 应用程序/代码/供应商/模块名称/

注册文件, 应用程序/代码/供应商/模块名称/registration.php

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

应用程序/代码/供应商/模块名称/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>

应用程序/代码/供应商/模块名称/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>

应用/代码/供应商/模块名称/定价/ConfigurablePrice.php

在此文件中,您必须插入resolveprice()函数

<?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;
    }

}

运行命令

php bin / magento设置:升级

删除var文件夹并检入前端


如何获得我的赏金?
Rakesh Jesadiya

我已经被标记为正确答案了。我不知道赏金功能有时无法正确授予。没有获得100分?
sivakumar

不,我没有得到,但是可能是在赏金到期之后,他们可以得到,您没有任何选择吗?
Rakesh Jesadiya

不,我被标记为正确答案,所以您应该立即得到。
sivakumar

:@sivakumar你应该点击“+100”,奖励奖金,你可以在这里获得更多信息meta.stackexchange.com/questions/16065/...
婴儿在Magento

4

请参阅\Magento\ConfigurableProduct\Pricing\Price\ConfigurablePriceResolver::resolvePrice。该方法可用于基于子价格计算可配置产品的价格。

您可以对其进行插件并实现您的算法。


我可以使用max代替min吗?够了吗
sivakumar

我检查过,如果您可以使用最大而不显示最高价格,则始终显示最低价格
Rakesh Jesadiya

@Rakesh你能看一次更新的问题吗?
sivakumar

@KAndy我正在尝试插件化,但是如何获取儿童价格数组。我想我需要重写整个ConfigurablePriceResolver类?
sivakumar

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.