How to integrate the PrintWearAI configurator on a WordPress / WooCommerce store: theme snippet, automatic add-to-cart via wc-ajax, product sync and mapping.
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.add_to_cart event to your page.PwaiEmbed.handlers.woocommerce() handler posts the item to the WooCommerce cart via POST /?wc-ajax=add_to_cart.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>
data-key is your public embed key (fk_โฆ), found in your workspace โ Integration. It is required โ the widget only reads data-key.#pwai-configurator div, the widget inserts itself near your product form (.woocommerce-product-gallery, .product .summary, form[action*="cart"]โฆ). Add the div explicitly to control placement.data-height (default 700), data-locale (fr/en/es/de/it), data-product (force a catalog slug), data-autodetect="false", data-no-consent="true".Tenant workspace โ Integrations โ WooCommerce โ Connect:
https://myshop.com/wp-json/wc/v3/system_status before it is saved.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.
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:
/api/resolve matches the SKU or product id found on the page and opens the right 3D model.add_to_cart event carries the WooCommerce product id, so the handler adds the correct product.You can also set the external ids manually per product in Products โ edit โ External catalog link.
PwaiEmbed.handlers.woocommerce(options) registers a handler that, for each add_to_cart event:
m.product.external_product_id. If it is missing it logs a warning and does nothing โ the product must be mapped.POST /?wc-ajax=add_to_cart (with cookies, so the item lands in the visitor's session cart) with these form fields:| Field | Content |
|---|---|
product_id | The mapped WooCommerce product id |
quantity | m.quantity |
flare_config_id | Saved configuration id โ the link to production on the PrintWearAI side |
flare_thumbnail | Preview image URL of the finished design |
flare_sizes | JSON size breakdown, e.g. {"M":3,"L":2} |
/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 โฆ
});
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).
data-key missing โ you wrote data-embed-key; the widget only reads data-key./panier/ but my cart is at /cart/ โ the built-in redirect targets /panier/. Call PwaiEmbed.handlers.woocommerce({ redirect: false }) and redirect yourself, or use PwaiEmbed.onAddToCart(fn).external_product_id. Link it in Integrations โ Product mapping, or set it manually in Products.external_product_id. Map the product.PwaiEmbed.handlers.woocommerce() (or PwaiEmbed.onAddToCart(fn)).product_id; if you need a specific variation, handle it yourself with PwaiEmbed.onAddToCart(fn).See also: Embed widget ยท Webhooks ยท Custom site (API)