Skip to content

Commit

Permalink
Merge pull request PrestaShop#35464 from Hlavtox/do-not-show-vouchers
Browse files Browse the repository at this point in the history
Do not show vouchers that should be hidden
  • Loading branch information
mflasquin authored Feb 27, 2024
2 parents d75f07d + 2d1ab43 commit bc4ab51
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
</div>
</div>

<div class="form-group" id="cart-rules-highlight"{if !$currentTab->getFieldValue($currentObject, 'code')} style="display: none;"{/if}>
<div class="form-group">
<label class="control-label col-lg-3">
<span class="label-tooltip" data-toggle="tooltip"
title="{l|escape s='If the voucher is not yet in the cart, it will be displayed in the cart summary.' d='Admin.Catalog.Help'}">
Expand Down Expand Up @@ -150,9 +150,4 @@
</div>
<script type="text/javascript">
$(".textarea-autosize").autosize();
$(document).ready(function() {
$('#code').bind('keyup change', function() {
$('#cart-rules-highlight').toggle($(this).val() !== "");
});
});
</script>
33 changes: 19 additions & 14 deletions classes/CartRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ public static function haveCartRuleToday($idCustomer)
* @param int $id_lang Language ID
* @param int $id_customer Customer ID
* @param bool $active Active vouchers only
* @param bool $includeGeneric Include generic AND highlighted vouchers, regardless of highlight_only setting
* @param bool $inStock Vouchers in stock only
* @param bool $includeGeneric Include generic vouchers that don't have specific customer
* @param bool $inStock Vouchers that have "total quantity" remaining
* @param CartCore|null $cart Cart
* @param bool $free_shipping_only Free shipping only
* @param bool $highlight_only Highlighted vouchers only
Expand All @@ -390,31 +390,36 @@ public static function getCustomerCartRules(
return [];
}

$sql_part1 = '* FROM `' . _DB_PREFIX_ . 'cart_rule` cr
LEFT JOIN `' . _DB_PREFIX_ . 'cart_rule_lang` crl ON (cr.`id_cart_rule` = crl.`id_cart_rule` AND crl.`id_lang` = ' . (int) $id_lang . ')';
// Basic part of the query, we are selecting all cart rules
$sql = '
SELECT SQL_NO_CACHE * FROM `' . _DB_PREFIX_ . 'cart_rule` cr
LEFT JOIN `' . _DB_PREFIX_ . 'cart_rule_lang` crl
ON (cr.`id_cart_rule` = crl.`id_cart_rule` AND crl.`id_lang` = ' . (int) $id_lang . ')';

$sql_where = ' WHERE ((cr.`id_customer` = ' . (int) $id_customer . ' OR (cr.`id_customer` = 0 AND (cr.`highlight` = 1 OR cr.`code` = "")))';
// We will definitely include vouchers for this specific customer
$sql .= ' WHERE (cr.`id_customer` = ' . (int) $id_customer;

// And if required, all the generic ones, that don't have any specific customer set
if ($includeGeneric && (int) $id_customer !== 0) {
$sql_where .= ' OR cr.`id_customer` = 0)';
} else {
$sql_where .= ')';
$sql .= ' OR cr.`id_customer` = 0';
}
$sql .= ')';

$sql_part2 = ' AND NOW() BETWEEN cr.date_from AND cr.date_to
// Then, conditions for date, voucher active property and total amount of vouchers in stock
$sql .= ' AND NOW() BETWEEN cr.date_from AND cr.date_to
' . ($active ? 'AND cr.`active` = 1' : '') . '
' . ($inStock ? 'AND cr.`quantity` > 0' : '');

// If we want to select only vouchers that have free shipping as the action
if ($free_shipping_only) {
$sql_part2 .= ' AND free_shipping = 1 AND carrier_restriction = 1';
$sql .= ' AND free_shipping = 1 AND carrier_restriction = 1';
}

// If we want to select only vouchers with "Highlight" option activated
if ($highlight_only) {
$sql_part2 .= ' AND highlight = 1 AND code NOT LIKE "' . pSQL(CartRule::BO_ORDER_CODE_PREFIX) . '%"';
$sql .= ' AND highlight = 1 AND code NOT LIKE "' . pSQL(CartRule::BO_ORDER_CODE_PREFIX) . '%"';
}

$sql = 'SELECT SQL_NO_CACHE ' . $sql_part1 . $sql_where . $sql_part2;

$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql, true, false);

