0

I would like to display the configurable product Price Range [Min and Max] Ex: $10.00 - $35.00 on wishlist items from the customer dashboard. (Customer dashboard -> My wishlist items list & left sidebar).

Can you please advise how we can achieve this. vendor/magento/module-wishlist/CustomerData/Wishlist.php

Here I am trying to overridden this method from custom module vendor/magento/module-wishlist/CustomerData/Wishlist::getItemData()

Here I have been facing two issues.

  1. getting an issue with $itemResolver

  2. Not fetching data from getPriceRange() method

app/code/Demo/PriceRange/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">
<preference for="Magento\Wishlist\CustomerData\Wishlist" type="Demo\PriceRange\CustomerData\SidebarWishlistPriceRange"/> 
</config>

app/code/Sidebar/CustomerData/WishlistPriceRange.php

 <?php
 namespace Demo\PriceRange\CustomerData;
 use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
 use Magento\Framework\Pricing\Helper\Data as PriceHelper;
 use Psr\Log\LoggerInterface;

class SidebarWishlistPriceRange extends \Magento\Wishlist\CustomerData\Wishlist
{

    /**
     * @var Configurable
     */
    protected $configurableProduct;

    /**
     * @var Configurable
     */
    protected $pricingHelper;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * SidebarWishlistPriceRange constructor.
     * @param \Magento\Wishlist\Helper\Data $wishlistHelper
     * @param \Magento\Wishlist\Block\Customer\Sidebar $block
     * @param \Magento\Catalog\Helper\ImageFactory $imageHelperFactory
     * @param \Magento\Framework\App\ViewInterface $view
     * @param \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface|null $itemResolver
     * @param Configurable $configurableProduct
     * @param PriceHelper $pricingHelper
     * @param LoggerInterface $logger
     */
    public function __construct(
        \Magento\Wishlist\Helper\Data $wishlistHelper,
        \Magento\Wishlist\Block\Customer\Sidebar $block,
        \Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
        \Magento\Framework\App\ViewInterface $view,
        \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface $itemResolver = null,
        Configurable $configurableProduct,
        PriceHelper $pricingHelper,
        LoggerInterface $logger
    )
    {
        parent::__construct(
            $wishlistHelper,
            $block,
            $imageHelperFactory,
            $view,
            $itemResolver
        );
        $this->configurableProduct = $configurableProduct;
        $this->pricingHelper = $pricingHelper;
        $this->logger = $logger;
    }

    /**
     * @param \Magento\Wishlist\Model\Item $wishlistItem
     * @return array
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    protected function getItemData(\Magento\Wishlist\Model\Item $wishlistItem)
    {
        try {
            $priceHtmlData = '';
            $product = $wishlistItem->getProduct();
            $priceHtmlData = $product->getTypeId() == Configurable::TYPE_CODE ? $this->getPriceRange($product) :
                $this->block->getProductPriceHtml(
                    $product,
                    'wishlist_configured_price',
                    \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
                    ['item' => $wishlistItem]
                );
            return [
                //'image' => $this->getImageData($this->itemResolver->getFinalProduct($wishlistItem)), Here is the first issue - 1
                'product_sku' => $product->getSku(),
                'product_id' => $product->getId(),
                'product_url' => $this->wishlistHelper->getProductUrl($wishlistItem),
                'product_name' => $product->getName(),
                'product_price' => $priceHtmlData,
                'product_is_saleable_and_visible' => $product->isSaleable() && $product->isVisibleInSiteVisibility(),
                'product_has_required_options' => $product->getTypeInstance()->hasRequiredOptions($product),
                'add_to_cart_params' => $this->wishlistHelper->getAddToCartParams($wishlistItem),
                'delete_item_params' => $this->wishlistHelper->getRemoveParams($wishlistItem),
            ];

        } catch (\Exception $e) {
            $this->logger('Something went wrong'.$e->getMessage());
        }
    }

    /**
     * @param $product
     * @return string
     */
    public function getPriceRange($product)
    {
        $childProductPrice = [];
        $childProducts = $this->configurableProduct->getUsedProducts($product);
        foreach ($childProducts as $child) {
            $price = number_format($child->getPrice(), 2, '.', '');
            $finalPrice = number_format($child->getFinalPrice(), 2, '.', '');
            $logger->info("finalPrice===".$finalPrice);
            if ($price == $finalPrice) {
                $childProductPrice[] = $price;
            } else if ($finalPrice < $price) {
                $childProductPrice[] = $finalPrice;
            }
        }
        $min = $this->pricingHelper->currencyByStore(min($childProductPrice));
        $max = $this->pricingHelper->currencyByStore(max($childProductPrice)); 
        if (min($childProductPrice) === max($childProductPrice)) {   
            return $this->getPriceRender($product, "$min", '');
        } else {           
            return $this->getPriceRender($product, "$min - $max", '');
        }
    }

