From 1b662b0158c10b18942474c15a7633b6cf086d9c Mon Sep 17 00:00:00 2001 From: Tiago Sampaio Date: Mon, 18 May 2020 13:33:11 -0300 Subject: [PATCH] Changes: - Improvements to quotation in product page. --- .../controllers/ProductController.php | 8 +- .../community/Frenet/Shipping/etc/config.xml | 13 ++ .../Frenet/Shipping/etc/jstranslator.xml | 6 + .../community/Frenet/Shipping/etc/system.xml | 13 ++ .../default/layout/frenet/frenet_shipping.xml | 18 +++ .../shipping/catalog/product/view/quote.phtml | 96 ++------------ app/locale/pt_BR/Frenet/Shipping.csv | 7 + .../base/default/css/frenet/shipping.css | 27 ++++ .../base/default/js/frenet/shipping.js | 124 ++++++++++++++++++ 9 files changed, 226 insertions(+), 86 deletions(-) create mode 100644 app/code/community/Frenet/Shipping/etc/jstranslator.xml create mode 100644 skin/frontend/base/default/css/frenet/shipping.css create mode 100644 skin/frontend/base/default/js/frenet/shipping.js diff --git a/app/code/community/Frenet/Shipping/controllers/ProductController.php b/app/code/community/Frenet/Shipping/controllers/ProductController.php index 5f1a086..a95c346 100644 --- a/app/code/community/Frenet/Shipping/controllers/ProductController.php +++ b/app/code/community/Frenet/Shipping/controllers/ProductController.php @@ -16,10 +16,10 @@ protected function _construct() public function quoteAction() { - $productId = (int) $this->getRequest()->getPost('product'); - $postcode = (string) $this->getRequest()->getPost('postcode'); - $qty = (float) $this->getRequest()->getPost('qty'); - $options = (array) $this->getRequest()->getPost(); + $productId = (int) $this->getRequest()->getPost('product'); + $postcode = (string) $this->getRequest()->getPost('postcode'); + $qty = (float) $this->getRequest()->getPost('qty'); + $options = (array) $this->getRequest()->getPost(); $this->getResponse()->setHeader('Content-type', 'application/json', true); diff --git a/app/code/community/Frenet/Shipping/etc/config.xml b/app/code/community/Frenet/Shipping/etc/config.xml index bb06da0..3ff96cc 100644 --- a/app/code/community/Frenet/Shipping/etc/config.xml +++ b/app/code/community/Frenet/Shipping/etc/config.xml @@ -1,4 +1,17 @@ + diff --git a/app/code/community/Frenet/Shipping/etc/jstranslator.xml b/app/code/community/Frenet/Shipping/etc/jstranslator.xml new file mode 100644 index 0000000..7574a60 --- /dev/null +++ b/app/code/community/Frenet/Shipping/etc/jstranslator.xml @@ -0,0 +1,6 @@ + + + + day(s) + + diff --git a/app/code/community/Frenet/Shipping/etc/system.xml b/app/code/community/Frenet/Shipping/etc/system.xml index 59d15af..d6fd2f9 100644 --- a/app/code/community/Frenet/Shipping/etc/system.xml +++ b/app/code/community/Frenet/Shipping/etc/system.xml @@ -1,4 +1,17 @@ + diff --git a/app/design/frontend/base/default/layout/frenet/frenet_shipping.xml b/app/design/frontend/base/default/layout/frenet/frenet_shipping.xml index 774eebb..d30992a 100644 --- a/app/design/frontend/base/default/layout/frenet/frenet_shipping.xml +++ b/app/design/frontend/base/default/layout/frenet/frenet_shipping.xml @@ -1,6 +1,24 @@ + + + + skin_jsjs/frenet/shipping.js + skin_csscss/frenet/shipping.css + diff --git a/app/design/frontend/base/default/template/frenet/shipping/catalog/product/view/quote.phtml b/app/design/frontend/base/default/template/frenet/shipping/catalog/product/view/quote.phtml index 8034db6..0c7486e 100644 --- a/app/design/frontend/base/default/template/frenet/shipping/catalog/product/view/quote.phtml +++ b/app/design/frontend/base/default/template/frenet/shipping/catalog/product/view/quote.phtml @@ -20,14 +20,7 @@ $product = $this->getProduct(); ?> isSaleable()) :?> - - - -getJsLayout() ?> - - - -
+

__('Shipping Quote')?>

__('Calculate the shipping quote for this product with Frenet.')?>

@@ -42,8 +35,7 @@ $product = $this->getProduct(); min="0" value="" maxlength="9" - class="input-text postcode" - data-validate="required: true" + class="input-text postcode required" data-bind="value: postcode, event: {change: activate}, attr: {title: fieldTitle}" />
@@ -60,89 +52,29 @@ $product = $this->getProduct();
- +
- - + + - - - + + + - - - - - - - - - - - +
-
+
diff --git a/app/locale/pt_BR/Frenet/Shipping.csv b/app/locale/pt_BR/Frenet/Shipping.csv index 8557b68..379b3b3 100644 --- a/app/locale/pt_BR/Frenet/Shipping.csv +++ b/app/locale/pt_BR/Frenet/Shipping.csv @@ -30,3 +30,10 @@ "Debug Filename","Nome do Arquivo de Log" "Sort Order","Ordenação" "When the cart's total weight is greater than the limit (30kg) multiple calls will be done for Correios.

