From 63fc3ea22237d356e072e92933b2151a380c47f8 Mon Sep 17 00:00:00 2001 From: "vishalsingh.qlo325" Date: Sat, 15 Feb 2025 18:30:49 +0530 Subject: [PATCH 1/9] Added suggestion for the guest to select guest detail while using booking for someone else feature --- classes/CartCustomerGuestDetail.php | 42 +++++- controllers/front/OrderOpcController.php | 34 +++++ install/data/db_structure.sql | 6 + .../hotel-reservation-theme/css/order-opc.css | 25 ++++ .../hotel-reservation-theme/js/order-opc.js | 128 +++++++++++++++++- 5 files changed, 232 insertions(+), 3 deletions(-) diff --git a/classes/CartCustomerGuestDetail.php b/classes/CartCustomerGuestDetail.php index 2d4ae2fa6..f26e4c795 100644 --- a/classes/CartCustomerGuestDetail.php +++ b/classes/CartCustomerGuestDetail.php @@ -54,6 +54,46 @@ class CartCustomerGuestDetailCore extends ObjectModel ); } + public function add($auto_date = true, $null_values = false) + { + if ($res = parent::add($auto_date, $null_values)) { + $objCart = new Cart($this->id_cart); + Db::getInstance()->insert('customer_guest_detail_mapping', array( + 'id_customer' => (int) $objCart->id_customer, + 'id_customer_guest_detail' => (int)$this->id + )); + } + + return $res; + } + + public function delete() + { + if ($res = parent::delete()) { + $objCart = new Cart($this->id_cart); + Db::getInstance()->delete('customer_guest_detail_mapping', + '`id_customer` = '.(int) $objCart->id_customer.' + AND `id_customer_guest_detail`='.(int) $this->id + ); + } + + return $res; + } + + public function getCustomerRelatedGuestDetails($idCustomer, $firstname = false, $lastname = false, $email = false) + { + return Db::getInstance()->executeS(' + SELECT cgd.`id_customer_guest_detail`, cgd.`email`, cgd.`firstname`, cgd.`lastname`, cgd.`phone`, cgd.`id_cart` + FROM `'._DB_PREFIX_.'customer_guest_detail_mapping` cgdm + LEFT JOIN `'._DB_PREFIX_.'cart_customer_guest_detail` cgd + ON (cgdm.`id_customer_guest_detail` = cgd.`id_customer_guest_detail`) + WHERE cgdm.`id_customer` = '.(int)$idCustomer. + (($firstname !== false && $firstname != '') ? ' AND cgd.`firstname` LIKE "%'.$firstname.'%"' : ' '). + (($lastname !== false && $lastname != '') ? ' AND cgd.`lastname` LIKE "%'.$lastname.'%"' : ' '). + (($email !== false && $email != '') ? ' AND cgd.`email` LIKE "%'.$email.'%"' : ' ') + ); + } + public static function getCustomerGuestDetail($id_customer_guest_detail) { return Db::getInstance()->getRow(' @@ -121,4 +161,4 @@ class CartCustomerGuestDetailCore extends ObjectModel return $isValid; } -} \ No newline at end of file +} diff --git a/controllers/front/OrderOpcController.php b/controllers/front/OrderOpcController.php index 61b602ec3..59eaf1dff 100644 --- a/controllers/front/OrderOpcController.php +++ b/controllers/front/OrderOpcController.php @@ -391,6 +391,10 @@ class OrderOpcControllerCore extends ParentOrderController $this->ajaxDie($this->submitCustomerGuestDetail()); exit; break; + case 'getCustomerGuestDetail': + $this->ajaxDie($this->getCustomerGuestDetail()); + exit; + break; default: throw new PrestaShopException('Unknown method "'.Tools::getValue('method').'"'); } @@ -1069,6 +1073,36 @@ class OrderOpcControllerCore extends ParentOrderController die('0'); } + public function getCustomerGuestDetail() + { + $response = array('status' => false); + $firstname = trim(Tools::getValue('firstname')); + $lastname = trim(Tools::getValue('lastname')); + $email = trim(Tools::getValue('email')); + $objCartCustomerGuestDetail = new CartCustomerGuestDetailCore(); + if ($guestDetails = $objCartCustomerGuestDetail->getCustomerRelatedGuestDetails( + $this->context->cart->id_customer, + $firstname, + $lastname, + $email, + )) { + $response['status'] = true; + $detailsExist = array(); + foreach ($guestDetails as $guestKey => $guestDetail) { + $joinKey = trim($guestDetail['firstname']).trim($guestDetail['lastname']).trim($guestDetail['email']); + if ($guestDetail['id_cart'] == $this->context->cart->id || isset($detailsExist[$joinKey])) { + unset($guestDetails[$guestKey]); + } + $detailsExist[$joinKey] = true; + } + + $response['guestDetails'] = $guestDetails; + } + + return json_encode($response); + } + + public function submitCustomerGuestDetail() { $customerGuestDetail = Tools::getValue('customer_guest_detail'); diff --git a/install/data/db_structure.sql b/install/data/db_structure.sql index de57c4024..b21a85bfd 100644 --- a/install/data/db_structure.sql +++ b/install/data/db_structure.sql @@ -699,6 +699,12 @@ CREATE TABLE `PREFIX_cart_customer_guest_detail` ( PRIMARY KEY (`id_customer_guest_detail`) ) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8 COLLATION; +CREATE TABLE `PREFIX_customer_guest_detail_mapping` ( + `id_customer_guest_detail` int(10) unsigned NOT NULL, + `id_customer` int(10) unsigned NOT NULL, + PRIMARY KEY (`id_customer_guest_detail`, `id_customer`) +) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8 COLLATION; + CREATE TABLE `PREFIX_customization` ( `id_customization` int(10) unsigned NOT NULL auto_increment, `id_product_attribute` int(10) unsigned NOT NULL DEFAULT '0', diff --git a/themes/hotel-reservation-theme/css/order-opc.css b/themes/hotel-reservation-theme/css/order-opc.css index b3f1c7b6e..d32358148 100644 --- a/themes/hotel-reservation-theme/css/order-opc.css +++ b/themes/hotel-reservation-theme/css/order-opc.css @@ -682,3 +682,28 @@ line-height: 19px; color: #333; margin-bottom: 15px; } + +/* Customer guest suggetions Css */ +.customer_guest_detail_ul { + overflow-x: hidden; + overflow-y: auto; + max-height: 200px; + background: #fff; + margin: 0; + margin-top: 2px; + font-size: 14px; + z-index: 10; + box-shadow: 0 4px 5px rgba(0, 0, 0, 0.15); + position: absolute; + width: 90%;} + +.customer_guest_detail_li { + white-space: nowrap; + padding: 15px;} +.customer_guest_detail_li.active { + color: #fff; + background-color: #379bf0;} +.customer_guest_detail_li:hover { + cursor: pointer; + background-color: #379bf0; + color: #fff;} diff --git a/themes/hotel-reservation-theme/js/order-opc.js b/themes/hotel-reservation-theme/js/order-opc.js index c828b6051..c8af2ab06 100644 --- a/themes/hotel-reservation-theme/js/order-opc.js +++ b/themes/hotel-reservation-theme/js/order-opc.js @@ -116,15 +116,139 @@ $(document).ready(function() validateCustomerGuestDetailForm(); } - $('#customer_guest_detail_form').on('change', function(e) { + function submitCustomerGuestDetailForm(postData) { $.ajax({ type: 'POST', url: orderOpcUrl, async: false, cache: false, dataType : "json", - data: $(this).serialize()+'&method=submitCustomerGuestDetail&ajax=true&token=' + static_token + data: postData }); + } + + $('#customer_guest_detail_form').on('change', function(e) { + let postData = $(this).serialize()+'&method=submitCustomerGuestDetail&ajax=true&token=' + static_token; + submitCustomerGuestDetailForm(postData); + }); + function setCustomerGuestDetailForm(guestDetail) { + $('#customer_guest_detail_firstname').val(guestDetail.firstname); + $('#customer_guest_detail_lastname').val(guestDetail.lastname); + $('#customer_guest_detail_email').val(guestDetail.email); + $('#customer_guest_detail_phone').val(guestDetail.phone); + validateCustomerGuestDetailForm(); + let postData = $('#customer_guest_detail_form').serialize()+'&method=submitCustomerGuestDetail&ajax=true&token=' + static_token; + submitCustomerGuestDetailForm(postData); + $('.customer_guest_detail_ul').remove(); + } + + $(document).on('keyup', function(e) { + if (!$(e.target).attr('id') == 'customer_guest_detail_firstname' + || !$(e.target).attr('id') == 'customer_guest_detail_lastname' + || !$(e.target).attr('id') == 'customer_guest_detail_email' + ) { + $('.customer_guest_detail_ul').remove(); + } + }); + + $(document).on('click', function(e) { + if ($(e.target).hasClass('customer_guest_detail_li')) { + let guestDetail = $(e.target).data('guest_detail'); + setCustomerGuestDetailForm(guestDetail); + } + + $('.customer_guest_detail_ul').remove(); + }); + let debounceTimeout; + $(document).on('keyup', '#customer_guest_detail_firstname, #customer_guest_detail_lastname, #customer_guest_detail_email', function(e) { + var keyCode = e.keyCode || e.which; + if (keyCode == 40 || keyCode == 38) { + let activeElem = $(".customer_guest_detail_ul .active"); + let newActiveElem; + if (activeElem.length) { + if (keyCode == 40) { + newActiveElem = activeElem.next("li"); + if (!newActiveElem.length) { + newActiveElem = $(".customer_guest_detail_ul li:first"); // Loop back to first + } + } else if (keyCode == 38) { + newActiveElem = activeElem.prev("li"); + if (!newActiveElem.length) { + newActiveElem = $(".customer_guest_detail_ul li:last"); // Loop back to last + } + } + } else { + newActiveElem = keyCode == 40 ? $(".customer_guest_detail_ul li:first") : $(".customer_guest_detail_ul li:last"); + } + + activeElem.removeClass("active"); + newActiveElem.addClass("active"); + + return; + } else if (keyCode == 13) { + if ($('.customer_guest_detail_ul .active').length != 0) { + let guestDetail = $('.customer_guest_detail_ul .active').data('guest_detail'); + setCustomerGuestDetailForm(guestDetail); + } + } else if (!((keyCode < 65 || keyCode > 122) && (keyCode < 48 || keyCode > 57) && (keyCode != 8))) { + $('.customer_guest_detail_ul').remove(); + clearTimeout(debounceTimeout); // Clear the existing timeout + debounceTimeout = setTimeout(() => { + let firstName = $('#customer_guest_detail_firstname').val(); + let lastName = $('#customer_guest_detail_lastname').val(); + let email = $('#customer_guest_detail_email').val(); + let targetElem = $(this); + customerGuestDetailAjax = $.ajax({ + type: 'POST', + url: orderOpcUrl, + async: false, + cache: false, + dataType : "json", + data: { + token : static_token, + ajax : true, + method : 'getCustomerGuestDetail', + firstname : firstName, + lastname : lastName, + email : email, + }, + success: function(data) { + if (data.status) { + let listElem = $('