if (empty($result)) {
Expand Down Expand Up @@ -526,7 +531,7 @@ public static function getAllCustomerCartRules(
$query = new DbQuery();
$query->select('cr.*, crl.name');
$query->from('cart_rule', 'cr');
$query->where('cr.id_customer = ' . $customerId . ' OR (cr.`id_customer` = 0 AND (cr.`highlight` = 1 OR cr.`code` = ""))');
$query->where('cr.id_customer = ' . $customerId);
$query->leftJoin('cart_rule_lang', 'crl', 'cr.id_cart_rule = crl.id_cart_rule AND crl.id_lang = ' . (int) Configuration::get('PS_LANG_DEFAULT'));
$query->orderBy('cr.active DESC, cr.id_customer DESC');

Expand Down
2 changes: 0 additions & 2 deletions js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,6 @@ function gencode(size)
for (var i = 1; i <= size; ++i) {
getE('code').value += chars.charAt(Math.floor(randomNumbers[i]/2**32 * chars.length));
}

getE('cart-rules-highlight').style.display = '';
}

var tpl_viewing_window = null;
Expand Down
169 changes: 166 additions & 3 deletions tests/Integration/Classes/CartRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,169 @@ public function testGetAllCartRulesWithGlobalCartRulesAvailable(): void
$this->assertEquals(2, count($customerCartRules));
}

/*
* Tests if the customer sees only his specific vouchers in my account zone
* in front office.
*/
public function testGetAllCustomersCartRulesInMyAccountZone(): void
{
// Reset table
self::setUpBeforeClass();

// Code, highlight, for everyone
$CodeHighlightEveryone = $this->createDummyCartRule(true, 0, true, true);
// Code, highlight, specific customer
$CodeHighlightSpecific = $this->createDummyCartRule(true, (int) $this->dummyCustomer->id, true, true);
// Code, no highlight, for everyone
$CodeNohighlightEveryone = $this->createDummyCartRule(true, 0, true);
// Code, no highlight, specific customer
$CodeNohighlightSpecific = $this->createDummyCartRule(true, (int) $this->dummyCustomer->id, true);
// No code, highlight, for everyone
$NocodeHighlightEveryone = $this->createDummyCartRule(true, 0, false, true);
// No code, highlight, specific customer
$NocodeHighlightSpecific = $this->createDummyCartRule(true, (int) $this->dummyCustomer->id, false, true);
// No code, no highlight, for everyone
$NocodeNohighlightEveryone = $this->createDummyCartRule(true, 0, false);
// No code, no highlight, specific customer
$NocodeNohighlightSpecific = $this->createDummyCartRule(true, (int) $this->dummyCustomer->id, false);
// Code, highlight, specific customer, disabled
$CodeHighlightSpecificDisabled = $this->createDummyCartRule(false, (int) $this->dummyCustomer->id, true, true);
// Code, highlight, specific customer, disabled
$CodeHighlightSpecificDisabled = $this->createDummyCartRule(false, (int) $this->dummyCustomer->id, true, true);

// Get customer's vouchers in frontoffice
$customerCartRules = CartRule::getCustomerCartRules(
$this->defaultLanguageId,
(int) $this->dummyCustomer->id,
true,
false
);

$this->assertEquals(
[
$CodeNohighlightSpecific->id,
$NocodeNohighlightSpecific->id,
$CodeHighlightSpecific->id,
$NocodeHighlightSpecific->id,
],
array_column($customerCartRules, 'id_cart_rule')
);
}

/*
* Tests customer's voucher in backoffice. We should see even the disabled vouchers here.
*/
public function testGetAllCustomersCartRulesInBackoffice(): void
{
// Reset table
self::setUpBeforeClass();

// Code, highlight, for everyone
$CodeHighlightEveryone = $this->createDummyCartRule(true, 0, true, true);
// Code, highlight, specific customer
$CodeHighlightSpecific = $this->createDummyCartRule(true, (int) $this->dummyCustomer->id, true, true);
// Code, no highlight, for everyone
$CodeNohighlightEveryone = $this->createDummyCartRule(true, 0, true);
// Code, no highlight, specific customer
$CodeNohighlightSpecific = $this->createDummyCartRule(true, (int) $this->dummyCustomer->id, true);
// No code, highlight, for everyone
$NocodeHighlightEveryone = $this->createDummyCartRule(true, 0, false, true);
// No code, highlight, specific customer
$NocodeHighlightSpecific = $this->createDummyCartRule(true, (int) $this->dummyCustomer->id, false, true);
// No code, no highlight, for everyone
$NocodeNohighlightEveryone = $this->createDummyCartRule(true, 0, false);
// No code, no highlight, specific customer
$NocodeNohighlightSpecific = $this->createDummyCartRule(true, (int) $this->dummyCustomer->id, false);
// Code, highlight, specific customer, disabled
$CodeHighlightSpecificDisabled = $this->createDummyCartRule(false, (int) $this->dummyCustomer->id, true, true);

$customerCartRules = CartRule::getAllCustomerCartRules(
(int) $this->dummyCustomer->id
);

$this->assertEquals(
[
$NocodeNohighlightSpecific->id,
$NocodeHighlightSpecific->id,
$CodeNohighlightSpecific->id,
$CodeHighlightSpecific->id,
$CodeHighlightSpecificDisabled->id,
],
array_column($customerCartRules, 'id_cart_rule')
);
}