    /**
     * @param $product
     * @param $price
     * @param string $text
     * @return mixed
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    protected function getPriceRender($product, $price, $text='')
    {
        //Here is the second issue - 2
        return $this->getLayout()->createBlock('Magento\Framework\View\Element\Template')
            ->setTemplate('Demo_PriceRange::Wishlist/Sidebar/price_range.phtml')
            ->setData('price_id', 'product-price-'.$product->getId())
            ->setData('display_label', $text)
            ->setData('product_id', $product->getId())
            ->setData('display_value', $price)->toHtml();
    }

    /**
     * @param \Magento\Catalog\Model\Product $product
     * @return array
     */
    protected function getImageData($product)
    {
        /** @var \Magento\Catalog\Helper\Image $helper */
        $helper = $this->imageHelperFactory->create()
            ->init($product, 'wishlist_sidebar_block');

        $template = 'Magento_Catalog/product/image_with_borders';

        try {
            $imagesize = $helper->getResizedImageInfo();
        } catch (NotLoadInfoImageException $exception) {
            $imagesize = [$helper->getWidth(), $helper->getHeight()];
        }

        $width = $helper->getFrame()
            ? $helper->getWidth()
            : $imagesize[0];

        $height = $helper->getFrame()
            ? $helper->getHeight()
            : $imagesize[1];

        return [
            'template' => $template,
            'src' => $helper->getUrl(),
            'width' => $width,
            'height' => $height,
            'alt' => $helper->getLabel(),
        ];
    }
}

app/code/Demo/PriceRange/view/frontend/templates/Wishlist/Sidebar/price_range.phtml

<?php /** @var \Magento\Framework\Pricing\Render\Amount $block */ ?>

<div class="price-box range second price-final_price" data-role="priceBox" data-product-id="<?php /* @escapeNotVerified */ echo $block->getProductId() ?>">
    <span class="special-price">
        <span class="price-container price-final_price">
            <?php if ($block->getDisplayLabel()): ?>
                <span class="price-label"><?php /* @escapeNotVerified */ echo $block->getDisplayLabel(); ?></span>
            <?php endif; ?>
            <span <?php if ($block->getPriceId()): ?> id="<?php /* @escapeNotVerified */ echo $block->getPriceId() ?>"<?php endif;?>
                data-price-amount=""
                data-price-type="finalPrice1"
                class="price-wrapper">
                <span class="price"><?php /* @escapeNotVerified */ echo $block->getDisplayValue();?></span>
            </span>
        </span>
    </span>
</div>

wishlist page for configurable prodct

  • Hello, You have to use plugin for display price. Please see this link, mageplaza.com/devdocs/… . Add conidtion on the logic, display price only if the page is wishilist. – Sunny Rahevar Apr 10 at 13:29
  • @sunny actually this plugin will affect the entire add to cart functionality aswell. Can u suggest another solution for this – Nagaraju K Apr 11 at 10:56
  • I think, you just want to display the price range on the wishlist page only. am i right? – Sunny Rahevar Apr 11 at 16:36
  • Yes @SunnyRahevar correct for configurable products we need to display min & max price range on wishlist items. I have tried to find the exact file but no luck where it is rendering. – Nagaraju K Apr 11 at 17:33
1

You can Do this By Overriding Magento\Wishlist\CustomerData\Wishlist

Here is a Function Named as getItemData() which i actually responsible to render and pass data to section if you want to show price range change the login of

Line 146

'product_price' => $this->block->getProductPriceHtml(
                $product,
                'wishlist_configured_price',
                \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
                ['item' => $wishlistItem]
            ),
| improve this answer | |
  • thanks @WaqarAli it is responsible for sidebar wishlist items & what about main wishlist items? please refer to this screenshot prnt.sc/rxhmg3 for this can u help me where we have to do the implement logic? – Nagaraju K Apr 11 at 20:35
  • i have updated my post can u plz help me on this – Nagaraju K Apr 12 at 15:59

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.