diff --git a/bin/frontend/controls/CouponCodeInput.css b/bin/frontend/controls/CouponCodeInput.css index f88583bf327d4ea95860ec1f8d75685875c44635..cf95da74bce32e5ee81a6bcd8f46648d4cec707c 100644 --- a/bin/frontend/controls/CouponCodeInput.css +++ b/bin/frontend/controls/CouponCodeInput.css @@ -29,3 +29,26 @@ div + .quiqqer-coupons-field { .quiqqer-coupons-remove .fa { margin-right: 0; } + +/** + * Simple Checkout + */ + +.quiqqer-coupons-field-simpleCheckout { + margin-top: 20px; + margin-left: 0 !important; +} + +.quiqqer-coupons-field-simpleCheckout .quiqqer-coupons-couponcodeinput { + background: var(--_sectionBgColor); + display: flex; + padding: var(--_sectionPadding); +} + +.quiqqer-coupons-field-simpleCheckout .quiqqer-coupons-couponcodeinput label { + flex-grow: 1; +} + +.quiqqer-coupons-field-simpleCheckout .quiqqer-coupons-couponcodeinput-input { + width: calc(100% - 1rem); +} \ No newline at end of file diff --git a/bin/frontend/controls/CouponCodeInput.js b/bin/frontend/controls/CouponCodeInput.js index 7bd7b9b5bc358b327e17d32807a5e658cbdf1e11..57a778028b1b65f2c8b488ce4d52dcd2c9350f27 100644 --- a/bin/frontend/controls/CouponCodeInput.js +++ b/bin/frontend/controls/CouponCodeInput.js @@ -20,25 +20,25 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ 'text!package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput.html', 'css!package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput.css' -], function (QUI, QUIControl, QUILoader, Sessions, CouponCodes, QUIAjax, QUILocale, Mustache, template) { - "use strict"; +], function(QUI, QUIControl, QUILoader, Sessions, CouponCodes, QUIAjax, QUILocale, Mustache, template) { + 'use strict'; var lg = 'quiqqer/coupons'; return new Class({ Extends: QUIControl, - Type : 'package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', + Type: 'package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', Binds: [ '$submit' ], - initialize: function (options) { + initialize: function(options) { this.parent(options); - this.$Input = null; - this.Loader = new QUILoader(); + this.$Input = null; + this.Loader = new QUILoader(); this.$running = false; this.addEvents({ @@ -49,23 +49,27 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ /** * Event: onInject */ - $onInject: function () { - var self = this; + $onInject: function() { + var self = this; var lgPrefix = 'controls.frontend.CouponCodeInput.template.'; this.$Elm.addClass('quiqqer-coupons-field'); + if (this.$Elm.getParent('[data-name="quiqqer-simple-checkout"]')) { + this.$Elm.addClass('quiqqer-coupons-field-simpleCheckout'); + } + this.$Elm.set('html', Mustache.render(template, { - labelInput : QUILocale.get(lg, lgPrefix + 'labelInput'), + labelInput: QUILocale.get(lg, lgPrefix + 'labelInput'), labelInputPlaceholder: QUILocale.get(lg, lgPrefix + 'labelInputPlaceholder'), - submitBtnText : QUILocale.get(lg, lgPrefix + 'submitBtnText') + submitBtnText: QUILocale.get(lg, lgPrefix + 'submitBtnText') })); this.Loader.inject(this.$Elm); this.$Input = this.$Elm.getElement('input[name="code"]'); - this.$Input.addEvent('keyup', function (event) { + this.$Input.addEvent('keyup', function(event) { if (event.code === 13) { self.$submit(); } @@ -73,7 +77,7 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ this.$Elm.getElement( 'button.quiqqer-coupons-couponcodeinput-btn' - ).addEvent('click', function (event) { + ).addEvent('click', function(event) { event.stop(); self.$submit(); }); @@ -81,23 +85,23 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ if (this.isInOrder()) { var OrderProcess = this.getOrderProcess(); - new Promise(function (resolve) { + new Promise(function(resolve) { if (!OrderProcess.isLoaded()) { return OrderProcess.addEvent('onLoad', resolve); } resolve(); - }).then(function () { + }).then(function() { if (OrderProcess.getAttribute('basketEditable') === false) { // remove coupon codes button new Element('button', { 'class': 'quiqqer-coupons-remove', - 'html' : '<span class="fa fa-close"></span>', - title : QUILocale.get(lg, 'remove.coupons.button'), - events : { - click: function (e) { + 'html': '<span class="fa fa-close"></span>', + title: QUILocale.get(lg, 'remove.coupons.button'), + events: { + click: function(e) { e.stop(); - self.removeCouponsFromOrder().catch(function (e) { + self.removeCouponsFromOrder().catch(function(e) { console.error(e); }); } @@ -111,7 +115,7 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ /** * Submit a CouponCode */ - $submit: function () { + $submit: function() { if (this.$running) { return; } @@ -127,18 +131,51 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ this.$running = true; this.Loader.show(); + // simple order + const SimpleCheckoutNode = this.getElm().getParent('[data-name="quiqqer-simple-checkout"]'); + + if (SimpleCheckoutNode) { + const SimpleCheckout = QUI.Controls.getById(SimpleCheckoutNode.get('data-quiid')); + + QUIAjax.get('package_quiqqer_order-simple-checkout_ajax_frontend_getOrder', (response) => { + CouponCodes.addCouponCodeToBasket(code, response.order.uuid).then(() => { + if (SimpleCheckout) { + SimpleCheckout.$refreshBasket().then(() => { + this.$running = false; + this.Loader.hide(); + }); + + return; + } + + this.$running = false; + this.Loader.hide(); + }).catch((err) => { + console.error(err); + + this.$running = false; + this.Loader.hide(); + }); + }, { + 'package': 'quiqqer/order-simple-checkout', + orderHash: window.location.hash.substring(1) + }); + + return; + } + // get order process if (!this.isInOrder()) { - QUIAjax.get('package_quiqqer_order_ajax_frontend_basket_getLastOrder', function (order) { - CouponCodes.addCouponCodeToBasket(code, order.hash).then(function (redeemed) { + QUIAjax.get('package_quiqqer_order_ajax_frontend_basket_getLastOrder', function(order) { + CouponCodes.addCouponCodeToBasket(code, order.hash).then(function(redeemed) { if (!redeemed) { self.$running = false; self.Loader.hide(); return; } - self.$addCouponCodeToSession(code).then(function () { - QUIAjax.get('package_quiqqer_order_ajax_frontend_basket_getOrderProcessUrl', function (url) { + self.$addCouponCodeToSession(code).then(function() { + QUIAjax.get('package_quiqqer_order_ajax_frontend_basket_getOrderProcessUrl', function(url) { window.location = url + '?coupon=' + code; }, { 'package': 'quiqqer/order' @@ -156,9 +193,9 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ OrderProcess.Loader.show(); - OrderProcess.getOrder().then(function (orderHash) { + OrderProcess.getOrder().then(function(orderHash) { return CouponCodes.addCouponCodeToBasket(code, orderHash); - }).then(function (redeemed) { + }).then(function(redeemed) { self.$running = false; if (redeemed === false) { @@ -167,7 +204,7 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ return; } - self.$addCouponCodeToSession(code).then(function () { + self.$addCouponCodeToSession(code).then(function() { OrderProcess.reload(); }); }); @@ -178,15 +215,15 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ * @param code * @return {Promise} */ - $addCouponCodeToSession: function (code) { - return Sessions.get('quiqqer-coupons').then(function (coupons) { + $addCouponCodeToSession: function(code) { + return Sessions.get('quiqqer-coupons').then(function(coupons) { if (!coupons || typeOf(coupons) !== 'array') { coupons = []; } coupons.push(code); - coupons = coupons.filter(function (value, index, self) { + coupons = coupons.filter(function(value, index, self) { return self.indexOf(value) === index; }); @@ -198,7 +235,7 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ * Is the coupon in the order process? * @return {boolean} */ - isInOrder: function () { + isInOrder: function() { var OrderProcessNode = this.getElm().getParent( '[data-qui="package/quiqqer/order/bin/frontend/controls/OrderProcess"]' ); @@ -211,7 +248,7 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ * * @return {Object} */ - getOrderProcess: function () { + getOrderProcess: function() { var OrderProcessNode = this.getElm().getParent( '[data-qui="package/quiqqer/order/bin/frontend/controls/OrderProcess"]' ); @@ -224,19 +261,19 @@ define('package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput', [ * * @return {Promise} */ - removeCouponsFromOrder: function () { + removeCouponsFromOrder: function() { if (!this.isInOrder()) { return Promise.resolve(); } var self = this; - return new Promise(function (resolve) { + return new Promise(function(resolve) { var OrderProcess = self.getOrderProcess(); OrderProcess.Loader.show(); - OrderProcess.getOrder().then(function (orderHash) { - QUIAjax.get('package_quiqqer_coupons_ajax_frontend_removeCoupons', function () { + OrderProcess.getOrder().then(function(orderHash) { + QUIAjax.get('package_quiqqer_coupons_ajax_frontend_removeCoupons', function() { resolve(); OrderProcess.reload(); }, { diff --git a/events.xml b/events.xml index f267a3e3c895402b6d0115c714f6a578f9faf23f..ba9ec1f86adc1f0bab8b186757181b1e0e78fb08 100644 --- a/events.xml +++ b/events.xml @@ -42,4 +42,7 @@ <event on="onQuiqqerOrderSuccessful" fire="\QUI\ERP\Coupons\Events::removeCouponsFromSession"/> <event on="onQuiqqer::order::orderProcessFinish" fire="\QUI\ERP\Coupons\Events::removeCouponsFromSession"/> <event on="onQuiqqerMigrationV2" fire="\QUI\ERP\Coupons\Events::onQuiqqerMigrationV2"/> + + <!-- simple order compatibility --> + <event on="onQuiqqer::simple::order::basket::middle" fire="\QUI\ERP\Coupons\Events::templateOrderSimpleOrder"/> </events> diff --git a/src/QUI/ERP/Coupons/Events.php b/src/QUI/ERP/Coupons/Events.php index b46dd5f68348556bef36177209a45c45cab05fff..245150326591f6fae5b76c842d6f25e152859e99 100644 --- a/src/QUI/ERP/Coupons/Events.php +++ b/src/QUI/ERP/Coupons/Events.php @@ -96,6 +96,26 @@ public static function templateOrderProcessBasketEnd( ); } + public static function templateOrderSimpleOrder( + Collector $Collector, + AbstractOrder $Order + ): void { + if ($Order instanceof QUI\ERP\Order\OrderInProcess && isset($_GET['coupon'])) { + try { + $code = Handler::sanitizeCode($_GET['coupon']); + + $CouponCode = Handler::getCouponCodeByCode($code); + $CouponCode->checkRedemption(QUI::getUserBySession()); + $CouponCode->addToOrder($Order); + } catch (Exception) { + } + } + + $Collector->append( + '<div data-qui="package/quiqqer/coupons/bin/frontend/controls/CouponCodeInput"></div>' + ); + } + /** * @param QUI\ERP\Order\OrderProcess $OrderProcess * @throws QUI\ERP\Order\Exception