
Syncing Magento 2 Orders to Google Sheets in Real-Time
Hey everyone! Welcome to Pillar 2 of our B2B Automation series here at MageSheet.
For the past few posts, we've focused heavily on the Google Workspace side: building interfaces, handling auth, and setting up webhooks.
Now, it's time to connect the dots to one of the most powerful and complex e-commerce platforms on the market: Magento 2 (Adobe Commerce).
If your business runs on Magento, you know firsthand that getting data out of the platform for operational teams (like fulfillment, accounting, or B2B sales reps) can be a headache. Most companies rely on manual CSV exports at the end of the day, or pay hundreds of dollars a month for middleware SaaS connectors that constantly hit API limits.
Today, we're going to build a direct, blazing-fast, serverless bridge between Magento 2 and Google Sheets to sync orders the exact second they are placed.
The Architecture: Magento Webhook to Apps Script
We are going to use the doPost() architecture we discussed in our Apps Script Webhooks guide. The flow is simple:
- A customer clicks "Place Order" in Magento.
- Magento fires an internal event (
sales_order_save_commit_after). - Your Magento server sends a lightweight HTTP POST payload directly to a Google Apps Script URL.
- Google Apps Script parses the JSON and appends the order to a Google Sheet.
Notice what is missing here? No middleware. No API polling. No delays. It happens instantly.
Step 1: Prepping the Google Sheet Backend
First, let's create the destination. Open a new Google Sheet, name the first tab LiveOrders, and create the following headers in Row 1:
| Magento Order ID | Status | Grand Total | Customer Email |
|---|
Next, open Extensions > Apps Script and deploy the listener. We'll add some basic security here to ensure random bots don't fill your sheet with garbage data.
// Code.gs
// Define a secret key that only Magento and Apps Script know
const WEBHOOK_SECRET = "MageSheet_Super_Secret_2026";
function doPost(e) {
try {
const payload = JSON.parse(e.postData.contents);
// Security check! Did Magento send the correct secret?
if (payload.secret !== WEBHOOK_SECRET) {
return ContentService.createTextOutput("Forbidden: Invalid Secret").setStatusCode(403);
}
// Parse the order data
const order = payload.order_data;
// Select the sheet and append
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("LiveOrders");
sheet.appendRow([
order.increment_id,
order.status,
order.grand_total,
order.customer_email
]);
return ContentService.createTextOutput(JSON.stringify({"status": "success"}))
.setMimeType(ContentService.MimeType.JSON);
} catch (error) {
return ContentService.createTextOutput("Error: " + error.message).setStatusCode(500);
}
}
Deploy this as a Web App (Execute as: Me, Access: Anyone). Copy the resulting URL.
Step 2: Firing the Event from Magento 2
While Magento 2 has robust REST and GraphQL APIs for pulling data, native "Outbound Webhooks" often require a small custom module.
To keep this tutorial lightweight, we will create a simple Observer module in Magento. Let's assume you have a custom module named MageSheet_GoogleSync.
You need an events.xml file in app/code/MageSheet/GoogleSync/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="sales_order_place_after">
<observer name="magesheet_sync_order_to_google" instance="MageSheet\GoogleSync\Observer\SyncOrder" />
</event>
</config>
And your Observer file app/code/MageSheet/GoogleSync/Observer/SyncOrder.php:
<?php
namespace MageSheet\GoogleSync\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\HTTP\Client\Curl;
class SyncOrder implements ObserverInterface
{
private $curl;
public function __construct(Curl $curl)
{
$this->curl = $curl;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$order = $observer->getEvent()->getOrder();
// The URL you got from Google Apps Script
$googleAppUrl = "https://script.google.com/macros/s/AKfycby..._aBCD/exec";
// Prepare payload matching our Apps Script expectations
$payload = [
"secret" => "MageSheet_Super_Secret_2026",
"order_data" => [
"increment_id" => $order->getIncrementId(),
"status" => $order->getStatus(),
"grand_total" => $order->getGrandTotal(),
"customer_email" => $order->getCustomerEmail()
]
];
// Fire the cURL POST request
$this->curl->addHeader("Content-Type", "application/json");
$this->curl->post($googleAppUrl, json_encode($payload));
// Important: Use asynchronous queues (RabbitMQ/Cron) in production
// to prevent Google's response time from slowing down Magento checkout!
}
}
The Crucial Production Warning: Asynchronous Queues
In the PHP example above, we placed a direct cURL request inside the sales_order_place_after event.
Warning: While this is great for a development test, doing this synchronously in production means your customer's browser will keep spinning on the checkout page until Google Apps Script replies. If Google is slow, your checkout is slow.
For enterprise environments, you MUST wrap that cURL request in a Magento Message Queue (RabbitMQ) or process it via a Cron job. This ensures that Magento instantly tells the customer "Order Success", while the actual HTTP request to Google happens a few seconds later in the background.
The Power of Instant Visibility
By utilizing this architecture, your Ops team can open Google Sheets and watch orders populate magically the second a B2B transaction is completed. From there, you can use Sheets formulas, Looker Studio dashboards, or Google Apps Script triggers to automatically forward that order to a 3PL or accounting department.
Building a resilient, enterprise-grade integration between Adobe Commerce and the Google Workspace ecosystem requires deep knowledge of Magento Message Queues, caching, and Apps Script quota limits. If your business needs a heavy-duty architecture designed for massive order volume, MageSheet specializes in exactly this. Get in touch!


