diff --git a/Controller/Product/Index.php b/Controller/Product/Index.php index 56143b5..3601832 100644 --- a/Controller/Product/Index.php +++ b/Controller/Product/Index.php @@ -32,21 +32,32 @@ class Index extends AbstractAction protected $imageHandler; /** - * Popup controller constructor. + * @var \Clerk\Clerk\Helper\Product + */ + protected $productHelper; + + /** + * Product Index constructor. * * @param Context $context * @param ScopeConfigInterface $scopeConfig + * @param CollectionFactory $productCollectionFactory + * @param \Clerk\Clerk\Helper\Product $productHelper + * @param Image $imageHandler + * @param LoggerInterface $logger */ public function __construct( Context $context, ScopeConfigInterface $scopeConfig, CollectionFactory $productCollectionFactory, + \Clerk\Clerk\Helper\Product $productHelper, Image $imageHandler, LoggerInterface $logger ) { $this->collectionFactory = $productCollectionFactory; $this->imageHandler = $imageHandler; + $this->productHelper = $productHelper; $this->addFieldHandlers(); parent::__construct($context, $scopeConfig, $logger); @@ -67,6 +78,13 @@ protected function addFieldHandlers() } }); + //Add price_formatted fieldhandler + $this->addFieldHandler('price_formatted', function($item) { + $priceHandler = $this->fieldHandlers['price']; + $price = $priceHandler($item); + return $this->productHelper->formatCurrency($price); + }); + //Add list_price fieldhandler $this->addFieldHandler('list_price', function($item) { try { @@ -83,6 +101,13 @@ protected function addFieldHandlers() } }); + //Add list_price_formatted fieldhandler + $this->addFieldHandler('list_price_formatted', function($item) { + $priceHandler = $this->fieldHandlers['list_price']; + $price = $priceHandler($item); + return $this->productHelper->formatCurrency($price); + }); + //Add image fieldhandler $this->addFieldHandler('image', function($item) { @@ -132,6 +157,8 @@ protected function getDefaultFields() 'description', 'price', 'list_price', + 'price_formatted', + 'list_price_formatted', 'image', 'url', 'categories', diff --git a/Helper/Product.php b/Helper/Product.php index 2123d29..a1e7420 100644 --- a/Helper/Product.php +++ b/Helper/Product.php @@ -24,10 +24,12 @@ class Product */ public function __construct( \Magento\CatalogInventory\Helper\Stock $stockHelper, - \Magento\CatalogInventory\Model\StockRegistryStorage $stockRegistryStorage + \Magento\CatalogInventory\Model\StockRegistryStorage $stockRegistryStorage, + \Magento\Framework\Pricing\Helper\Data $pricingHelper ) { $this->stockHelper = $stockHelper; $this->stockRegistryStorage = $stockRegistryStorage; + $this->pricingHelper = $pricingHelper; } /** @@ -49,4 +51,15 @@ public function isSalable(\Magento\Catalog\Model\Product $product) return $product->isSalable(); } + + /** + * Format the given value as currency + * + * @param float $value + * @return mixed + */ + public function formatCurrency($value) + { + return $this->pricingHelper->currency($value, true, false); + } } diff --git a/Model/Indexer/Product/Action/Rows.php b/Model/Indexer/Product/Action/Rows.php index e04fde7..0a5531c 100644 --- a/Model/Indexer/Product/Action/Rows.php +++ b/Model/Indexer/Product/Action/Rows.php @@ -177,12 +177,17 @@ protected function reindexRow($productId) $imageUrl = $this->imageHandler->handle($product); + $price = $product->getFinalPrice(); + $listPrice = $product->getPrice(); + $productItem = [ 'id' => $product->getId(), 'name' => $product->getName(), 'description' => $product->getDescription(), - 'price' => $product->getFinalPrice(), - 'list_price' => $product->getPrice(), + 'price' => $price, + 'price_formatted' => $this->helper->formatCurrency($price), + 'list_price' => $listPrice, + 'list_price_formatted' => $this->helper->formatCurrency($listPrice), 'image' => $imageUrl, 'url' => $product->getUrlModel()->getUrl($product), 'categories' => $product->getCategoryIds(),