Skip to content

Commit 5f101ec

Browse files
committed
🚧 wip
1 parent 242634a commit 5f101ec

File tree

9 files changed

+137
-27
lines changed

9 files changed

+137
-27
lines changed

blueprints/tabs/product-local.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fields:
2+
layout:
3+
type: layout
4+
layouts:
5+
- "1/1"
6+
- "1/2, 1/2"

index.js

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Bnomei\Kart\Cart;
1515
use Bnomei\Kart\CartLine;
1616
use Bnomei\Kart\Kart;
17-
use Bnomei\Kart\Kerbs;
1817
use Bnomei\Kart\License;
1918
use Bnomei\Kart\MagicLinkChallenge;
2019
use Bnomei\Kart\Router;
@@ -474,6 +473,7 @@ function kerbs(?array $props = null, ?string $template = null): void
474473
'pages/products' => ProductsPage::phpBlueprint(),
475474
'pages/stock' => StockPage::phpBlueprint(),
476475
'pages/stocks' => StocksPage::phpBlueprint(),
476+
'tabs/product-local' => __DIR__.'/blueprints/tabs/product-local.yml',
477477
],
478478
'templates' => [
479479
'cart' => __DIR__.'/templates/cart.php',

models/OrderPage.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ public static function phpBlueprint(): array
101101
{
102102
return [
103103
'name' => 'order',
104+
'image' => [
105+
'back' => 'var(--color-black)',
106+
'color' => 'var(--color-gray-500)',
107+
'cover' => true,
108+
'icon' => 'kart-order',
109+
'query' => false,
110+
],
104111
'options' => [
105112
'preview' => '{{page.urlWithSignature}}',
106113
'changeSlug' => false,

models/OrdersPage.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public static function phpBlueprint(): array
2323
{
2424
return [
2525
'name' => 'orders',
26+
'image' => [
27+
'back' => 'var(--color-black)',
28+
'color' => 'var(--color-gray-500)',
29+
'cover' => true,
30+
'icon' => 'kart-orders',
31+
'query' => false,
32+
],
2633
'options' => [
2734
'preview' => false,
2835
'changeSlug' => false,

models/ProductPage.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public static function phpBlueprint(): array
6767
return [
6868
'name' => 'product',
6969
'num' => '{{ page.created.toDate("YmdHis") }}',
70+
'image' => [
71+
'icon' => 'blank',
72+
'cover' => true,
73+
'query' => 'page.gallery.toFiles.first',
74+
],
7075
'options' => [
7176
'changeTemplate' => false,
7277
],
@@ -85,11 +90,11 @@ public static function phpBlueprint(): array
8590
[
8691
'label' => 'bnomei.kart.sales-count',
8792
'value' => '{{ page.salesCount }}',
88-
'link' => '{{ site.kart.page("orders").url }}',
93+
'link' => '{{ site.kart.page("orders")?.panel.url }}',
8994
],
9095
[
9196
'label' => 'bnomei.kart.stock',
92-
'value' => '{{ page.stock(null, "*") }}',
97+
'value' => '{{ page.stockWithVariants }}',
9398
'link' => '{{ page.stockUrl }}',
9499
],
95100
],
@@ -291,7 +296,7 @@ public static function phpBlueprint(): array
291296
'local' => [
292297
'label' => 'bnomei.kart.local-storage',
293298
'icon' => 'server',
294-
'extends' => 'tabs/product',
299+
'extends' => 'tabs/product-local',
295300
],
296301
],
297302
];
@@ -309,20 +314,27 @@ public function storage(): Storage
309314
*/
310315
public function inStock(): bool
311316
{
312-
$mainStock = $this->stock();
313-
if (is_string($mainStock)) {
314-
return true;
315-
}
316-
if (is_numeric($mainStock) && $mainStock > 0) {
317+
$stock = $this->stockWithVariants();
318+
if (is_string($stock)) {
317319
return true;
318320
}
319321

320-
$variantsStock = $this->stock(variant: '*');
321-
if (is_numeric($variantsStock) && $variantsStock > 0) {
322-
return true;
322+
return $stock > 0;
323+
}
324+
325+
/**
326+
* @kql-allowed
327+
*/
328+
public function stockWithVariants(): int|string
329+
{
330+
$self = $this->stock();
331+
$variants = $this->stock(variant: '*');
332+
333+
if (is_string($self) || is_string($variants)) {
334+
return $self;
323335
}
324336

325-
return false;
337+
return $self + $variants;
326338
}
327339

328340
/**

models/ProductsPage.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public static function phpBlueprint(): array
1818
{
1919
return [
2020
'name' => 'products',
21+
'image' => [
22+
'back' => 'var(--color-black)',
23+
'color' => 'var(--color-gray-500)',
24+
'cover' => true,
25+
'icon' => 'kart-store',
26+
'query' => false,
27+
],
2128
'options' => [
2229
'changeSlug' => false,
2330
'changeTemplate' => false,
@@ -45,6 +52,11 @@ public static function phpBlueprint(): array
4552
'label' => 'bnomei.kart.products',
4653
'value' => '{{ page.children.count }}',
4754
],
55+
[
56+
'label' => 'bnomei.kart.out-of-stock',
57+
'value' => '{{ page.outOfStock.count }}',
58+
'link' => '{{ site.kart.page("stocks")?.panel.url }}',
59+
],
4860
[
4961
'label' => 'bnomei.kart.provider',
5062
'value' => '{{ site.kart.provider.title }}',
@@ -83,7 +95,7 @@ public static function phpBlueprint(): array
8395
'layout' => 'cards',
8496
'search' => true,
8597
'template' => 'product', // maps to ProductPage model
86-
'info' => '{{ page.formattedPrice }} [{{ page.stock(null, "*") }}] {{ page.featured.ecco("★") }} {{ page.variants.ecco("❖") }}',
98+
'info' => '{{ page.formattedPrice }} [{{ page.stock }}]{{ page.inStock ? "" : " ⚠️" }}{{ page.featured.ecco(" ★") }}{{ page.variants.ecco(" ❖") }}',
8799
'image' => [
88100
'cover' => true,
89101
'query' => 'page.gallery.first.toFile',
@@ -146,4 +158,12 @@ public function tags(): Field
146158
{
147159
return new Field($this, 'tags', implode(',', kart()->allTags()));
148160
}
161+
162+
public function outOfStock(): Pages
163+
{
164+
return $this->children()->filterBy(function (ProductPage $p) {
165+
$stock = $p->stockWithVariants();
166+
return !(is_string($stock) || $stock > 0);
167+
});
168+
}
149169
}

models/StockPage.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public static function phpBlueprint(): array
4949
{
5050
return [
5151
'name' => 'stock',
52+
'image' => [
53+
'back' => 'var(--color-black)',
54+
'color' => 'var(--color-gray-500)',
55+
'cover' => true,
56+
'icon' => 'kart-stock',
57+
'query' => false,
58+
],
5259
'options' => [
5360
'changeSlug' => false,
5461
'changeTitle' => false,
@@ -64,10 +71,16 @@ public static function phpBlueprint(): array
6471
'type' => 'pages',
6572
'query' => 'site.kart.page("products")', // TODO: .withoutStocks does not work
6673
// 'required' => true,
74+
'info' => '{{ page.formattedPrice }}{{ page.featured.ecco(" ★") }}{{ page.variants.ecco(" ❖") }}',
75+
// 'layout' => 'cards',
6776
'multiple' => false,
6877
'subpages' => false,
6978
'translate' => false,
70-
'width' => '2/3',
79+
'width' => '1/4',
80+
'image' => [
81+
'cover' => true,
82+
'query' => 'page.gallery.toFiles.first',
83+
]
7184
],
7285
'timestamp' => [
7386
'label' => 'bnomei.kart.timestamp',
@@ -76,7 +89,11 @@ public static function phpBlueprint(): array
7689
'time' => true,
7790
'default' => 'now',
7891
'translate' => false,
79-
'width' => '1/3',
92+
'width' => '1/4',
93+
],
94+
'gap1' => [
95+
'type' => 'gap',
96+
'width' => '1/2',
8097
],
8198
'stock' => [
8299
'label' => 'bnomei.kart.stock',
@@ -86,14 +103,14 @@ public static function phpBlueprint(): array
86103
'step' => 1,
87104
'default' => 0,
88105
'translate' => false,
89-
'width' => '1/2',
106+
'width' => '1/4',
90107
],
91108
'variants' => [
92109
'label' => 'bnomei.kart.variants',
93110
'type' => 'structure',
94111
'after' => '{{ kirby.option("bnomei.kart.currency") }}',
95112
'translate' => false,
96-
'width' => '1/2',
113+
'width' => '3/4',
97114
'fields' => [
98115
'variant' => [
99116
'label' => 'bnomei.kart.variant',
@@ -108,7 +125,6 @@ public static function phpBlueprint(): array
108125
],
109126
],
110127
],
111-
112128
],
113129
];
114130
}
@@ -133,9 +149,12 @@ public function onlyOneStockPagePerProduct(array $values): bool
133149
*/
134150
public function stockPad(int $length): string
135151
{
136-
/** @var StocksPage $stocks */
137-
$stocks = $this->parent();
138-
$stock = $stocks->stock($this->page()->toPage()->uuid()->toString(), variant: '*');
152+
/** @var ProductPage $product */
153+
$product = $this->page()->toPage();
154+
$stock = $product?->stockWithVariants();
155+
if (is_string($stock)) {
156+
return $stock;
157+
}
139158

140159
return str_pad((string) $stock, $length, '0', STR_PAD_LEFT);
141160
}

models/StocksPage.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public static function phpBlueprint(): array
1919
{
2020
return [
2121
'name' => 'stocks',
22+
'image' => [
23+
'back' => 'var(--color-black)',
24+
'color' => 'var(--color-gray-500)',
25+
'cover' => true,
26+
'icon' => 'kart-stocks',
27+
'query' => false,
28+
],
2229
'options' => [
2330
'preview' => false,
2431
'changeSlug' => false,
@@ -37,7 +44,11 @@ public static function phpBlueprint(): array
3744
[
3845
'label' => 'bnomei.kart.products',
3946
'value' => '{{ page.children.count }}',
40-
'link' => '{{ site.kart.page("products").panel.url }}',
47+
'link' => '{{ site.kart.page("products")?.panel.url }}',
48+
],
49+
[
50+
'label' => 'bnomei.kart.out-of-stock',
51+
'value' => '{{ site.kart.page("products")?.outOfStock.count }}',
4152
],
4253
[
4354
'label' => 'bnomei.kart.stocks',
@@ -60,10 +71,10 @@ public static function phpBlueprint(): array
6071
'stocks' => [
6172
'label' => 'bnomei.kart.stocks',
6273
'type' => 'pages',
63-
'search' => true,
74+
// 'search' => true,
6475
'template' => 'stock', // maps to StockPage model
6576
'sortBy' => 'timestamp desc',
66-
'text' => '[{{ page.stockPad(3) }}] {{ page.page.toPage.title }}',
77+
'text' => '{{ page.page.toPage.inStock ? "" : "⚠️ " }}[{{ page.stockPad(3) }}] {{ page.page.toPage.title }}',
6778
'info' => '{{ page.title }} ・ {{ page.timestamp }}',
6879
'limit' => 1000,
6980
],
@@ -101,8 +112,11 @@ public function stock(?string $id = null, ?string $withHold = null, ?string $var
101112
}
102113
}
103114
$t += $c;
115+
if ($c > 0) {
116+
$stocks[$page->uuid()->toString().'|*'] = $c;
117+
}
104118
if ($p + $c > 0) {
105-
$stocks[$page->uuid()->toString().'|*'] = $p + $c;
119+
$stocks[$page->uuid()->toString().'|='] = $p + $c;
106120
}
107121
}
108122
if ($t > 0) {
@@ -116,7 +130,7 @@ public function stock(?string $id = null, ?string $withHold = null, ?string $var
116130
} else {
117131
// slowish...
118132
$stocks = $this->stockPages($id);
119-
$stock = $stocks->count() ? $stocks->sumField('stock')->toInt() : null;
133+
$stock = $stocks->count() && !$variant ? $stocks->sumField('stock')->toInt() : null;
120134
foreach ($stocks as $p) {
121135
foreach ($p->variants()->toStructure() as $var) {
122136
$v = $var->variant()->split();

0 commit comments

Comments
 (0)