Added : Hotel selection in the cart rule

Co-authored-by: Name <sumit201@webkul.in>
This commit is contained in:
aradhana kumari
2026-07-03 17:33:35 +05:30
parent a000f7e711
commit cdef809530
4 changed files with 109 additions and 4 deletions

View File

@@ -220,6 +220,40 @@
</div>
{/if}
{if $hotels.unselected|@count + $hotels.selected|@count > 1}
<p class="checkbox">
<label>
<input type="checkbox" id="hotel_restriction" name="hotel_restriction" value="1" {if $hotels.unselected|@count}checked="checked"{/if} />
{l s='Hotel selection'}
</label>
</p>
<div id="hotel_restriction_div">
<br />
<table class="table">
<tr>
<td>
<p>{l s='Unselected hotels'}</p>
<select id="hotel_select_1" class="input-large" multiple>
{foreach from=$hotels.unselected item='hotel'}
<option value="{$hotel.id_hotel|intval}">&nbsp;{$hotel.hotel_name|escape}</option>
{/foreach}
</select>
<a id="hotel_select_add" class="btn btn-default btn-block clearfix" >{l s='Add'} <i class="icon-arrow-right"></i></a>
</td>
<td>
<p>{l s='Selected hotels'}</p>
<select name="hotel_select[]" class="input-large" id="hotel_select_2" multiple>
{foreach from=$hotels.selected item='hotel'}
<option value="{$hotel.id_hotel|intval}">&nbsp;{$hotel.hotel_name|escape}</option>
{/foreach}
</select>
<a id="hotel_select_remove" class="btn btn-default btn-block clearfix" ><i class="icon-arrow-left"></i> {l s='Remove'}</a>
</td>
</tr>
</table>
</div>
{/if}
{if $cart_rules.unselected|@count + $cart_rules.selected|@count > 0}
<p class="checkbox">
<label>

View File

