@@ -16,41 +16,77 @@ class User extends Model implements Customer
1616
1717## Item Model
1818
19- Add the ` HasWallet ` trait and ` Product ` interface to Item model.
19+ Add the ` HasWallet ` trait and interface to ` Item ` model.
2020
21+ Starting from version 9.x there are two product interfaces:
22+ - For an unlimited number of products (` ProductLimitedInterface ` );
23+ - For a limited number of products (` ProductInterface ` );
24+
25+ An example with an unlimited number of products:
2126``` php
2227use Bavix\Wallet\Traits\HasWallet;
23- use Bavix\Wallet\Interfaces\Product;
2428use Bavix\Wallet\Interfaces\Customer;
29+ use Bavix\Wallet\Interfaces\ProductInterface;
2530
26- class Item extends Model implements Product
31+ class Item extends Model implements ProductInterface
32+ {
33+ use HasWallet;
34+
35+ public function getAmountProduct(Customer $customer)
36+ {
37+ return 100;
38+ }
39+
40+ public function getMetaProduct(): ?array
41+ {
42+ return [
43+ 'title' => $this->title,
44+ 'description' => 'Purchase of Product #' . $this->id,
45+ ];
46+ }
47+ }
48+ ```
49+
50+ Example with a limited number of products:
51+ ``` php
52+ use Bavix\Wallet\Traits\HasWallet;
53+ use Bavix\Wallet\Interfaces\Customer;
54+ use Bavix\Wallet\Interfaces\ProductLimitedInterface;
55+
56+ class Item extends Model implements ProductLimitedInterface
2757{
2858 use HasWallet;
2959
3060 public function canBuy(Customer $customer, int $quantity = 1, bool $force = false): bool
3161 {
3262 /**
63+ * This is where you implement the constraint logic.
64+ *
3365 * If the service can be purchased once, then
3466 * return !$customer->paid($this);
3567 */
3668 return true;
3769 }
38-
70+
3971 public function getAmountProduct(Customer $customer)
4072 {
41- return round($this->price * 100) ;
73+ return 100;
4274 }
4375
4476 public function getMetaProduct(): ?array
4577 {
4678 return [
4779 'title' => $this->title,
48- 'description' => 'Purchase of Product #' . $this->getKey(),
80+ 'description' => 'Purchase of Product #' . $this->id,
4981 ];
5082 }
5183}
5284```
5385
86+ I do not recommend using the limited interface when working with a shopping cart.
87+ If you are working with a shopping cart, then you should override the ` PurchaseServiceInterface ` interface.
88+ With it, you can check the availability of all products with one request, there will be no N-queries in the database.
89+
5490## Fill the cart
5591
5692Find the user and check the balance.
0 commit comments