One Order Model for WooCommerce, Shopify and More: Adapters and Idempotent Imports
WooCommerce, Shopify and other platforms all describe an order differently. If your platform creates orders from several of them, the worst design is code full of if ($source === 'shopify') everywhere. A cleaner design converts every incoming order into one internal format at the edge, and everything after that works with only your own model.
What this article is, and is not
This is a design guide with example code. The fields shown are simplified. Your platform will need more, such as shipping methods, taxes and discounts, but the structure stays the same.
1. Design your own order tables first
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->string('source', 30); // woocommerce, shopify, manual
$table->string('external_id', 64); // the order id on that platform
$table->string('number')->nullable(); // what the customer sees
$table->string('status', 20); // your own statuses
$table->char('currency', 3);
$table->bigInteger('total_minor'); // cents, never floats
$table->string('customer_name');
$table->string('customer_email')->nullable();
$table->json('shipping_address')->nullable();
$table->json('raw_payload'); // original data, for debugging
$table->timestamp('placed_at');
$table->timestamps();
$table->unique(['source', 'external_id']); // the key to idempotent imports
});
The unique pair (source, external_id) is what makes it safe to receive the same order twice.
2. One adapter per platform
interface OrderAdapter
{
public function normalize(array $payload): NormalizedOrder;
}
final class NormalizedOrder
{
public function __construct(
public string $source, public string $externalId, public string $status,
public string $currency, public int $totalMinor, public string $customerName,
public ?string $customerEmail, public array $shippingAddress,
public array $items, public CarbonImmutable $placedAt, public array $raw,
) {}
}
final class WooCommerceAdapter implements OrderAdapter
{
public function normalize(array $p): NormalizedOrder
{
return new NormalizedOrder(
source: 'woocommerce',
externalId: (string) $p['id'],
status: $this->status($p['status']),
currency: $p['currency'],
totalMinor: (int) round(((float) $p['total']) * 100),
customerName: trim($p['billing']['first_name'].' '.$p['billing']['last_name']),
customerEmail: $p['billing']['email'] ?: null,
shippingAddress: $p['shipping'],
items: array_map(fn ($i) => ['name' => $i['name'], 'sku' => $i['sku'], 'qty' => $i['quantity']], $p['line_items']),
placedAt: CarbonImmutable::parse($p['date_created_gmt'], 'UTC'),
raw: $p,
);
}
private function status(string $s): string
{
return match ($s) {
'completed' => 'completed',
'cancelled', 'failed', 'refunded' => 'cancelled',
default => 'open',
};
}
}
A Shopify adapter does the same job for Shopify's field names. Everything after this point does not know or care where the order came from.
3. One place that saves orders
class OrderImporter
{
public function __construct(private array $adapters) {} // ['woocommerce' => ..., 'shopify' => ...]
public function import(string $source, array $payload): Order
{
$n = $this->adapters[$source]->normalize($payload);
return DB::transaction(function () use ($n) {
$order = Order::updateOrCreate(
['source' => $n->source, 'external_id' => $n->externalId],
['status' => $n->status, 'currency' => $n->currency, 'total_minor' => $n->totalMinor,
'customer_name' => $n->customerName, 'customer_email' => $n->customerEmail,
'shipping_address' => $n->shippingAddress, 'raw_payload' => $n->raw, 'placed_at' => $n->placedAt]
);
$order->items()->delete();
$order->items()->createMany($n->items);
if ($order->wasRecentlyCreated) {
GenerateInvoice::dispatch($order->id);
}
return $order;
});
}
}
Only a brand-new order triggers the invoice. An update from the same platform changes the order but does not print a second invoice.
4. Keep the raw payload
When something looks wrong, the original data is the first thing you will want. Store it, and delete it later if privacy rules require it.
Common mistakes
- Letting platform-specific fields spread through your code.
- Storing money as floats.
- No unique key on source and external id, so duplicates appear.
- Triggering side effects such as invoices on every update, not only on creation.
Summary
Define your own order model, convert each platform's data with an adapter, save through one importer that is safe to repeat, and keep the original payload for debugging.