0

I have tried all examples found on Magento forums but I can't make it work.

I need to get Order ID and customer email on checkout success page.

Here's my module structure:

TV2M/GoogleSurvey/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'TV2M_GoogleSurvey',
    __DIR__
);

TV2M/GoogleSurvey/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="TV2M_GoogleSurvey" setup_version="1.0.0" schema_version="1.0.0">
    </module>
</config>

TV2M/GoogleSurvey/Block/Onepage/Success.php

<?php
namespace TV2M\GoogleSurvey\Block\Onepage;
use \Magento\Framework\View\Element\Template;



class Success extends \Magento\Checkout\Block\Onepage\Success
{
    public function getGoogleSurvey()
    {
        return "my@email.com";
    }
}

Module is activated, setup upgrade, cache clean...

In my theme folder, I have created a phtml file called by layout XML. File is loaded correctly. But when I call my function it returns nothing: echo $block->getEmail();

I have also tried to add di.xml:

TV2M/GoogleSurvey/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Onepage\Success" type="TV2M\GoogleSurvey\Block\Onepage\Success" />
</config>

Can someone please tell me how to access getGoogleSurvey() function? Which files are necessary in my module? Thanks in advance.

  • another thing that should be recommended, is using the sequence inside your module.xml file, to define the loading order. This should not be the issue in this case, but since it depends on the checkout module, it should be placed inside the module sequence. – Ekk4rd Nov 15 '19 at 11:54
0

you should run di:compile command, also pass require parameters in constructor of parent

class(\Magento\Checkout\Block\Onepage\Success)

Also change getEmail() function name it will be good in practice because we also access DB field by getEmail().

You should have call that template file with your current block in layout file. Can you please share the layout code?

| improve this answer | |
  • 1
    You do not need to implement the constructor in the extended class, if you dont change any constructor parameters. – Ekk4rd Nov 15 '19 at 11:52
  • I've changed function name to getGoogleSurvey() it's not working. Why should I run di:compile I'm in developer mode? – Tz V Nov 15 '19 at 16:59
  • @TzV It is good practice to run di:compile if we add something into di.xml or constructor. Anyways, Is your block is calling? You can check with enabling template and block hints. Can you provide your layout xml code? – Ankit Nov 16 '19 at 13:18
  • Thank's for your feedback. But no output. My XML is correct as it calls the phtml file in my theme folder: <referenceContainer name="before.body.end"> <block class="Magento\Framework\View\Element\Template" name="google_servey.body" after="-" template="Magento_Theme::html/google_survey.phtml" /> </referenceContainer> And there google_survey.phtml: <?php echo $block->getGoogleSurvey(); ?> Hello I just get the Hello. Why should I need di.xml? Can someone provide my with the files I really need just to output something from a block file in module? On any page. – Tz V Nov 20 '19 at 9:29
  • My block is called: /home/webmaster/public_html/dev/app/design/frontend/TV2M/KarineJeff_v2/Magento_Theme/templates/html/google_survey.phtml > Magento\Framework\View\Element\Template But the class is called by another block I didn't edit: /home/webmaster/public_html/dev/vendor/magento/module-checkout/view/frontend/templates/button.phtml > TV2M\GoogleSurvey\Block\Onepage\Success – Tz V Nov 20 '19 at 9:33
0

first you have a problem in your

TV2M/GoogleSurvey/Block/Onepage/Success.php

you defined namespace two times change file to this

<?php
namespace TV2M\GoogleSurvey\Block\Onepage;
use \Magento\Framework\View\Element\Template;

class Success extends \Magento\Checkout\Block\Onepage\Success
{
    public function getEmail()
    {
        return "my@email.com";
    }
}

if helps accept answer

| improve this answer | |
  • You're right for namespace, but it's just a wrong copy and paste. I have edited my question with your code, it's not working either. – Tz V Nov 15 '19 at 16:59
  • is there any error ? – Asad Ullah Nov 16 '19 at 17:28
  • No error, no output. – Tz V Nov 20 '19 at 9:22
0

I finally found by myself.

In google_survey.phtml the correct code to call the function getGoogleSurvey() is:

$blockObj= $block->getLayout()->createBlock('TV2M\GoogleSurvey\Block\Onepage\Success');
echo $blockObj->getGoogleSurvey();

For the entire module to work we need the files mentioned above:

TV2M/GoogleSurvey/registration.php

TV2M/GoogleSurvey/etc/module.xml

TV2M/GoogleSurvey/Block/Onepage/Success.php

And in theme folder:

templates/html/google_survey.phtml

layout/checkout_onepage_success.xml

Now I can change the function getGoogleSurvey() in order to get customer email:

$order = $this->_checkoutSession->getLastRealOrder();
return $order->getCustomerEmail()

We don't need di.xml in this case.

Thanks to those who spent time to help me.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.