Created
December 12, 2018 23:12
-
-
Save olance/7173f31a1e9426959453e04a130142e3 to your computer and use it in GitHub Desktop.
[MAGENTO 2] Injecting data into Cart data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vendor\Module\Plugin\Checkout\CustomerData; | |
class CartTotals | |
{ | |
/** | |
* @var \Magento\Customer\Model\Session | |
*/ | |
protected $checkoutSession; | |
/** | |
* @param \Magento\Checkout\Model\Session $checkoutSession | |
*/ | |
public function __construct(\Magento\Checkout\Model\Session $checkoutSession) { | |
$this->checkoutSession = $checkoutSession; | |
} | |
/** | |
* Add cart grand total to result data | |
* | |
* @param \Magento\Checkout\CustomerData\Cart $subject | |
* @param array $result | |
* @return array | |
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
*/ | |
public function afterGetSectionData(\Magento\Checkout\CustomerData\Cart $subject, $result) | |
{ | |
$totals = $this->checkoutSession->getQuote()->getTotals(); | |
if(isset($totals['grand_total'])) { | |
$result['grand_total'] = $totals['grand_total']->getValueInclTax() ?: $totals['grand_total']->getValue(); | |
} | |
return $result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | |
<type name="Magento\Checkout\CustomerData\Cart"> | |
<plugin name="cart_totals" type="Vendor\Module\Plugin\Checkout\CustomerData\CartTotals"/> | |
</type> | |
</config> |
@dmcmillin I’ve created this a few years ago but as far as I can tell & remember, yes you’d need to create the module basics and replace the Vendor\Module
accordingly in the files above
I follow same process. But my plugin is not working. Please help me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Are these the only two files I need to add or must I create the module basics(registration,et all)