@@ -147,7 +147,7 @@ function updateProductRuleShortDescription(item)
$('#' + id2 + '_match').val(length);
}
var restrictions = new Array('country', 'carrier', 'group', 'cart_rule', 'shop');
var restrictions = new Array('country', 'carrier', 'group', 'hotel', 'cart_rule', 'shop');
for (i in restrictions)
{
toggleCartRuleFilter($('#' + restrictions[i] + '_restriction'));

View File

@@ -59,6 +59,7 @@ class CartRuleCore extends ObjectModel
public $cart_rule_restriction;
public $product_restriction;
public $shop_restriction;
public $hotel_restriction;
public $free_shipping;
public $reduction_percent;
public $reduction_amount;
@@ -102,6 +103,7 @@ class CartRuleCore extends ObjectModel
'cart_rule_restriction' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'product_restriction' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'shop_restriction' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'hotel_restriction' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'free_shipping' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'reduction_percent' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPercentage'),
'reduction_amount' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat'),
@@ -163,6 +165,7 @@ class CartRuleCore extends ObjectModel
$r &= Db::getInstance()->delete('cart_rule_carrier', '`id_cart_rule` = '.(int)$this->id);
$r &= Db::getInstance()->delete('cart_rule_shop', '`id_cart_rule` = '.(int)$this->id);
$r &= Db::getInstance()->delete('cart_rule_group', '`id_cart_rule` = '.(int)$this->id);
$r &= Db::getInstance()->delete('cart_rule_hotel', '`id_cart_rule` = '.(int)$this->id);
$r &= Db::getInstance()->delete('cart_rule_country', '`id_cart_rule` = '.(int)$this->id);
$r &= Db::getInstance()->delete('cart_rule_combination', '`id_cart_rule_1` = '.(int)$this->id.' OR `id_cart_rule_2` = '.(int)$this->id);
$r &= Db::getInstance()->delete('cart_rule_product_rule_group', '`id_cart_rule` = '.(int)$this->id);
@@ -637,6 +640,21 @@ class CartRuleCore extends ObjectModel
}
}
// Check if the cart contains a product from a restricted hotel
if ($this->hotel_restriction) {
$id_cart_rule = (int)Db::getInstance()->getValue('
SELECT crh.id_cart_rule
FROM '._DB_PREFIX_.'cart_rule_hotel crh
INNER JOIN '._DB_PREFIX_.'htl_room_type hrt ON hrt.id_hotel = crh.id_hotel
INNER JOIN '._DB_PREFIX_.'cart_product cp ON cp.id_product = hrt.id_product
WHERE crh.id_cart_rule = '.(int)$this->id.'
AND cp.id_cart = '.(int)$context->cart->id
);
if (!$id_cart_rule) {
return (!$display_error) ? false : Tools::displayError('You cannot use this voucher with the selected hotels');
}
}
// Check if the products chosen by the customer are usable with the cart rule
if ($this->product_restriction) {
$r = $this->checkProductRestrictions($context, false, $display_error, $alreadyInCart);
@@ -1370,6 +1388,49 @@ class CartRuleCore extends ObjectModel
return $array;
}
public function getHotelRestrictions($id_profile = null)
{
$array = array('selected' => array(), 'unselected' => array());
$id_lang = (int)Context::getContext()->language->id;
$hotel_access_condition = '';
if ($id_profile) {
$accessible_hotels = HotelBranchInformation::getProfileAccessedHotels($id_profile, 1, 1);
if (!empty($accessible_hotels)) {
$hotel_access_condition = ' AND hbi.`id` IN ('.implode(',', array_map('intval', $accessible_hotels)).')';
}
}
if (!Validate::isLoadedObject($this) || $this->hotel_restriction == 0) {
$array['selected'] = Db::getInstance()->executeS('
SELECT hbi.`id` AS id_hotel, hbl.`hotel_name`, 1 AS selected
FROM `'._DB_PREFIX_.'htl_branch_info` hbi
LEFT JOIN `'._DB_PREFIX_.'htl_branch_info_lang` hbl
ON (hbl.`id` = hbi.`id` AND hbl.`id_lang` = '.$id_lang.')
WHERE hbi.`active` = 1'.$hotel_access_condition.'
ORDER BY hbl.`hotel_name` ASC
');
} else {
$resource = Db::getInstance()->query('
SELECT hbi.`id` AS id_hotel, hbl.`hotel_name`,
IF(crh.id_hotel IS NULL, 0, 1) AS selected
FROM `'._DB_PREFIX_.'htl_branch_info` hbi
LEFT JOIN `'._DB_PREFIX_.'htl_branch_info_lang` hbl
ON (hbl.`id` = hbi.`id` AND hbl.`id_lang` = '.$id_lang.')
LEFT JOIN (
SELECT id_hotel FROM `'._DB_PREFIX_.'cart_rule_hotel`
WHERE id_cart_rule = '.(int)$this->id.'
) crh ON hbi.`id` = crh.id_hotel
WHERE hbi.`active` = 1'.$hotel_access_condition.'
ORDER BY hbl.`hotel_name` ASC
');
while ($row = Db::getInstance()->nextRow($resource)) {
$array[$row['selected'] ? 'selected' : 'unselected'][] = $row;
}
}
return $array;
}
public static function autoRemoveFromCart($context = null)
{
if (!$context) {

View File

@@ -324,7 +324,7 @@ class AdminCartRulesControllerCore extends AdminController
}
// These are checkboxes (which aren't sent through POST when they are not check), so they are forced to 0
foreach (array('country', 'carrier', 'group', 'cart_rule', 'product', 'shop') as $type) {
foreach (array('country', 'carrier', 'group', 'cart_rule', 'product', 'shop', 'hotel') as $type) {
if (!Tools::getValue($type.'_restriction')) {
$_POST[$type.'_restriction'] = 0;
}
@@ -380,7 +380,7 @@ class AdminCartRulesControllerCore extends AdminController
{
// All the associations are deleted for an update, then recreated when we call the "afterAdd" method
$id_cart_rule = Tools::getValue('id_cart_rule');
foreach (array('country', 'carrier', 'group', 'product_rule_group', 'shop') as $type) {
foreach (array('country', 'carrier', 'group', 'hotel', 'product_rule_group', 'shop') as $type) {
Db::getInstance()->delete('cart_rule_'.$type, '`id_cart_rule` = '.(int)$id_cart_rule);
}
Db::getInstance()->delete('cart_rule_product_rule', 'NOT EXISTS (SELECT 1 FROM `'._DB_PREFIX_.'cart_rule_product_rule_group`
@@ -414,7 +414,7 @@ class AdminCartRulesControllerCore extends AdminController
*/
protected function afterAdd($currentObject)
{
// Add restrictions for generic entities like country, carrier and group
// Add restrictions for generic entities like country, carrier, group, hotel and shop
foreach (array('country', 'carrier', 'group', 'shop') as $type) {
if (Tools::getValue($type.'_restriction') && is_array($array = Tools::getValue($type.'_select')) && count($array)) {
$values = array();
@@ -424,6 +424,14 @@ class AdminCartRulesControllerCore extends AdminController
Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'cart_rule_'.$type.'` (`id_cart_rule`, `id_'.$type.'`) VALUES '.implode(',', $values));
}
}
// Add hotel restrictions
if (Tools::getValue('hotel_restriction') && is_array($array = Tools::getValue('hotel_select')) && count($array)) {
$values = array();
foreach ($array as $id) {
$values[] = '('.(int)$currentObject->id.','.(int)$id.')';
}
Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'cart_rule_hotel` (`id_cart_rule`, `id_hotel`) VALUES '.implode(',', $values));
}
// Add cart rule restrictions
if (Tools::getValue('cart_rule_restriction') && is_array($array = Tools::getValue('cart_rule_select')) && count($array)) {
$values = array();
@@ -803,6 +811,7 @@ class AdminCartRulesControllerCore extends AdminController
$languages = Language::getLanguages();
$countries = $current_object->getAssociatedRestrictions('country', true, true);
$groups = $current_object->getAssociatedRestrictions('group', false, true);
$hotels = $current_object->getHotelRestrictions($this->context->employee->id_profile);
$shops = $current_object->getAssociatedRestrictions('shop', false, false);
$cart_rules = $current_object->getAssociatedRestrictions('cart_rule', false, true, 0, $limit);
$carriers = $current_object->getAssociatedRestrictions('carrier', true, false);
@@ -862,6 +871,7 @@ class AdminCartRulesControllerCore extends AdminController
'countries' => $countries,
'carriers' => $carriers,
'groups' => $groups,
'hotels' => $hotels,
'shops' => $shops,
'cart_rules' => $cart_rules,
'product_rule_groups' => $product_rule_groups,