Merx – Plugin to create online shops with Kirby 3

this is my solution for updating quantity if item already exists in cart


    [
      'pattern' => 'add',
      'method' => 'post',
      'action'  => function () {
        $id = get('id');
        $quantity = get('quantity');
        $existingCartItem = cart()->find($id);
        try {
          if ($existingCartItem) {
            kirby()->session()->set('ww.merx.cartMessage', 'Quantity Updated.');
            $quantity = $existingCartItem['quantity'] + $quantity;
          }
          cart()->add([
            'id' => $id,
            'quantity' => $quantity,
          ]);
          go(option('ww.merx.bgn.cartPage'));
        } catch (Exception $ex) {
          return $ex->getMessage();
        }
      },
    ],

2 Likes