I'm using Magento 2.4 and I'm trying to get grand total and customer data variables to be inserted to Google tracking code.
I've followed the link below to create custom module to call $block->getGrandTotal but it's not work ! https://magento.stackexchange.com/a/116922/105485
This is google code which I've to fill it with magento variables :
<script> gtag('event', 'conversion', { 'send_to': 'AW-595825986/_BYQCLj7tr0DEMKqjpwC', 'value': <?php echo $totall;?>, 'currency': 'SAR', 'transaction_id': '' }); </script>
<script> dataLayer.push({ 'event':'purchase', 'order_value':'verraible for order value', 'order_id':'order_id', 'enhanced_conversion_data': { "email": 'yourEmailVariable', "phone_number": 'yourPhoneVariable', "first_name": 'yourFirstNameVariable', "last_name": 'yourLastNameVariable', "street": 'yourStreetAddressVariable', "city": 'yourCityVariable', "region": 'yourRegionVariable', "postal_code": 'yourPostalCodeVariable', "country": 'yourCountryVariable' } }) </script>
Hi @infonectar9054 ,
Refer or use below extension and you will get more idea from it.
https://github.com/magepal/magento2-google-tag-manager
or
https://www.weltpixel.com/google-analytics-enhanced-ecommerce-tag-manager-magento-2.html
Hope it helps!
Thanks
Hello @infonectar9054
Try to refer below code:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $orderData = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId()); this will show all order data >>>$orderData->getData(); ====or===== $order = \Magento\Framework\App\ObjectManager::getInstance(); $orderData = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId()); $orderData->>getSubtotal(); $orderData->getDiscountAmount(); $orderData->getGrandTotal()
It may help you!
Thank you
It' sounds good, what about the shipping address ? How I can get it ?
Also I saw many persons said it's not recommended to use object manager directly ! is it ok ?
Hello @infonectar9054
Try below code for get shipping address.
$shippingAddress = $order->getShippingAddress(); $city = $shippingAddress->getCity(); $state = $shippingAddress->getRegion(); $countryData = $this->countryInformation->getCountryInfo($shippingAddress->getCountryId()); $country = $countryData->getFullNameLocale();
It may help you!
Thank you
Could you please confirm about my question ?
Also I saw many persons said it's not recommended to use object manager directly ! is it ok ?
Hello @infonectar9054
I have just share reference code with you. Developer need to use this with block and get values by block function. Then use with phtml file. Which is standard magento coding.
Thanks
Hello @Bhanu Periwal
That's what I'm looking for.
Because I've created module with block but it's not work with me.
I need the right way to use this in block and call the function in success page.
Hello @infonectar9054
In that case you can refer below example code:
Create Block file in your custom module and create function:
<?php namespace Company\Helloworld\Block; use Magento\Framework\View\Element\Template; class Main extends Template { public function getMyCustomMethod() { return '<b>I Am From MyCustomMethod</b>'; } }
then in any phtml file you can use following code to get method of this block.
<?php $blockObj= $block->getLayout()->createBlock('Company\Helloworld\Block\Main'); echo $blockObj->getMyCustomMethod(); ?>
It will help you!
Thank you
Hello @Bhanu Periwal
Sorry to bother you because I'm not professional in PHP, so I've to use the object manager inside custom function right ?