Note: with this feature enabled you'll need to ship the products in diferent packages.

Warning: The shipping rules and restrictions based in cart total or weight may not work properly when this option is enabled.","Quando o peso total do carrinho for maior do que o limite (30kg), múltiplas cotações serão feitas para o método dos Correios apenas.

Nota: com essa funcionalidade habilitada você precisará enviar os produtos em pacotes diferentes.

Warning: As regras e restrições de frete baseada em valor de carrinho ou peso pode não funcionar corretamente ao habilitar essa funcionalidade." +"Calculate the shipping quote for this product with Frenet.","Calcule a cotação de entrega para este produto com a Frenet." +"Postcode","CEP" +"Get Quote","Obter Cotação" +"Description","Descrição" +"Delivery Time","Tempo de Entrega" +"Price","Preço" +" day(s)"," dias(s)" diff --git a/skin/frontend/base/default/css/frenet/shipping.css b/skin/frontend/base/default/css/frenet/shipping.css new file mode 100644 index 0000000..ba66cd2 --- /dev/null +++ b/skin/frontend/base/default/css/frenet/shipping.css @@ -0,0 +1,27 @@ +/** + * Frenet Shipping Gateway + * + * @category Frenet + * + * @author Tiago Sampaio + * @link https://github.com/tiagosampaio + * @link https://tiagosampaio.com + * + * Copyright (c) 2020. + */ + +.box-frenet-quote { + margin: 20px 0; +} + +.box-frenet-quote .data-table { + margin: 20px 0; +} + +.box-frenet-quote .field { + margin-top: 10px; +} + +.box-frenet-quote .field .postcode { + margin: 10px 0; +} diff --git a/skin/frontend/base/default/js/frenet/shipping.js b/skin/frontend/base/default/js/frenet/shipping.js new file mode 100644 index 0000000..5a3705b --- /dev/null +++ b/skin/frontend/base/default/js/frenet/shipping.js @@ -0,0 +1,124 @@ +/** + * Frenet Shipping Gateway + * + * @category Frenet + * + * @author Tiago Sampaio + * @link https://github.com/tiagosampaio + * @link https://tiagosampaio.com + * + * Copyright (c) 2020. + */ + +var ProductQuote = Class.create(); +ProductQuote.prototype = { + initialize: function (config) { + this.url = '/frenet/product/quote'; + this.postcode = null; + this.active = false; + this.field = $('frenet-postcode-field'); + this.button = $('frenet-postcode-button'); + this.table = $('frenet-result-table'); + this.tableBody = $('frenet-result-table-body'); + this.errorMessage = null; + this.priceFormat = config.priceFormat; + + this.field.observe('change', this.check.bind(this)); + this.button.observe('click', this.updateRates.bind(this)); + this.disable(); + }, + check: function () { + if (this.field.value.length === 0) { + this.disable(); + this.reset(); + return; + } + + this.enable(); + }, + reset: function () { + this.table.hide(); + this.tableBody.update(''); + }, + updateRates: function () { + this.postcode = this.field.value; + this.reset(); + + if (this.postcode) { + // this.loaderStart(); + + new Ajax.Request(this.url, { + method: 'POST', + parameters: $('product_addtocart_form').serialize(), + onSuccess: this.processSuccess.bind(this), + onFailure: this.processFailure.bind(this), + onComplete: this.processAlways.bind(this) + }); + } + + if (!this.postcode) { + } + }, + processSuccess: function (result) { + // console.log("RESULT SUCCESS", result); + var response = result.responseJSON; + + if (response.error) { + this.processFailure(result); + return; + } + + this.pushRates(response.rates); + }, + processFailure: function (result) { + // console.log("RESULT FAILURE", result); + // this.reset(); + this.errorMessage(result.message); + // this.error(true); + }, + processAlways: function (result) { + console.log("RESULT ALWAYS", result); + this.table.show(); + // this.loaderStop(); + }, + pushRates: function (rates) { + var Rates = $A(rates); + + if (Rates.size() <= 0) { + return; + // this.visible(true); + // this.error(false); + // this.deactivate(); + } + + Rates.each(this.appendRate.bind(this)); + + // if (rates.length === 0) { + // this.visible(false); + // } + + // this.displayNoResults(!this.visible()); + }, + appendRate: function (rate, index) { + var row = new Element('tr'); + this.createColumn(row, rate.service_description); + this.createColumn(row, this.formatDeliveryTime(rate.delivery_time)); + this.createColumn(row, this.formatPrice(rate.shipping_price)); + this.tableBody.appendChild(row); + }, + createColumn: function (row, text) { + row.appendChild(new Element('td').update(text)) + }, + formatDeliveryTime: function (days) { + return days + Translator.translate(' day(s)') + }, + formatPrice: function (price) { + return formatCurrency(price, this.priceFormat); + }, + disable: function () { + this.button.addClassName('disabled'); + }, + enable: function () { + this.button.removeClassName('disabled'); + } +}