E-CommerceSustainabilityAPIDeveloper

Magento Tree Planting: Add Verified Reforestation

GoodAPI Team ·

Magento Tree Planting: How to Add Verified Reforestation to a Magento Store

If you run a Magento store and you want to plant a tree for every order, you have probably searched for a Magento tree planting extension and come up short. The sustainability tools that dominate ecommerce are built for Shopify, packaged as apps and theme widgets. Magento is a different animal: a PHP platform you control at the code level, often running B2B catalogs, custom checkouts, and serious order volume. The good news is that you do not need an off-the-shelf extension to add verified reforestation. You need an API and a small custom module.

This guide shows Magento developers and merchants how to add verified tree planting to a Magento 2 or Adobe Commerce store using GoodAPI. It covers where to hook into the order lifecycle, the observer code that calls the planting endpoint, and how to keep the whole thing retry-safe in production.

Why Magento Stores Need an API, Not an App

Magento is not a niche platform you can afford to ignore. It powers roughly 8% of the global ecommerce platform market and remains the leading B2B ecommerce platform, with well over 100,000 live stores and around $173 billion in annual gross merchandise value flowing through it in 2026. Europe alone accounts for close to half of all Magento usage. That is a lot of orders that could be planting trees and a lot of merchants who get told the sustainability ecosystem was built for someone else’s platform.

The reason an embedded widget does not fit Magento is the same reason developers choose Magento in the first place: control. Your checkout is customized, your order workflow may include manual review or B2B net terms, and your store might be fully headless with a separate frontend. In that world, the right place to trigger an environmental action is not a theme block. It is your server, at the exact moment an order is confirmed as paid. A REST API lets you put tree planting wherever your business logic already lives.

What an API-first approach buys you

A platform-agnostic sustainability API gives a Magento team three things a plugin cannot. You decide the trigger, so a tree can be planted on a paid invoice, a subscription renewal, a B2B purchase order, or any custom milestone in your order state machine. You own the data, so you can attribute trees to specific orders and pull verification evidence into your own admin or customer account pages. And you stay portable, because the same integration survives a theme rebuild, a move to headless, or a future Adobe Commerce upgrade.

What You Will Build

The pattern here is deliberately production-shaped and simple: a customer completes an order, Magento captures payment and pays the invoice, your custom module observes that event, and an observer class calls GoodAPI to plant a tree attributed to the order. Planting on the paid event rather than on order placement means you never plant a tree for an order that fails payment or gets cancelled in review.

In Magento 2 terms, that is one events.xml file and one observer class. Nothing exotic, no marketplace dependency, and no theme changes.

Step-by-Step: Magento Tree Planting With GoodAPI

1

Get your API keys

Sign up at app.thegoodapi.com for a production key and a test key. Both behave identically, but the test key never charges you and never plants real trees, so use it for the entire build. Store the key in Magento’s configuration or an environment variable, never hard-coded in a template.

2

Scaffold a small custom module

Create a module under app/code/GoodApi/TreePlanting with the usual registration.php, etc/module.xml, and an etc/events.xml. This is the container for your integration and keeps it cleanly separable from core and from third-party extensions.

3

Listen for the order-paid event

In events.xml, subscribe an observer to the sales_order_invoice_pay event. This fires when an invoice is paid, which is the moment you want to plant.

4

Call the plant/trees endpoint from the observer

In the observer’s execute method, read the order off the invoice and send a POST to https://app.thegoodapi.com/plant/trees with your key in the Authorization header, a count, an attribution, and an idempotency_key.

5

Surface verified impact to customers

Use GET /plant/trees and GET /evidence to read totals and verification media, then render a “trees planted” figure on the storefront, in the customer account, or in order confirmation emails.

The events.xml

Register the observer against the paid-invoice event. This is the entire wiring layer.

app/code/GoodApi/TreePlanting/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_invoice_pay">
<observer name="goodapi_plant_tree_on_invoice_pay"
instance="GoodApi\TreePlanting\Observer\PlantTreeOnInvoicePay" />
</event>
</config>

The observer

Here is the core of the integration. The observer implements ObserverInterface, pulls the order from the paid invoice, and plants one tree attributed to that order. It is safe to call more than once for the same order because of the idempotency key.

