Skip to content

Commit

Permalink
add eventlistener for zipcode updates, and set zipcode accordingly on…
Browse files Browse the repository at this point in the history
… quote as fix for some shippingmethods
  • Loading branch information
iesus committed Jan 23, 2019
1 parent f5b1fae commit 8c3350f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
9 changes: 7 additions & 2 deletions Controller/Index/updateCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function __construct(
$this->cart = $cart;
$this->quoteFactory = $quoteFactory;
$this->configProvider = $configProvider;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;


parent::__construct($context);
Expand Down Expand Up @@ -266,9 +266,14 @@ protected function updateQty($quote, $cart)
*/
protected function updateShipping($quote, $method=null)
{
$sveaOrder = $this->buildOrder->getOrder($quote, false);
$this->quoteRepository->save($quote);

$shippingAddress = $quote->getShippingAddress();

if (isset($sveaOrder['ShippingAddress']['PostalCode'])) {
$shippingAddress->setPostcode($sveaOrder['ShippingAddress']['PostalCode']);
}

if($shippingAddress) {
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
Expand Down
2 changes: 1 addition & 1 deletion Model/Api/BuildOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ protected function _addShipping($sveaOrder, $quote, $noSave)
$shippingAddress = $quote->getShippingAddress();

//Chosen shipping method.
if ($quote->getShippingAddress()->getShippingMethod()) {
if ($shippingAddress->getShippingMethod()) {
$method = $shippingAddress->getShippingMethod();
}

Expand Down
23 changes: 19 additions & 4 deletions view/frontend/web/js/sveacheckout.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require([
'Magento_Checkout/js/model/shipping-service'
], function ($, getTotals, quote, shippingEstimation, checkoutData, addressConverter, customerData) {


var addressData = {'countryId': sveacheckout.country};
checkoutData.setShippingAddressFromData(addressData);

Expand Down Expand Up @@ -122,6 +121,7 @@ require([
if ('scoApi' in window) {
window.scoApi.setCheckoutEnabled(false);
}

// submit form
$.ajax({
type: 'POST',
Expand Down Expand Up @@ -167,14 +167,29 @@ require([
});
}


/**
* Observe the validationresult, check for last error message, if found display it.
*/
// Update checkout window
if ('scoApi' in window) {
window.scoApi.observeModel('validation.result', function() {
window.scoApi.observeEvent('validation.result', function() {
window.scoApi.setCheckoutEnabled(true);
});
}

function observeEvents($) {
if ('scoApi' in window) {
window.scoApi.observeEvent("identity.postalCode", function (data) {
$('#shipping-zip-form [name="postcode"]').val(data.value);
$('#shipping-zip-form [name="postcode"]').trigger('change', this);
});
} else {
setTimeout(setupObservers, 500, $);
}
}

function setupObservers($) {
observeEvents($);
}

setupObservers($);
});

0 comments on commit 8c3350f

Please sign in to comment.