Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use price renderer defined in layout to render product prices in auto… #207

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

namespace Smile\ElasticsuiteCatalog\Model\Autocomplete\Product;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\ObjectManagerInterface;
use Magento\Catalog\Helper\Image as ImageHelper;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\Pricing\Render;

/**
* Create an autocomplete item from a product.
Expand All @@ -37,16 +39,29 @@ class ItemFactory extends \Magento\Search\Model\Autocomplete\ItemFactory
*/
private $imageHelper;

/**
* @var \Magento\Framework\Pricing\Render
*/
private $priceRenderer;

/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* Constructor.
*
* @param ObjectManagerInterface $objectManager Object manager used to instantiate new item.
* @param ImageHelper $imageHelper Catalog product image helper.
* @param Render $priceRenderer Catalog product price renderer.
*/
public function __construct(ObjectManagerInterface $objectManager, ImageHelper $imageHelper)
public function __construct(ObjectManagerInterface $objectManager, ImageHelper $imageHelper, Render $priceRenderer)
{
parent::__construct($objectManager);
$this->imageHelper = $imageHelper;
$this->priceRenderer = $priceRenderer;
$this->objectManager = $objectManager;
}

/**
Expand All @@ -72,11 +87,10 @@ private function addProductData($data)
$product = $data['product'];

$productData = [
'title' => html_entity_decode($product->getName()),
'title' => $product->getName(),
'image' => $this->getImageUrl($product),
'url' => $product->getProductUrl(),
'price' => $product->getFinalPrice(),
'final_price' => $product->getPrice(),
'price' => $this->renderProductPrice($product, \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE),
];

$data = array_merge($data, $productData);
Expand All @@ -97,4 +111,58 @@ private function getImageUrl($product)

return $this->imageHelper->getUrl();
}


/**
* Renders product price.
*
* @param \Magento\Catalog\Model\Product $product The product
* @param string $priceCode The Price Code to render
*
* @return string
*/
private function renderProductPrice(\Magento\Catalog\Model\Product $product, $priceCode)
{
$priceRender = $this->getPriceRenderer();

$price = $product->getData($priceCode);

if ($priceRender) {
$price = $priceRender->render(
$priceCode,
$product,
[
'include_container' => false,
'display_minimal_price' => true,
'zone' => Render::ZONE_ITEM_LIST,
'list_category_page' => true,
]
);
}

return $price;
}

/**
* Retrieve Price Renderer Block
*
* @return bool|\Magento\Framework\View\Element\BlockInterface
*/
private function getPriceRenderer()
{
/** @var \Magento\Framework\View\LayoutInterface $layout */
$layout = $this->objectManager->get('\Magento\Framework\View\LayoutInterface');
$layout->getUpdate()->addHandle('default');
$priceRenderer = $layout->getBlock('product.price.render.default');

if (!$priceRenderer) {
$priceRenderer = $layout->createBlock(
'Magento\Framework\Pricing\Render',
'product.price.render.default',
['data' => ['price_render_handle' => 'catalog_product_prices']]
);
}

return $priceRenderer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,10 @@
font-size: 63%;
color: #777;
}

.price-box {
span.old-price {
display:block;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="product-primary"><div class="product-name"><%- data.title %></div></div>
<div class="product-secondary">
<div class="price-box">
<span class="regular-price"><span class="price"><%- data.price %></span></span>
<span class="regular-price"><span class="price"><%= data.price %></span></span>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ define([
var template = this._getTemplate(element);
element.index = index;

if (element.price) {
if (element.price && (!isNaN(element.price))) {
element.price = priceUtil.formatPrice(element.price, this.options.priceFormat);
}

Expand Down