๐Ÿ“ฆ WooCommerce integration

How to integrate the PrintWearAI configurator on a WordPress / WooCommerce store: theme snippet, automatic add-to-cart via wc-ajax, product sync and mapping.

How it works

  1. embed.js injects the 3D configurator as an iframe on your product pages. On WooCommerce it auto-detects the current product: body.single-product, the input[name=product_id] / input[name=variation_id] field, the postid-N body class and the .sku element.
  2. When the customer clicks add-to-cart inside the configurator, the widget emits an add_to_cart event to your page.
  3. The built-in PwaiEmbed.handlers.woocommerce() handler posts the item to the WooCommerce cart via POST /?wc-ajax=add_to_cart.

Step 1 โ€” Install the snippet in your theme

Add the snippet to your product template (child theme single-product.php, a hook in functions.php, or a "custom HTML" block/snippet plugin):

<div id="pwai-configurator"></div>

<script src="https://printwearai.com/embed.js"
        data-key="fk_YOUR_KEY"
        defer></script>
<script>
  document.addEventListener('DOMContentLoaded', function () {
    PwaiEmbed.handlers.woocommerce();   // automatic WooCommerce cart
  });
</script>
The embedded widget requires a Pro plan or higher. On Free/Essentiel plans the widget shows an upgrade notice and your customers use your hosted configurator page instead.

Step 2 โ€” Connect your store (product sync)

Tenant workspace โ†’ Integrations โ†’ WooCommerce โ†’ Connect:

Then click Sync (runs automatically after connecting). PrintWearAI imports your published products from /wp-json/wc/v3/products โ€” id, SKU, name, price, image and permalink โ€” into your external products catalog.

Step 3 โ€” Map WooCommerce products to 3D products

In Integrations โ†’ Product mapping, link each imported WooCommerce product to one of your 3D products. Linking stores external_product_id / external_sku on the 3D product, which enables:

You can also set the external ids manually per product in Products โ†’ edit โ†’ External catalog link.

Automatic add-to-cart

PwaiEmbed.handlers.woocommerce(options) registers a handler that, for each add_to_cart event:

  1. Reads m.product.external_product_id. If it is missing it logs a warning and does nothing โ€” the product must be mapped.
  2. Sends POST /?wc-ajax=add_to_cart (with cookies, so the item lands in the visitor's session cart) with these form fields:
FieldContent
product_idThe mapped WooCommerce product id
quantitym.quantity
flare_config_idSaved configuration id โ€” the link to production on the PrintWearAI side
flare_thumbnailPreview image URL of the finished design
flare_sizesJSON size breakdown, e.g. {"M":3,"L":2}
  1. Redirects to /panier/ (disable with { redirect: false } โ€” see pitfalls below if your cart lives at another URL).

WooCommerce core ignores extra POST fields by default. To persist the flare_* fields as cart/order item meta, add a small snippet in your (child) theme's functions.php:

add_filter('woocommerce_add_cart_item_data', function ($data) {
  foreach (['flare_config_id', 'flare_thumbnail', 'flare_sizes'] as $f) {
    if (!empty($_POST[$f])) $data[$f] = sanitize_text_field(wp_unslash($_POST[$f]));
  }
  return $data;
}, 10, 1);

add_action('woocommerce_checkout_create_order_line_item', function ($item, $key, $values) {
  foreach (['flare_config_id', 'flare_thumbnail', 'flare_sizes'] as $f) {
    if (!empty($values[$f])) $item->add_meta_data($f, $values[$f]);
  }
}, 10, 3);

Prefer full control? Register your own callback instead of the built-in handler:

PwaiEmbed.onAddToCart(function (m) {
  // m.config_id, m.quantity, m.sizes,
  // m.product.external_product_id, m.customization.thumbnail โ€ฆ
});

Keep products in sync (WooCommerce โ†’ PrintWearAI webhook)

To update your imported catalog without manual re-syncs, create WooCommerce webhooks (WooCommerce โ†’ Settings โ†’ Advanced โ†’ Webhooks) for the topics product.created and product.updated, pointing to:

POST https://printwearai.com/api/webhooks/woocommerce/fk_YOUR_KEY

Use the same webhook secret as saved in Integrations: the X-WC-Webhook-Signature header is verified and invalid deliveries are rejected (401).

FAQ & common pitfalls

See also: Embed widget ยท Webhooks ยท Custom site (API)