Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56ec30e

Browse files
nicosombjolelievre
authored andcommitted
Hide expiration date field when the 'period never expires' option is checked
fix review add test Fix for ps_apiresources testsuite update ps_apiresources Fix review Small improvement on FormFieldToggler update module ps_apiresources
1 parent dc1900e commit 56ec30e

22 files changed

Lines changed: 198 additions & 92 deletions

File tree

admin-dev/themes/new-theme/js/components/form/form-field-toggler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export default class FormFieldToggler {
113113
this.toggle(targetSelector, disabledState, switchEvent);
114114
}
115115

116-
private getInputValue(inputElement: HTMLInputElement): string | undefined {
116+
private getInputValue(inputElement: HTMLInputElement): string | false | undefined {
117117
switch (inputElement.type) {
118118
case 'radio': {
119119
const checkedRadios = document.querySelectorAll<HTMLInputElement>(`[name="${inputElement.name}"]`);
@@ -127,7 +127,7 @@ export default class FormFieldToggler {
127127
return checkedValue;
128128
}
129129
case 'checkbox':
130-
return inputElement.checked ? inputElement.value : undefined;
130+
return inputElement.checked ? inputElement.value : false;
131131
default:
132132
return inputElement.value;
133133
}

admin-dev/themes/new-theme/js/pages/discount/discount-map.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ export default {
2828
productSegmentFeatures: '#discount_conditions_product_product_segment_features',
2929
quantityPerCustomerInput: '#discount_usability_quantity_per_customer',
3030
customerEligibilityInput: '#discount_customer_eligibility_eligibility',
31+
periodNeverExpiresCheckbox: 'input[name*="[period_never_expires]"]',
32+
periodExpiryDateFormGroup: '.date-range.row .col:has(.date-range-end-date)',
3133
};

admin-dev/themes/new-theme/js/pages/discount/form/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* docs/licenses/LICENSE.txt file that was distributed with this source code.
44
*/
55

6+
import FormFieldToggler, {ToggleType} from '@components/form/form-field-toggler';
67
import PriceReductionManager from '@components/form/price-reduction-manager';
78
import DiscountMap from '@pages/discount/discount-map';
89
import CreateFreeGiftDiscount from '@pages/discount/form/create-free-gift-discount';
@@ -75,6 +76,14 @@ $(() => {
7576
$(DiscountMap.quantityPerCustomerInput).parents('.form-group').show();
7677
}
7778

79+
new FormFieldToggler({
80+
disablingInputSelector: DiscountMap.periodNeverExpiresCheckbox,
81+
matchingValue: '1',
82+
disableOnMatch: true,
83+
targetSelector: DiscountMap.periodExpiryDateFormGroup,
84+
toggleType: ToggleType.visibility,
85+
});
86+
7887
$(DiscountMap.countriesSelect).select2({
7988
templateResult: formatOption,
8089
templateSelection: formatOption,

classes/CartRule.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PrestaShop\PrestaShop\Core\Domain\Discount\ValueObject\DiscountType;
1111
use PrestaShop\PrestaShop\Core\FeatureFlag\FeatureFlagSettings;
1212
use PrestaShop\PrestaShop\Core\FeatureFlag\FeatureFlagStateCheckerInterface;
13+
use PrestaShop\PrestaShop\Core\Util\DateTime\DateTime as DateTimeUtil;
1314

1415
/**
1516
* Class CartRuleCore.
@@ -46,7 +47,7 @@ class CartRuleCore extends ObjectModel
4647
/**
4748
* @var string|null
4849
*/
49-
public $date_to;
50+
public $date_to = null;
5051
public $description;
5152
public $quantity = 1;
5253
public ?int $total_quantity = null;
@@ -111,7 +112,7 @@ class CartRuleCore extends ObjectModel
111112
'fields' => [
112113
'id_customer' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId'],
113114
'date_from' => ['type' => self::TYPE_DATE, 'validate' => 'isDate', 'required' => true],
114-
'date_to' => ['type' => self::TYPE_DATE, 'validate' => 'isDate', 'required' => true],
115+
'date_to' => ['type' => self::TYPE_DATE, 'validate' => 'isDateOrNull', 'required' => false, 'allow_null' => true],
115116
'description' => ['type' => self::TYPE_STRING, 'validate' => 'isCleanHtml', 'size' => DiscountSettings::MAX_DESCRIPTION_LENGTH],
116117
'quantity' => ['type' => self::TYPE_INT, 'allow_null' => true, 'validate' => 'isUnsignedInt'],
117118
'total_quantity' => ['type' => self::TYPE_INT, 'allow_null' => true, 'validate' => 'isUnsignedInt'],
@@ -377,8 +378,9 @@ public static function haveCartRuleToday($idCustomer)
377378
'") OR (date_from >= "' . $start_date .
378379
'" AND date_from <= "' . $end_date .
379380
'") OR (date_from < "' . $start_date .
380-
'" AND date_to > "' . $end_date .
381-
'")) AND `id_customer` IN (0,' . (int) $idCustomer . ')';
381+
'" AND (date_to > "' . $end_date .
382+
'" OR date_to IS NULL)' .
383+
')) AND `id_customer` IN (0,' . (int) $idCustomer . ')';
382384

383385
$haveCartRuleToday[$idCustomer] = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
384386
}
@@ -432,7 +434,7 @@ public static function getCustomerCartRules(
432434
$sql .= ')';
433435

434436
// Then, conditions for date, voucher active property and total amount of vouchers in stock
435-
$sql .= ' AND NOW() BETWEEN cr.date_from AND cr.date_to
437+
$sql .= ' AND ((cr.date_from <= NOW() AND cr.date_to IS NULL) OR (NOW() BETWEEN cr.date_from AND cr.date_to))
436438
' . ($active ? 'AND cr.`active` = 1' : '') . '
437439
' . ($inStock ? 'AND (cr.`quantity` > 0 OR cr.`quantity` is null)' : '');
438440

@@ -789,7 +791,7 @@ public function checkValidity(Context $context, $alreadyInCart = false, $display
789791
if (strtotime($this->date_from) > time()) {
790792
return (!$display_error) ? false : $this->trans('This voucher is not valid yet', [], 'Shop.Notifications.Error');
791793
}
792-
if (strtotime($this->date_to) < time()) {
794+
if (!DateTimeUtil::isNull($this->date_to ?? null) && strtotime($this->date_to) < time()) {
793795
return (!$display_error) ? false : $this->trans('This voucher has expired', [], 'Shop.Notifications.Error');
794796
}
795797

@@ -1995,7 +1997,7 @@ public static function autoAddToCart(?Context $context = null, bool $useOrderPri
19951997
WHERE cr.active = 1
19961998
AND cr.code = ""
19971999
AND (cr.quantity > 0 OR cr.quantity is null)
1998-
AND NOW() BETWEEN cr.date_from AND cr.date_to
2000+
AND ((cr.date_from <= NOW() AND cr.date_to IS NULL) OR (NOW() BETWEEN cr.date_from AND cr.date_to))
19992001
AND (
20002002
cr.id_customer = 0
20012003
' . (Validate::isLoadedObject($context->customer) ? 'OR cr.id_customer = ' . (int) $context->cart->id_customer : '') . '

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"prestashop/hummingbird": "^2.0",
7272
"prestashop/pagesnotfound": "^3",
7373
"prestashop/productcomments": "^8.0",
74-
"prestashop/ps_apiresources": "0.5.0",
74+
"prestashop/ps_apiresources": "dev-change-validto",
7575
"prestashop/ps_banner": "^2",
7676
"prestashop/ps_bestsellers": "^1.0",
7777
"prestashop/ps_brandlist": "^1.0",

composer.lock

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

install-dev/data/db_structure.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ CREATE TABLE `PREFIX_cart_rule` (
167167
`id_cart_rule` int(10) unsigned NOT NULL auto_increment,
168168
`id_customer` int unsigned NOT NULL DEFAULT '0',
169169
`date_from` datetime NOT NULL,
170-
`date_to` datetime NOT NULL,
170+
`date_to` datetime DEFAULT NULL,
171171
`description` MEDIUMTEXT,
172172
`quantity` int(10) unsigned DEFAULT '0',
173173
`quantity_per_user` int(10) unsigned DEFAULT '0',

src/Adapter/Discount/QueryHandler/GetDiscountForEditingHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PrestaShop\PrestaShop\Core\Domain\Discount\Query\GetDiscountForEditing;
1717
use PrestaShop\PrestaShop\Core\Domain\Discount\QueryHandler\GetDiscountForEditingHandlerInterface;
1818
use PrestaShop\PrestaShop\Core\Domain\Discount\QueryResult\DiscountForEditing;
19+
use PrestaShop\PrestaShop\Core\Util\DateTime\DateTime as DateTimeUtil;
1920

2021
#[AsQueryHandler]
2122
class GetDiscountForEditingHandler implements GetDiscountForEditingHandlerInterface
@@ -45,7 +46,7 @@ public function handle(GetDiscountForEditing $query): DiscountForEditing
4546
$cartRule->priority,
4647
$cartRule->active,
4748
new DateTimeImmutable($cartRule->date_from),
48-
new DateTimeImmutable($cartRule->date_to),
49+
DateTimeUtil::buildDateTimeOrNull($cartRule->date_to),
4950
$cartRule->total_quantity,
5051
$cartRule->quantity,
5152
$quantityUsedInOrders,

src/Adapter/Discount/Update/DiscountBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function build(AddDiscountCommand $command): CartRule
2525
{
2626
$cartRule = new CartRule();
2727
$validFrom = $command->getValidFrom() ?: new DateTimeImmutable();
28-
$validTo = $command->getValidTo() ?: $validFrom->modify('+1 month');
28+
$validTo = $command->getValidTo();
2929

3030
$cartRule->name = $command->getLocalizedNames();
3131
$cartRule->description = $command->getDescription();
@@ -36,7 +36,7 @@ public function build(AddDiscountCommand $command): CartRule
3636
$cartRule->active = $command->isActive();
3737
$cartRule->id_customer = $command->getCustomerId()?->getValue();
3838
$cartRule->date_from = $validFrom->format(DateTimeUtil::DEFAULT_DATETIME_FORMAT);
39-
$cartRule->date_to = $validTo->format(DateTimeUtil::DEFAULT_DATETIME_FORMAT);
39+
$cartRule->date_to = $validTo !== null ? $validTo->format(DateTimeUtil::DEFAULT_DATETIME_FORMAT) : null;
4040
// We are initializing the discount so the remaining quantity and the total quantity are the same thing
4141
$cartRule->total_quantity = $command->getTotalQuantity();
4242
$cartRule->quantity = $command->getTotalQuantity();

src/Adapter/Discount/Update/Filler/DiscountFiller.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public function fillUpdatableProperties(CartRule $cartRule, UpdateDiscountComman
3232
$updatableProperties[] = 'date_from';
3333
}
3434
if ($command->isDirty('validTo')) {
35-
$cartRule->date_to = $command->getValidTo()->format(DateTimeUtil::DEFAULT_DATETIME_FORMAT);
35+
$validTo = $command->getValidTo();
36+
$cartRule->date_to = $validTo !== null ? $validTo->format(DateTimeUtil::DEFAULT_DATETIME_FORMAT) : null;
3637
$updatableProperties[] = 'date_to';
3738
}
3839
if ($command->isDirty('localizedNames')) {

0 commit comments

Comments
 (0)