Add custom price to item checkout summary Magento 2
-
11-10-2020 - |
문제
Product sale price is custom (different from price-saleprice). Now i want to show row total based on Original mrp(Mrp*qty). Its displaying row total based on custom (sale price * qty). I did below to achieve this Created di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Model\DefaultConfigProvider">
<plugin name="AddAttPlug" type="[company]\[module]\Model\Plugin\ConfigProviderPlugin" />
</type>
</config>
namespace [company][module]\Model\Plugin;
class ConfigProviderPlugin extends \Magento\Framework\Model\AbstractModel
{
public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, array $result)
{
$items = $result['totalsData']['items'];
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
for($i=0;$i<count($items);$i++){
$quoteId = $items[$i]['item_id'];
$quote = $objectManager->create('\Magento\Quote\Model\Quote\Item')->load($quoteId);
$originalRowTotal = $quote->getOriginalPriceRowTotal();
$items[$i]['original_row_total'] = $originalRowTotal;
}
$result['totalsData']['items'] = $items;
return $result;
}
}
[company]/[module]/view/frontend/web/js/checkout/summary/items/details/subtotal.js
define(
[
'Magento_Checkout/js/view/summary/abstract-total'
],
function (viewModel) {
"use strict";
return viewModel.extend({
defaults: {
displayArea: 'after_details',
template: 'Magento_Checkout/summary/item/details/subtotal'
},
getValue: function(quoteItem) {
return this.getFormattedPrice(quoteItem.original_row_total);
}
});
}
);
I am getting the value right on proceed to checkout but when i go to next step payment and review that value is going to 0.And form payment review to back to shipping step its again going to 0.
해결책 2
class ConfigProviderPlugin
{
/**
*@var checkoutSession
*/
protected $checkoutSession;
/**
*Constructor
* @param CheckoutSession $checkoutSession
*/
public function __construct(CheckoutSession $checkoutSession)
{
$this->checkoutSession = $checkoutSession;
}
public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, array $result)
{
$items = $result['totalsData']['items'];
foreach ($items as $index => $item) {
$quoteItem = $this->checkoutSession->getQuote()->getItemById($item['item_id']);
$result['quoteItemData'][$index]['original_row_total'] = $quoteItem->getOriginalPriceRowTotal();
}
return $result;
}
}
I just replace configProvidePlugin class with above class.
다른 팁
Just to Add Shekhar Suman Answer didn't work for me but certainly put me in the right path so Here is my changes to the ConfigProviderPlugin
:
class ConfigProviderPlugin
{
/**
*@var checkoutSession
*/
protected $checkoutSession;
/**
*Constructor
* @param CheckoutSession $checkoutSession
*/
public function __construct(CheckoutSession $checkoutSession)
{
$this->checkoutSession = $checkoutSession;
}
public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, array $result)
{
$items = $result['totalsData']['items'];
foreach ($items as $index => $item) {
$quoteItem = $this->checkoutSession->getQuote()->getItemById($item['item_id']);
$result['totalsData']['items'][$index]['original_row_total'] = $quoteItem->getProduct()->getPrice();
}
return $result;
}
}
In case anyone stumble on this with the above changes I was able to get original price which in my case I had special price and I need to display the regular price too in summary so it's similar display to product page.