w jaki sposób magento dostaje konfigurowalny produkt najniższą cenę powiązanych produktów?


11

W widoku strony domyślnie wyświetlana jest najniższa cena powiązanych produktów.

Muszę wyświetlić najwyższą cenę powiązanych produktów. Każdy ma pojęcie, gdzie znajduje się logika. Jak dostosować to zachowanie.

aktualizacja:

Magento \ ConfigurableProduct \ Price \ 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;
    }

Próbuję zastąpić ten podstawowy plik, ale nie działa.

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

Czy chcesz wyświetlić maksymalną cenę na stronie szczegółów?
— Rakesh Jesadiya

tak szczegółowo i lista również. kiedy zmieniają opcje w tym czasie jak zwykle.
— sivakumar

w cenie oferty nie są zmieniane, czy sprawdziłeś, wyświetlana jest tylko jedna cena
— Rakesh Jesadiya 13.09.16

to jest w porządku. klient powinien zobaczyć maksymalną cenę konfigurowalnego produktu.
— sivakumar

czy to działa dla ciebie?
— podałem

Odpowiedzi:


15

Musisz zrobić wtyczkę, aby wyświetlać maksymalną cenę na stronie szczegółów. Poniżej znajduje się moduł krok po kroku dla Twoich potrzeb,

Ścieżka pliku, aplikacja / kod / dostawca / nazwa modułu /

Plik rejestracyjny, aplikacja / kod / dostawca / nazwa modulatora / rejestracja.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 / Price / ConfigurablePrice.php

Wewnątrz tego pliku musisz pluginize funkcji resolprice ()

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

}

Uruchom polecenie

konfiguracja php bin / magento: aktualizacja

usuń folder var i sprawdź w interfejsie


jak zdobyć dla mnie tę nagrodę?
— Rakesh Jesadiya

już zaznaczyłem jako właściwą odpowiedź. nie wiem, czy czasami funkcja nagrody nie przyznaje się poprawnie. nie dostałeś 100 punktów?
— sivakumar

Nie, nie dostałem, ale mogą być po wygaśnięciu okresów nagrody, mogą dostać, Nie masz na to żadnej opcji?
— Rakesh Jesadiya

nie. oznaczone jako poprawna odpowiedź, więc powinieneś otrzymać natychmiast.
— sivakumar

@ sivakumar należy kliknąć „+100”, aby przyznać nagrodę, można sprawdzić tutaj, aby uzyskać więcej informacji: meta.stackexchange.com/questions/16065/…
— Baby in Magento

4

Zobaczyć \Magento\ConfigurableProduct\Pricing\Price\ConfigurablePriceResolver::resolvePrice. Jest to metoda odpowiedzialna za obliczanie ceny konfigurowalnego produktu na podstawie ceny podrzędnej.

Możesz go podłączyć i implementować algorytm.


zamiast min mogę użyć max? Wystarczy?
— sivakumar

Sprawdziłem, że jeśli możesz użyć maks., To nie wyświetla maks. Ceny, zawsze pokazuje minimalną cenę,
— Rakesh Jesadiya

@Rakesh czy możesz raz spojrzeć na zaktualizowane pytanie?
— sivakumar

@KAndy próbuję podłączyć, ale jak uzyskać tablicę cen dla dzieci. myślę, że muszę przepisać całą klasę ConfigurablePriceResolver?
— sivakumar

Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.