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

Add price_formatted and list_price_formatted fields #2

Open
wants to merge 1 commit into
base: convert-fixes
Choose a base branch
from
Open
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
29 changes: 28 additions & 1 deletion Controller/Product/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -132,6 +157,8 @@ protected function getDefaultFields()
'description',
'price',
'list_price',
'price_formatted',
'list_price_formatted',
'image',
'url',
'categories',
Expand Down
15 changes: 14 additions & 1 deletion Helper/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);
}
}
9 changes: 7 additions & 2 deletions Model/Indexer/Product/Action/Rows.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down