Updating cart using AlpineJS only #1036
-
Hey! I am trying to update item quantity in the cart view using AJAX via AlpineJS, but some of the needed data like |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
While Simple Commerce does have a dedicated endpoint to return the cart & its line items (GET If you need that (or any other bits of information not returned by that endpoint), you'd have to make your own controller & route: // app/Http/Controllers/CartController.php
<?php
namespace App\Http\Controllers;
use DuncanMcClean\SimpleCommerce\Orders\Cart\Drivers\CartDriver;
use Illuminate\Http\Request;
class CartController extends Controller
{
use CartDriver;
public function __invoke(Request $request)
{
$order = $this->getCart();
if (! $order) {
abort(404);
}
return response()->json([
'order_number' => $order->orderNumber(),
'items' => $order->lineItems()
->map(function ($lineItem) {
return array_merge($lineItem->toArray(), [
'total_including_tax' => $lineItem->totalIncludingTax(),
]);
})
->all(),
// ... anything else you want to return
// see the docs for reference: https://simple-commerce.duncanmcclean.com/php-api#content-orders
]);
}
} // routes/web.php
use App\Http\Controllers\CartController;
Route::get('/cart-json', CartController::class); |
Beta Was this translation helpful? Give feedback.
While Simple Commerce does have a dedicated endpoint to return the cart & its line items (GET
!/simple-commerce/cart
), it won't include thetotal_including_tax
bit you're after (since it's a tag, not actually part of the augmentation).If you need that (or any other bits of information not returned by that endpoint), you'd have to make your own controller & route: