Skip to content

Commit e1b0bb8

Browse files
author
Akimov, Alexander(aakimov)
committed
Merge pull request #418 from magento-folks/checkout
[Folks] Enhanced Checkout Flow. Part III
2 parents 0df70ba + b885ed2 commit e1b0bb8

File tree

9 files changed

+88
-22
lines changed

9 files changed

+88
-22
lines changed

app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
*/
55
define([
66
'jquery',
7+
'mage/translate',
78
'jquery/ui'
8-
], function($) {
9+
], function($, $t) {
910
"use strict";
1011

1112
$.widget('mage.catalogAddToCart', {
@@ -16,7 +17,13 @@ define([
1617
bindSubmit: true,
1718
minicartSelector: '[data-block="minicart"]',
1819
messagesSelector: '[data-placeholder="messages"]',
19-
productStatusSelector: '.stock.available'
20+
productStatusSelector: '.stock.available',
21+
addToCartButtonSelector: '.action.tocart',
22+
addToCartButtonDisabledClass: 'disabled',
23+
addToCartButtonTextWhileAdding: $t('Adding...'),
24+
addToCartButtonTextAdded: $t('Added'),
25+
addToCartButtonTextDefault: $t('Add to Cart')
26+
2027
},
2128

2229
_create: function() {
@@ -49,6 +56,9 @@ define([
4956

5057
ajaxSubmit: function(form) {
5158
var self = this;
59+
$(self.options.minicartSelector).trigger('contentLoading');
60+
self.disableAddToCartButton(form);
61+
5262
$.ajax({
5363
url: form.attr('action'),
5464
data: form.serialize(),
@@ -82,8 +92,30 @@ define([
8292
.find('span')
8393
.html(res.product.statusText);
8494
}
95+
self.enableAddToCartButton(form);
8596
}
8697
});
98+
},
99+
100+
disableAddToCartButton: function(form) {
101+
var addToCartButton = $(form).find(this.options.addToCartButtonSelector);
102+
addToCartButton.addClass(this.options.addToCartButtonDisabledClass);
103+
addToCartButton.attr('title', this.options.addToCartButtonTextWhileAdding);
104+
addToCartButton.find('span').text(this.options.addToCartButtonTextWhileAdding);
105+
},
106+
107+
enableAddToCartButton: function(form) {
108+
var self = this,
109+
addToCartButton = $(form).find(this.options.addToCartButtonSelector);
110+
111+
addToCartButton.find('span').text(this.options.addToCartButtonTextAdded);
112+
addToCartButton.attr('title', this.options.addToCartButtonTextAdded);
113+
114+
setTimeout(function() {
115+
addToCartButton.removeClass(self.options.addToCartButtonDisabledClass);
116+
addToCartButton.find('span').text(self.options.addToCartButtonTextDefault);
117+
addToCartButton.attr('title', self.options.addToCartButtonTextDefault);
118+
}, 1000);
87119
}
88120
});
89121

app/code/Magento/Checkout/Model/ShippingInformationManagement.php

+23-10
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public function __construct(
9494

9595
/**
9696
* {@inheritDoc}
97-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
9897
* @SuppressWarnings(PHPMD.NPathComplexity)
9998
*/
10099
public function saveAddressInformation(
@@ -107,15 +106,7 @@ public function saveAddressInformation(
107106

108107
/** @var \Magento\Quote\Model\Quote $quote */
109108
$quote = $this->quoteRepository->getActive($cartId);
110-
if ($quote->isVirtual()) {
111-
throw new NoSuchEntityException(
112-
__('Cart contains virtual product(s) only. Shipping address is not applicable.')
113-
);
114-
}
115-
116-
if (0 == $quote->getItemsCount()) {
117-
throw new InputException(__('Shipping method is not applicable for empty cart'));
118-
}
109+
$this->validateQuote($quote);
119110

120111
$saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
121112
$sameAsBilling = $address->getSameAsBilling() ? 1 : 0;
@@ -139,6 +130,7 @@ public function saveAddressInformation(
139130
$address->setShippingMethod($carrierCode . '_' . $methodCode);
140131

141132
try {
133+
/** TODO: refactor this code. Eliminate save operation */
142134
$address->save();
143135
$address->collectTotals();
144136
} catch (\Exception $e) {
@@ -175,4 +167,25 @@ public function saveAddressInformation(
175167
$paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
176168
return $paymentDetails;
177169
}
170+
171+
/**
172+
* Validate quote
173+
*
174+
* @param \Magento\Quote\Model\Quote $quote
175+
* @throws InputException
176+
* @throws NoSuchEntityException
177+
* @return void
178+
*/
179+
protected function validateQuote(\Magento\Quote\Model\Quote $quote)
180+
{
181+
if ($quote->isVirtual()) {
182+
throw new NoSuchEntityException(
183+
__('Cart contains virtual product(s) only. Shipping address is not applicable.')
184+
);
185+
}
186+
187+
if (0 == $quote->getItemsCount()) {
188+
throw new InputException(__('Shipping method is not applicable for empty cart'));
189+
}
190+
}
178191
}

app/code/Magento/Checkout/view/frontend/templates/cart/minicart.phtml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
/** @var $block \Magento\Checkout\Block\Cart\Sidebar */
1010
?>
1111

12-
<div data-block="minicart" class="minicart-wrapper" >
12+
<div data-block="minicart" class="minicart-wrapper">
1313
<a class="action showcart" href="<?php echo $block->getShoppingCartUrl(); ?>"
1414
data-bind="scope: 'minicart_content'">
1515
<span class="text"><?php echo __('My Cart'); ?></span>
1616
<span class="counter qty empty"
17-
data-bind="css: { empty: cart().summary_count == 0 }">
17+
data-bind="css: { empty: cart().summary_count == 0 }, blockLoader: isLoading">
1818
<span class="counter-number"><!-- ko text: cart().summary_count --><!-- /ko --></span>
1919
<span class="counter-label">
2020
<!-- ko if: cart().summary_count -->

app/code/Magento/Checkout/view/frontend/web/js/view/minicart.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ define([
1313
'use strict';
1414

1515
var sidebarInitialized = false;
16-
16+
var addToCartCalls = 0;
1717
url.setBaseUrl(window.checkout.baseUrl);
1818

1919
function initSidebar() {
@@ -60,12 +60,20 @@ define([
6060
return Component.extend({
6161
shoppingCartUrl: window.checkout.shoppingCartUrl,
6262
initialize: function () {
63+
var self = this;
6364
this._super();
6465
this.cart = customerData.get('cart');
6566
this.cart.subscribe(function () {
67+
addToCartCalls--;
68+
this.isLoading(addToCartCalls > 0);
6669
sidebarInitialized = false;
70+
}, this);
71+
$('[data-block="minicart"]').on('contentLoading', function(event) {
72+
addToCartCalls++;
73+
self.isLoading(true);
6774
});
6875
},
76+
isLoading: ko.observable(false),
6977
initSidebar: ko.observable(initSidebar),
7078
closeSidebar: function() {
7179
var minicart = $('[data-block="minicart"]');

app/code/Magento/Checkout/view/frontend/web/js/view/payment/default.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ define(
4545
/**
4646
* Place order.
4747
*/
48-
placeOrder: function () {
48+
placeOrder: function (data, event) {
49+
if (event) {
50+
event.preventDefault();
51+
}
4952
var self = this,
5053
placeOrder,
5154
emailValidationResult = customer.isLoggedIn(),
@@ -61,7 +64,9 @@ define(
6164
$.when(placeOrder).fail(function(){
6265
self.isPlaceOrderActionAllowed(true);
6366
});
67+
return true;
6468
}
69+
return false;
6570
},
6671

6772
selectPaymentMethod: function() {

app/code/Magento/OfflinePayments/view/frontend/web/js/view/payment/method-renderer/purchaseorder-method.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ define(
3636

3737
},
3838
validate: function () {
39-
var form = '#purchaseorder';
39+
var form = 'form[data-role=purchaseorder-form]';
4040
return $(form).validation() && $(form).validation('isValid');
4141
}
4242
});

app/code/Magento/OfflinePayments/view/frontend/web/template/payment/purchaseorder-form.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
-->
77
<div class="payment-method" data-bind="css: {'_active': (getCode() == isChecked())}">
8-
<form id="purchaseorder-form" class="form form-purchase-order">
8+
<form id="purchaseorder-form" class="form form-purchase-order" data-role="purchaseorder-form">
99

1010
<div class="payment-method-title field choice">
1111
<input type="radio"

app/code/Magento/Payment/view/frontend/web/template/payment/cc-form.html

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
<!--/ko-->
3232
</ul>
3333
<input type="hidden"
34-
name="payment[cc_number]"
34+
name="payment[cc_type]"
3535
class="input-text"
3636
value=""
37-
data-bind="attr: {id: getCode() + '_cc_type'}, value: creditCardType">
37+
data-bind="attr: {id: getCode() + '_cc_type', 'data-container': getCode() + '-cc-type'},
38+
value: creditCardType
39+
">
3840
</div>
3941
</div>
4042
<div class="field number required">

app/code/Magento/Paypal/view/frontend/web/js/view/payment/method-renderer/iframe-methods.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ define(
2828
* Places order in pending payment status.
2929
*/
3030
placePendingPaymentOrder: function () {
31-
this.placeOrder(false);
32-
this.isInAction(true);
31+
if (this.placeOrder()) {
32+
this.isInAction(true);
33+
// capture all click events
34+
document.addEventListener('click', function(event) {
35+
event.stopImmediatePropagation();
36+
event.preventDefault();
37+
}, true);
38+
}
3339
}
3440
});
3541
}

0 commit comments

Comments
 (0)