Skip to content

Commit 2f2c20c

Browse files
authored
Merge pull request PrestaShop#37911 from tleon/Issue-37796-cart-rule-refacto-command-free-shipping
Cart rule refacto command free shipping
2 parents e8ef9ae + 93de280 commit 2f2c20c

File tree

22 files changed

+1905
-655
lines changed

22 files changed

+1905
-655
lines changed

classes/CartRule.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class CartRuleCore extends ObjectModel
105105
public $active = true;
106106
public $date_add;
107107
public $date_upd;
108+
public ?string $type = null;
108109

109110
protected static $cartAmountCache = [];
110111

@@ -148,6 +149,7 @@ class CartRuleCore extends ObjectModel
148149
'active' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
149150
'date_add' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
150151
'date_upd' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
152+
'type' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
151153
/* Lang fields */
152154
'name' => [
153155
'type' => self::TYPE_HTML,
@@ -511,8 +513,8 @@ public static function getCustomerCartRules(
511513
* in the cart rule. So he will be able to use it.
512514
*/
513515
$validAddressExists = Db::getInstance()->getValue('
514-
SELECT crc.id_cart_rule
515-
FROM ' . _DB_PREFIX_ . 'cart_rule_country crc
516+
SELECT crc.id_cart_rule
517+
FROM ' . _DB_PREFIX_ . 'cart_rule_country crc
516518
INNER JOIN ' . _DB_PREFIX_ . 'address a
517519
ON a.id_customer = ' . (int) $id_customer . ' AND
518520
a.deleted = 0 AND

install-dev/data/db_structure.sql

+3-1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ CREATE TABLE `PREFIX_cart_rule` (
197197
`active` tinyint(1) unsigned NOT NULL DEFAULT '0',
198198
`date_add` datetime NOT NULL,
199199
`date_upd` datetime NOT NULL,
200+
`type` varchar(128) DEFAULT NULL,
200201
PRIMARY KEY (`id_cart_rule`),
201202
KEY `id_customer` (
202203
`id_customer`, `active`, `date_to`
@@ -213,7 +214,8 @@ CREATE TABLE `PREFIX_cart_rule` (
213214
`date_to`
214215
),
215216
KEY `date_from` (`date_from`),
216-
KEY `date_to` (`date_to`)
217+
KEY `date_to` (`date_to`),
218+
KEY `type` (`type`)
217219
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8mb4 COLLATION;
218220

219221
/* Localized name assocatied with a promo code */
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Open Software License (OSL 3.0)
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/OSL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to [email protected] so we can send you a copy immediately.
15+
*
16+
* DISCLAIMER
17+
*
18+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19+
* versions in the future. If you wish to customize PrestaShop for your
20+
* needs please refer to https://devdocs.prestashop.com/ for more information.
21+
*
22+
* @author PrestaShop SA and Contributors <[email protected]>
23+
* @copyright Since 2007 PrestaShop SA and Contributors
24+
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25+
*/
26+
27+
namespace PrestaShop\PrestaShop\Adapter\CartRule;
28+
29+
use CartRule;
30+
use DateTimeImmutable;
31+
use PrestaShop\PrestaShop\Core\Domain\Discount\Command\AddDiscountCommand;
32+
use PrestaShop\PrestaShop\Core\Domain\Discount\Command\AddFreeShippingDiscountCommand;
33+
use PrestaShop\PrestaShop\Core\Util\DateTime\DateTime as DateTimeUtil;
34+
35+
class CartRuleBuilder
36+
{
37+
public function build(AddDiscountCommand $command): CartRule
38+
{
39+
$cartRule = new CartRule();
40+
$validFrom = $command->getValidFrom() ?: new DateTimeImmutable();
41+
$validTo = $command->getValidTo() ?: $validFrom->modify('+1 month');
42+
43+
$cartRule->name = $command->getLocalizedNames();
44+
$cartRule->description = $command->getDescription();
45+
$cartRule->code = $command->getCode();
46+
$cartRule->highlight = $command->isHighlightInCart();
47+
$cartRule->partial_use = $command->allowPartialUse();
48+
$cartRule->priority = $command->getPriority();
49+
$cartRule->active = $command->isActive();
50+
$cartRule->id_customer = $command->getCustomerId()?->getValue();
51+
$cartRule->date_from = $validFrom->format(DateTimeUtil::DEFAULT_DATETIME_FORMAT);
52+
$cartRule->date_to = $validTo->format(DateTimeUtil::DEFAULT_DATETIME_FORMAT);
53+
$cartRule->quantity = $command->getTotalQuantity();
54+
$cartRule->quantity_per_user = $command->getQuantityPerUser();
55+
$cartRule->type = $command->getDiscountType()->getValue();
56+
$cartRule->free_shipping = $command instanceof AddFreeShippingDiscountCommand;
57+
58+
return $cartRule;
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Open Software License (OSL 3.0)
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/OSL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to [email protected] so we can send you a copy immediately.
15+
*
16+
* DISCLAIMER
17+
*
18+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19+
* versions in the future. If you wish to customize PrestaShop for your
20+
* needs please refer to https://devdocs.prestashop.com/ for more information.
21+
*
22+
* @author PrestaShop SA and Contributors <[email protected]>
23+
* @copyright Since 2007 PrestaShop SA and Contributors
24+
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25+
*/
26+
27+
namespace PrestaShop\PrestaShop\Adapter\Discount\CommandHandler;
28+
29+
use PrestaShop\PrestaShop\Adapter\CartRule\CartRuleBuilder;
30+
use PrestaShop\PrestaShop\Adapter\CartRule\Repository\CartRuleRepository;
31+
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
32+
use PrestaShop\PrestaShop\Core\Domain\Discount\Command\AddFreeShippingDiscountCommand;
33+
use PrestaShop\PrestaShop\Core\Domain\Discount\CommandHandler\AddFreeShippingDiscountHandlerInterface;
34+
use PrestaShop\PrestaShop\Core\Domain\Discount\ValueObject\DiscountId;
35+
36+
#[AsCommandHandler]
37+
class AddFreeShippingDiscountHandler implements AddFreeShippingDiscountHandlerInterface
38+
{
39+
public function __construct(
40+
private readonly CartRuleRepository $cartRuleRepository,
41+
private readonly CartRuleBuilder $cartRuleBuilder
42+
) {
43+
}
44+
45+
public function handle(AddFreeShippingDiscountCommand $command): DiscountId
46+
{
47+
$BuiltCartRule = $this->cartRuleBuilder->build($command);
48+
$discount = $this->cartRuleRepository->add($BuiltCartRule);
49+
50+
return new DiscountId((int) $discount->id);
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Open Software License (OSL 3.0)
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/OSL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to [email protected] so we can send you a copy immediately.
15+
*
16+
* DISCLAIMER
17+
*
18+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19+
* versions in the future. If you wish to customize PrestaShop for your
20+
* needs please refer to https://devdocs.prestashop.com/ for more information.
21+
*
22+
* @author PrestaShop SA and Contributors <[email protected]>
23+
* @copyright Since 2007 PrestaShop SA and Contributors
24+
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25+
*/
26+
27+
namespace PrestaShop\PrestaShop\Adapter\Discount\QueryHandler;
28+
29+
use DateTimeImmutable;
30+
use Exception;
31+
use PrestaShop\PrestaShop\Adapter\CartRule\Repository\CartRuleRepository;
32+
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsQueryHandler;
33+
use PrestaShop\PrestaShop\Core\Domain\CartRule\ValueObject\CartRuleId;
34+
use PrestaShop\PrestaShop\Core\Domain\Discount\Query\GetDiscountForEditing;
35+
use PrestaShop\PrestaShop\Core\Domain\Discount\QueryHandler\GetDiscountForEditingHandlerInterface;
36+
use PrestaShop\PrestaShop\Core\Domain\Discount\QueryResult\DiscountForEditing;
37+
38+
#[AsQueryHandler]
39+
class GetDiscountForEditingHandler implements GetDiscountForEditingHandlerInterface
40+
{
41+
public function __construct(
42+
protected readonly CartRuleRepository $cartRuleRepository
43+
) {
44+
}
45+
46+
/**
47+
* @throws Exception
48+
*/
49+
public function handle(GetDiscountForEditing $query): DiscountForEditing
50+
{
51+
$cartRuleId = new CartRuleId($query->discountId->getValue());
52+
$cartRule = $this->cartRuleRepository->get($cartRuleId);
53+
54+
return new DiscountForEditing(
55+
$query->discountId->getValue(),
56+
$cartRule->priority,
57+
$cartRule->active,
58+
new DateTimeImmutable($cartRule->date_from),
59+
new DateTimeImmutable($cartRule->date_to),
60+
$cartRule->quantity,
61+
$cartRule->quantity_per_user,
62+
$cartRule->description,
63+
$cartRule->code,
64+
(int) $cartRule->id_customer,
65+
$cartRule->highlight,
66+
$cartRule->partial_use,
67+
$cartRule->type
68+
);
69+
}
70+
}

0 commit comments

Comments
 (0)