Skip to content

Commit 8a93ec0

Browse files
committed
added shipping methods, added book model to test shipping costs
1 parent 0fd7201 commit 8a93ec0

File tree

38 files changed

+1443
-27
lines changed

38 files changed

+1443
-27
lines changed

app/base/commands/Generate/Model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ protected function getMigrationFileContents($className, $modelClassName, $migrat
272272
273273
class " . $className . " extends DBMigration
274274
{
275-
protected \$tableName = '" . $migration_table . "';
275+
protected string \$tableName = '" . $migration_table . "';
276276
277277
public function getName(): string
278278
{

app/base/commands/Generate/Product.php

Lines changed: 94 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,48 @@ class Product extends CodeGeneratorCommand
4545
'nullable' => true,
4646
'default_value' => null,
4747
],
48-
'name' => [
49-
'col_name' => 'name',
48+
'title' => [
49+
'col_name' => 'title',
5050
'php_type' => 'string',
5151
'mysql_type' => 'VARCHAR',
5252
'col_parameters' => [255],
5353
'col_options' => [],
5454
'nullable' => true,
5555
'default_value' => null,
5656
],
57+
'content' => [
58+
'col_name' => 'content',
59+
'php_type' => 'string',
60+
'mysql_type' => 'TEXT',
61+
'col_parameters' => null,
62+
'col_options' => [],
63+
'nullable' => true,
64+
'default_value' => null,
65+
],
5766
'tax_class_id' => [
5867
'col_name' => 'tax_class_id',
5968
'php_type' => 'int',
6069
'mysql_type' => 'INT',
6170
'col_parameters' => null,
62-
'col_options' => ['UNSIGNED'],
71+
'col_options' => ['\'UNSIGNED\''],
72+
'nullable' => true,
73+
'default_value' => null,
74+
],
75+
'website_id' => [
76+
'col_name' => 'website_id',
77+
'php_type' => 'int',
78+
'mysql_type' => 'INT',
79+
'col_parameters' => null,
80+
'col_options' => ['\'UNSIGNED\''],
81+
'nullable' => true,
82+
'default_value' => null,
83+
],
84+
'user_id' => [
85+
'col_name' => 'user_id',
86+
'php_type' => 'int',
87+
'mysql_type' => 'INT',
88+
'col_parameters' => null,
89+
'col_options' => ['\'UNSIGNED\''],
6390
'nullable' => true,
6491
'default_value' => null,
6592
],
@@ -72,6 +99,51 @@ class Product extends CodeGeneratorCommand
7299
'nullable' => true,
73100
'default_value' => null,
74101
],
102+
'url' => [
103+
'col_name' => 'url',
104+
'php_type' => 'string',
105+
'mysql_type' => 'VARCHAR',
106+
'col_parameters' => [255],
107+
'col_options' => [],
108+
'nullable' => true,
109+
'default_value' => null,
110+
],
111+
'locale' => [
112+
'col_name' => 'locale',
113+
'php_type' => 'string',
114+
'mysql_type' => 'VARCHAR',
115+
'col_parameters' => [10],
116+
'col_options' => [],
117+
'nullable' => true,
118+
'default_value' => null,
119+
],
120+
'meta_keywords' => [
121+
'col_name' => 'meta_keywords',
122+
'php_type' => 'string',
123+
'mysql_type' => 'VARCHAR',
124+
'col_parameters' => [1024],
125+
'col_options' => [],
126+
'nullable' => true,
127+
'default_value' => null,
128+
],
129+
'meta_description' => [
130+
'col_name' => 'meta_description',
131+
'php_type' => 'string',
132+
'mysql_type' => 'VARCHAR',
133+
'col_parameters' => [1024],
134+
'col_options' => [],
135+
'nullable' => true,
136+
'default_value' => null,
137+
],
138+
'html_title' => [
139+
'col_name' => 'html_title',
140+
'php_type' => 'string',
141+
'mysql_type' => 'VARCHAR',
142+
'col_parameters' => [1024],
143+
'col_options' => [],
144+
'nullable' => true,
145+
'default_value' => null,
146+
],
75147
];
76148

77149
/**
@@ -220,7 +292,7 @@ protected function askColumnInfo(): ?array
220292
}
221293

222294
$question = new Question('Column options (comma separated): ');
223-
$options = array_map("strtoupper", array_filter(array_map("trim", explode(",", $helper->ask($this->input, $this->output, $question)))));
295+
$options = array_map("strtoupper", array_filter(array_map("trim", explode(",", (string) $helper->ask($this->input, $this->output, $question)))));
224296
foreach ($options as $k => $option) {
225297
if (!is_numeric($option) && !empty($option) && $option != 'NULL') {
226298
$options[$k] = "'" . $option . "'";
@@ -263,11 +335,11 @@ protected function getModelFileContents($className): string
263335
264336
namespace App\\Site\\Models;
265337
266-
use App\\Base\\Abstracts\\Models\\BaseModel;
338+
use App\\Base\\Abstracts\\Models\\FrontendModel;
267339
use App\Base\Interfaces\Model\ProductInterface;
268340
269341
/**\n" . $comment . " */
270-
class " . $className . " extends BaseModel implements ProductInterface
342+
class " . $className . " extends FrontendModel implements ProductInterface
271343
{
272344
public function isPhysical(): bool
273345
{
@@ -291,12 +363,22 @@ public function getTaxClassId(): ?int
291363
292364
public function getName() : ?string
293365
{
294-
return \$this->getData('name');
366+
return \$this->getData('title');
295367
}
296368
297369
public function getSku(): string
298370
{
299-
return \$this->getData('sku')?? 'product_' . \$this->getId();
371+
return \$this->getData('sku')?? '".$this->getUtils()->pascalCaseToSnakeCase($className)."_' . \$this->getId();
372+
}
373+
374+
/**
375+
* {@inheritdoc}
376+
*
377+
* @return string
378+
*/
379+
public function getRewritePrefix(): string
380+
{
381+
return '".$this->getUtils()->pascalCaseToSnakeCase($className)."';
300382
}
301383
}
302384
";
@@ -338,7 +420,7 @@ protected function getMigrationFileContents($className, $modelClassName, $migrat
338420
339421
class " . $className . " extends DBMigration
340422
{
341-
protected \$tableName = '" . $migration_table . "';
423+
protected string \$tableName = '" . $migration_table . "';
342424
343425
public function getName(): string
344426
{
@@ -352,6 +434,9 @@ public function addDBTableDefinition(Table \$table): Table
352434
->addColumn('created_at', 'TIMESTAMP', null, [], false, 'CURRENT_TIMESTAMP()')
353435
->addColumn('updated_at', 'TIMESTAMP', null, [], false, 'CURRENT_TIMESTAMP()')
354436
->addIndex(null, 'id', Index::TYPE_PRIMARY)
437+
->addForeignKey('fk_".$migration_table."_website_id', ['website_id'], 'website', ['id'])
438+
->addForeignKey('fk_".$migration_table."_owner_id', ['user_id'], 'user', ['id'])
439+
->addForeignKey('fk_".$migration_table."_language_locale', ['locale'], 'language', ['locale'])
355440
->setAutoIncrementColumn('id');
356441
357442
return \$table;
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
/**
4+
* SiteBase
5+
* PHP Version 8.3
6+
*
7+
* @category CMS / Framework
8+
* @package Degami\Sitebase
9+
* @author Mirko De Grandis <degami@github.com>
10+
* @license MIT https://opensource.org/licenses/mit-license.php
11+
* @link https://github.com/degami/sitebase
12+
*/
13+
14+
namespace App\Base\Commerce\ShippingMethods;
15+
16+
use App\App;
17+
use App\Base\Interfaces\Commerce\ShippingMethodInterface;
18+
use Degami\PHPFormsApi as FAPI;
19+
use Degami\PHPFormsApi\Containers\SeamlessContainer;
20+
use App\Base\Models\Cart;
21+
use App\Base\Models\CartItem;
22+
use Degami\PHPFormsApi\Accessories\FormValues;
23+
24+
class FlatRate implements ShippingMethodInterface
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function getCode() : string
30+
{
31+
return 'flatrate';
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function getName(): string
38+
{
39+
return 'Flat Rate';
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function isActive(Cart $cart): bool
46+
{
47+
return App::getInstance()->getSiteData()->getConfigValue('shipping/flatrate/active') == true;
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function isApplicable(Cart $cart): bool
54+
{
55+
return $cart->requireShipping();
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function getConfigurationForm(FAPI\Form $form, array &$form_state) : FAPI\Form
62+
{
63+
$form
64+
->addField('cost', [
65+
'type' => 'number',
66+
'title' => 'Shipping Cost',
67+
'min' => 0,
68+
'max' => PHP_INT_MAX,
69+
'step' => 0.1,
70+
'default_value' => App::getInstance()->getSiteData()->getConfigValue('shipping/flatrate/cost'),
71+
])
72+
->addField('apply_to', [
73+
'type' => 'select',
74+
'title' => 'Apply cost to',
75+
'options' => [
76+
'cart' => 'Once',
77+
'cart_items' => 'Once per cart item',
78+
'products' => 'Each product (with qty)',
79+
],
80+
'default_value' => App::getInstance()->getSiteData()->getConfigValue('shipping/flatrate/apply_to'),
81+
]);
82+
return $form;
83+
}
84+
85+
/**
86+
* {@inheritdoc}
87+
*/
88+
public function getShippingFormFieldset(Cart $cart, FAPI\Form $form, array &$form_state) : FAPI\Interfaces\FieldsContainerInterface
89+
{
90+
/** @var SeamlessContainer $out */
91+
$out = $form->getFieldObj('flatrate', [
92+
'type' => 'seamless_container',
93+
]);
94+
95+
$totalCosts = $this->calculateShipping(null, $cart)['shipping_cost'];
96+
$totalCostsFormatted = App::getInstance()->getUtils()->formatPrice($totalCosts, $cart->getCurrencyCode());
97+
98+
$out->addField('shipping_costs', [
99+
'type' => 'markup',
100+
'value' => App::getInstance()->getUtils()->translate("Total Shipping costs: %s", [$totalCostsFormatted]),
101+
]);
102+
103+
return $out;
104+
}
105+
106+
/**
107+
* {@inheritdoc}
108+
*/
109+
public function calculateShipping(?FormValues $values, Cart $cart) : array
110+
{
111+
$cost = App::getInstance()->getSiteData()->getConfigValue('shipping/flatrate/cost');
112+
$applyTo = App::getInstance()->getSiteData()->getConfigValue('shipping/flatrate/apply_to');
113+
114+
$totalCosts = match($applyTo) {
115+
'cart_items' => $cost * count(array_filter($cart->getItems(), fn(CartItem $item) => $item->requireShipping())),
116+
'products' => $cost * array_sum(array_map(fn(CartItem $item) => $item->requireShipping() ? $item->getQuantity() : 0, $cart->getItems())),
117+
'cart' => $cost,
118+
default => $cost,
119+
};
120+
121+
return [
122+
'shipping_cost' => $totalCosts,
123+
'additional_data' => ['calculate_when' => time()]
124+
];
125+
}
126+
}

0 commit comments

Comments
 (0)