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.
getting an issue with $itemResolver
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>