0

I am writing a simple extension that is designed to capture the data from an order once it has been placed on Magento. However, any code I write to my observer appears to get passed to the browser as JSON and causes the site to hang at the checkout. I tested the code by simply echoing "Hello World" and it did indeed appear as JSON in the browser.

"Uncaught SyntaxError: Unexpected token H in JSON at position 0"  

Hello World"11"

app\code\Company\ServerSync\etc\events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_submit_all_after">
        <observer name="get_place_order" instance="Company\ServerSync\Observer\Order"/>
    </event>

app\code\Company\ServerSync\Observer\Order.php:

<?php

namespace Company\ServerSync\Observer;

    use Magento\Framework\Event\ObserverInterface;

class Order implements ObserverInterface {

    public function execute(\Magento\Framework\Event\Observer $observer) {
        echo "Hello World";
    }
}

I am fairly new to Magento, can anyone explain to me what I am doing wrong?

1 Answer 1

1

Do not use echo in your models. Only in templates basically.

In your case order submit using ajax call to webapi and echo output breaks down response data format.

You can test your observer using debug or logger as a last resort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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