boxybird / inertia-wordpress

The WordPress adapter for Inertia.js.
170 stars 26 forks source link

How to work with woocommerce? #20

Closed mangcoding closed 1 year ago

boxybird commented 1 year ago

You bet. Any data that can be pulled from WordPress and passed to Inertia will work.

<?php

use BoxyBird\Inertia\Inertia;

$products = array_map(function ($product) {
    return [
        'id'        => $product->get_id(),
        'name'      => $product->get_name(),
        'price'     => $product->get_price(),
        'image'     => $product->get_image(),
        'permalink' => $product->get_permalink(),
    ];
}, wc_get_products(['limit' => 10]));

return Inertia::render('Products/Index', [
    'products' => $products,
]);

The real lift here would be replicating Woocommerce pages like the cart and checkout using Inertia. Something I personally would not love to do.

The good news is that only templates where you return Inertia::render() are treated as Inertia pages. If you link to the cart or checkout page within an Inertia component using a standard <a> tag rather than the Inertia <Link> helper, all should work fine. Naturally, that click will perform a full page refresh, but not having to rebuild the complexity of those pages may be worth it.