Skip to content

Commit

Permalink
Docs: fix example usage of addLines (#1429)
Browse files Browse the repository at this point in the history
Fixes a typo in the example of CartSession::addLines usage.

The implementation of
[addLines](https://github.com/lunarphp/lunar/blob/7f41cf66ebc96ee7465a84c8a4c4dd24228bf47d/packages/core/src/Models/Cart.php#L337C10-L337C10)
currently expects a purchasable instance as below:
```php
public function add(Purchasable $purchasable, int $quantity = 1, array $meta = [], bool $refresh = true): Cart
{
    foreach (config('lunar.cart.validators.add_to_cart', []) as $action) {
        // Throws a validation exception?
        app($action)->using(
            cart: $this,
            purchasable: $purchasable,
            quantity: $quantity,
            meta: $meta
        )->validate();
    }

    return app(
        config('lunar.cart.actions.add_to_cart', AddOrUpdatePurchasable::class)
    )->execute($this, $purchasable, $quantity, $meta)
        ->then(fn () => $refresh ? $this->refresh()->calculate() : $this);
}

public function addLines(iterable $lines): Cart
{
    DB::transaction(function () use ($lines) {
        collect($lines)->each(function ($line) {
            $this->add(
                purchasable: $line['purchasable'],
                quantity: $line['quantity'],
                meta: (array) ($line['meta'] ?? null),
                refresh: false
            );
        });
    });

    return $this->refresh()->calculate();
}
```

Co-authored-by: Alec Ritson <hello@itsalec.co.uk>
  • Loading branch information
se09deluca and alecritson authored Jan 11, 2024
1 parent ff999fe commit f7919a4
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion docs/core/reference/carts.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ CartSession::add($purchasable, $quantity);
```php
CartSession::addLines([
[
'id' => 1,
'purchasable' => \Lunar\Models\ProductVariant::find(123),
'quantity' => 25,
'meta' => ['foo' => 'bar'],
],
Expand Down

1 comment on commit f7919a4

@vercel
Copy link

@vercel vercel bot commented on f7919a4 Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.