/*
* Tests if both logged in and logged out customer gets offered
* proper highlighted vouchers in cart.
*/
public function testGetHighlightedVouchersInCart(): void
{
// Reset table
self::setUpBeforeClass();

// Code, highlight, for everyone
$CodeHighlightEveryone = $this->createDummyCartRule(true, 0, true, true);
// Code, highlight, specific customer
$CodeHighlightSpecific = $this->createDummyCartRule(true, (int) $this->dummyCustomer->id, true, true);
// Code, no highlight, for everyone
$CodeNohighlightEveryone = $this->createDummyCartRule(true, 0, true);
// Code, no highlight, specific customer
$CodeNohighlightSpecific = $this->createDummyCartRule(true, (int) $this->dummyCustomer->id, true);
// No code, highlight, for everyone
$NocodeHighlightEveryone = $this->createDummyCartRule(true, 0, false, true);
// No code, highlight, specific customer
$NocodeHighlightSpecific = $this->createDummyCartRule(true, (int) $this->dummyCustomer->id, false, true);
// No code, no highlight, for everyone
$NocodeNohighlightEveryone = $this->createDummyCartRule(true, 0, false);
// No code, no highlight, specific customer
$NocodeNohighlightSpecific = $this->createDummyCartRule(true, (int) $this->dummyCustomer->id, false);
// Code, highlight, specific customer, disabled
$CodeHighlightSpecificDisabled = $this->createDummyCartRule(false, (int) $this->dummyCustomer->id, true, true);
// Code, highlight, specific customer, disabled
$CodeHighlightSpecificDisabled = $this->createDummyCartRule(false, (int) $this->dummyCustomer->id, true, true);

// Get logged in customer's vouchers, we simulate getCustomerHighlightedDiscounts with no cart
$customerCartRules = CartRule::getCustomerCartRules(
$this->defaultLanguageId,
(int) $this->dummyCustomer->id,
true,
true,
true,
null,
false,
true
);
$this->assertEquals(
[
$CodeHighlightEveryone->id,
$NocodeHighlightEveryone->id,
$CodeHighlightSpecific->id,
$NocodeHighlightSpecific->id,
],
array_column($customerCartRules, 'id_cart_rule')
);

// Get guest customer's vouchers, we simulate getCustomerHighlightedDiscounts with no cart
$customerCartRules = CartRule::getCustomerCartRules(
$this->defaultLanguageId,
0,
true,
true,
true,
null,
false,
true
);
$this->assertEquals(
[
$CodeHighlightEveryone->id,
$NocodeHighlightEveryone->id,
],
array_column($customerCartRules, 'id_cart_rule')
);
}

/**
* Test sorting of the CartRules
*
Expand Down Expand Up @@ -202,9 +365,7 @@ public function testSortingOfTheAvailableCustomerCartRules(): void
$this->assertEquals(
[
$activeCustomerRule->id,
$activeGlobalRule->id,
$inactiveCustomerRule->id,
$inactiveGlobalRule->id,
],
array_column($customerCartRules, 'id_cart_rule')
);
Expand All @@ -220,7 +381,8 @@ public function testSortingOfTheAvailableCustomerCartRules(): void
public function createDummyCartRule(
bool $active,
int $customerId,
bool $code = true
bool $code = true,
bool $highlight = false
): CartRule {
$randomNumber = rand(999, 9999);
$cart_rule = new CartRule();
Expand All @@ -237,6 +399,7 @@ public function createDummyCartRule(
$cart_rule->date_from = date('Y-m-d H:i:s', time());
$cart_rule->date_to = date('Y-m-d H:i:s', time() + 24 * 36000);
$cart_rule->active = $active;
$cart_rule->highlight = $highlight;
$cart_rule->add();

return $cart_rule;
Expand Down

0 comments on commit bc4ab51

Please sign in to comment.