Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- Improvements to quotation in product page.
  • Loading branch information
tiagosampaio committed May 18, 2020
1 parent c8e489f commit 1b662b0
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
13 changes: 13 additions & 0 deletions app/code/community/Frenet/Shipping/etc/config.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
<?xml version="1.0"?>
<!--
/**
* Frenet Shipping Gateway
*
* @category Frenet
*
* @author Tiago Sampaio <[email protected]>
* @link https://github.com/tiagosampaio
* @link https://tiagosampaio.com
*
* Copyright (c) 2020.
*/
-->
<config>
<modules>
<Frenet_Shipping>
Expand Down
6 changes: 6 additions & 0 deletions app/code/community/Frenet/Shipping/etc/jstranslator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<jstranslator>
<delivery-time translate="message" module="frenet_shipping">
<message> day(s)</message>
</delivery-time>
</jstranslator>
13 changes: 13 additions & 0 deletions app/code/community/Frenet/Shipping/etc/system.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
<?xml version="1.0"?>
<!--
/**
* Frenet Shipping Gateway
*
* @category Frenet
*
* @author Tiago Sampaio <[email protected]>
* @link https://github.com/tiagosampaio
* @link https://tiagosampaio.com
*
* Copyright (c) 2020.
*/
-->
<config>
<sections>
<carriers>
Expand Down
18 changes: 18 additions & 0 deletions app/design/frontend/base/default/layout/frenet/frenet_shipping.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
<?xml version="1.0"?>
<!--
/**
* Frenet Shipping Gateway
*
* @category Frenet
*
* @author Tiago Sampaio <[email protected]>
* @link https://github.com/tiagosampaio
* @link https://tiagosampaio.com
*
* Copyright (c) 2020.
*/
-->
<layout version="0.1.0">
<frenet_product_shipping_quote>
<reference name="head">
<action method="addJs"><script>varien/js.js</script></action>
<action method="addItem"><type>skin_js</type><name>js/frenet/shipping.js</name></action>
<action method="addItem"><type>skin_css</type><name>css/frenet/shipping.css</name></action>
</reference>
<block type="frenet_shipping/catalog_product_view_quote" name="product.info.frenet.shipping.quote" as="frenet.shipping.quote" template="frenet/shipping/catalog/product/view/quote.phtml">
</block>
<reference name="product.info.extrahint">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ $product = $this->getProduct();
?>

<?php if ($product->isSaleable()) :?>
<!-- <script type="text/x-magento-init">-->
<!-- {-->
<!-- "*": {-->
<!-- "Magento_Ui/js/core/app": --><?//= $block->getJsLayout() ?>
<!-- }-->
<!-- }-->
<!-- </script>-->
<div class="box-frenet-quote" data-bind="scope: 'frenet-quote'">
<div class="box-frenet-quote">
<div class="fieldset">
<h2><?php echo $this->__('Shipping Quote')?></h2>
<p><?php echo $this->__('Calculate the shipping quote for this product with Frenet.')?></p>
Expand All @@ -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}"
/>
</div>
Expand All @@ -60,89 +52,29 @@ $product = $this->getProduct();
<div data-bind="visible: displayNoResults">
<span data-bind="text: noResultsMessage"></span>
</div>
<table data-bind="visible: visible" class="data table">
<table id="frenet-result-table" class="data-table" style="display: none;">
<col width="60%">
<col width="10%">
<col width="10%">
<col width="20%">
<col width="20%">
<thead>
<tr>
<th data-bind="i18n: 'Description'"></th>
<th data-bind="i18n: 'Time'"></th>
<th data-bind="i18n: 'Price'"></th>
<th><?php echo $this->__('Description') ?></th>
<th><?php echo $this->__('Delivery Time') ?></th>
<th><?php echo $this->__('Price') ?></th>
</tr>
</thead>
<tbody>
<!-- ko foreach: rates -->
<tr>
<td data-bind="text: service_description"></td>
<td data-bind="text: delivery_time"></td>
<td data-bind="text: shipping_price"></td>
</tr>
<tr data-bind="visible: message" class="footer">
<td colspan="4">
<small class="note" data-bind="text: message"></small>
</td>
</tr>
<!-- /ko -->
<tbody id="frenet-result-table-body">
</tbody>
</table>
</div>
<div id="frenet-loader" data-mage-init='{"loader": {}}'>
<div id="frenet-loader">
</div>
</div>
</div>
<script type="text/javascript">
var productQuote = Class.create();
productQuote.prototype = {
initialize: function () {
this.url = '/frenet/product/quote';
this.postcode = null;
this.active = false;
this.field = $('frenet-postcode-field');
this.button = $('frenet-postcode-button');

// this.field.observe('change', this.updateRates.bind(this));
this.button.observe('click', this.updateRates.bind(this));
},
updateRates: function () {
this.postcode = this.field.value;
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()) {
// this.reset();
// }
},
processSuccess: function (result) {
console.log("RESULT SUCCESS", result);
// if (result.error) {
// this.processFailure(result);
// return;
// }

// this.pushRates(result.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.loaderStop();
},
}

var quote = new productQuote();
var config = {
priceFormat: JSON.parse('<?php echo json_encode(Mage::app()->getLocale()->getJsPriceFormat()) ?>')
};
var quote = new ProductQuote(config);
</script>
<?php endif; ?>
7 changes: 7 additions & 0 deletions app/locale/pt_BR/Frenet/Shipping.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br/><br/><b>Note</b>: with this feature enabled you'll need to ship the products in diferent packages.<br/><br/><b style='color:red;'>Warning</b>: 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.<br/><br/><b>Nota</b>: com essa funcionalidade habilitada você precisará enviar os produtos em pacotes diferentes.<br/><br/><b style='color:red;'>Warning</b>: 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)"
27 changes: 27 additions & 0 deletions skin/frontend/base/default/css/frenet/shipping.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Frenet Shipping Gateway
*
* @category Frenet
*
* @author Tiago Sampaio <[email protected]>
* @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;
}
124 changes: 124 additions & 0 deletions skin/frontend/base/default/js/frenet/shipping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* Frenet Shipping Gateway
*
* @category Frenet
*
* @author Tiago Sampaio <[email protected]>
* @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');
}
}

0 comments on commit 1b662b0

Please sign in to comment.