app/code/GoodApi/TreePlanting/Observer/PlantTreeOnInvoicePay.php
<?php
namespace GoodApi\TreePlanting\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\HTTP\Client\Curl;
class PlantTreeOnInvoicePay implements ObserverInterface
{
public function __construct(private Curl $curl) {}
public function execute(Observer $observer): void
{
$invoice = $observer->getEvent()->getInvoice();
$order = $invoice->getOrder();
$payload = [
'count' => 1,
'attribution' => $order->getCustomerEmail(),
'idempotency_key' => $order->getIncrementId(),
'metadata' => [
'order_id' => $order->getIncrementId(),
'source' => 'magento',
],
];
$this->curl->addHeader('Authorization', getenv('GOODAPI_KEY'));
$this->curl->addHeader('Content-Type', 'application/json');
$this->curl->post('https://app.thegoodapi.com/plant/trees', json_encode($payload));
// Log $this->curl->getBody() with the order ID; rely on the
// idempotency_key so a replayed event is always safe.
}
}

The attribution field tags the tree with a non-unique lookup key, so you can later fetch every tree tied to a customer or a campaign. The metadata object accepts arbitrary key/value pairs, which is the natural place for your Magento order increment ID or store view. And the idempotency_key is what makes the call safe to retry when Magento reindexes, replays, or an admin re-saves the invoice.

Reading impact back into Magento

A Magento store should show off its impact, and GoodAPI gives you two reads for that. GET https://app.thegoodapi.com/plant/trees returns your totals and can be filtered by attribution_key, date range, or metadata, which is handy if you want a per-customer tree count on the account page. GET https://app.thegoodapi.com/evidence returns aggregated verification evidence including photos and impact metrics like total trees, carbon offset, and hectares restored across countries such as Kenya, Madagascar, Brazil, and Indonesia. You can render those numbers in a block, a CMS widget, or a transactional email template.

Verification: The Part That Actually Matters

Wiring up an endpoint is the easy part. The reason to integrate GoodAPI instead of mailing a check to a planting charity is that every tree is verifiable down to the GPS coordinate. GoodAPI’s reforestation partner is Veritree, a verified reforestation organization with global projects, so each tree is tracked, geolocated, and supported through its critical first years of growth.

For developers, that verification is itself an API. The evidence endpoint can return a timeline for a single tree: when it was registered, when funds were distributed, when it was allocated to a planting site and crew, and when it was physically planted, with geotagged photo evidence at the planting coordinate. That means a Magento store can show a B2B buyer or a retail customer not just “we planted a tree” but the verified status of their specific tree.

Cost and Operational Notes for Magento Teams

Pricing suits a platform where you are usually trying to avoid yet another per-seat SaaS bill. GoodAPI charges $0.43 per tree with no monthly fee and no separate cost for being on Magento versus Shopify, because it is the same underlying API. Trees accrue to a single end-of-month invoice, which is easy to reconcile against your order volume.

Two practical tips for production. First, do all of your development against the test key so you can hammer the endpoint without generating real charges or real trees. Second, treat the plant call like any external dependency in your order pipeline: wrap it in error handling, log failures with the order increment ID, and lean on the idempotency_key so a replay after a failure is always safe. If you run high order volume, you can also batch by passing a higher count, for example planting a tree per item rather than per order.

Best for

Magento 2 and Adobe Commerce merchants and developers who want verified tree planting they control in code, not a Shopify-only app, at the lowest per-tree cost.

Bringing It Together

Adding reforestation to a Magento store does not require waiting for someone to build a marketplace extension. With a REST sustainability API, you plant from your own server on the paid-invoice event, attribute every tree to an order, stay retry-safe with idempotency keys, and read verified impact back into Magento. The same module keeps working whether your store is classic Magento, Adobe Commerce, or a headless build with a custom frontend.

Tree planting is also only one of the impact types GoodAPI exposes through the same API. If you want to combine reforestation with plastic removal or charitable donations behind one integration, see our guide to the unified impact stack for ecommerce. To go deeper on the API itself, read the GoodAPI developer documentation, our overview of what a sustainability API is, and our best tree planting API comparison. Running Shopify alongside Magento? The GoodAPI app holds a 5.0★ rating across 220+ merchant reviews and is Built for Shopify, so you can install GoodAPI from the Shopify App Store for the managed route while your Magento store uses the API directly.