Custom Fields
v1.xCart custom fields package for Basketin — attach arbitrary key-value metadata to any cart in Laravel e-commerce.
Cart Fields
Cart custom fields package for Basketin — attach arbitrary key-value metadata to any cart in Laravel e-commerce.
Installation
composer require basketin/cart-fields
The service provider is auto-discovered by Laravel.
Usage
Setting a Field
Use the fields() method on the cart instance to store custom data:
use Obelaw\Basketin\Cart\Facades\Cart;
$cart = Cart::make('cart_id', 'USD');
$cart->fields()->set('customer_name', 'John Doe');
Values can be strings, arrays, booleans, or any serialisable type:
$cart->fields()->set('shipping_address', [
'city' => 'Cairo',
'country' => 'Egypt',
]);
$cart->fields()->set('gift_wrap', true);
Setting a field that already exists will overwrite the previous value:
$cart->fields()->set('customer_note', 'First note');
$cart->fields()->set('customer_note', 'Updated note');
// 'customer_note' now holds 'Updated note'
Getting a Field
Retrieve a single field by key:
$name = $cart->fields()->get('customer_name');
// 'John Doe'
You can provide a default value for keys that may not exist:
$value = $cart->fields()->get('non_existing', 'default');
// 'default'
Checking for a Field
$cart->fields()->has('customer_email'); // true / false
Removing a Field
Remove a single field by key:
$cart->fields()->forget('temporary_field');
Getting All Fields
Retrieve every field as a key-value array:
$cart->fields()->set('field1', 'value1');
$cart->fields()->set('field2', 'value2');
$cart->fields()->set('field3', 'value3');
$all = $cart->fields()->all();
// ['field1' => 'value1', 'field2' => 'value2', 'field3' => 'value3']
Flushing All Fields
Remove all fields at once:
$cart->fields()->flush();
$cart->fields()->all(); // []
Using Fields with Cart Quotes
Custom fields work alongside the quote system, so you can annotate a cart that already contains products:
$cart = Cart::make('cart_id', 'USD');
$cart->quote()->addQuote($product, 1);
$cart->fields()->set('gift_wrap', true);
$cart->fields()->get('gift_wrap'); // true
$cart->getCountProducts(); // 1
API Reference
FieldsService
Accessed via $cart->fields().
Methods:
| Method | Description |
|---|---|
set(string $key, mixed $value) | Store a field value. Overwrites if the key already exists. |
get(string $key, mixed $default = null) | Retrieve a field value, or $default if the key is not set. |
has(string $key): bool | Check whether a field exists. |
forget(string $key) | Remove a single field. |
all(): array | Return all fields as a key-value array. |
flush() | Remove all fields from the cart. |