ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,370 @@
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
define([
|
||||
'jquery',
|
||||
'Magento_Customer/js/model/authentication-popup',
|
||||
'Magento_Customer/js/customer-data',
|
||||
'Magento_Ui/js/modal/alert',
|
||||
'Magento_Ui/js/modal/confirm',
|
||||
'underscore',
|
||||
'jquery-ui-modules/widget',
|
||||
'mage/decorate',
|
||||
'mage/collapsible',
|
||||
'mage/cookies',
|
||||
'jquery-ui-modules/effect-fade'
|
||||
], function ($, authenticationPopup, customerData, alert, confirm, _) {
|
||||
'use strict';
|
||||
|
||||
$.widget('mage.sidebar', {
|
||||
options: {
|
||||
isRecursive: true,
|
||||
minicart: {
|
||||
maxItemsVisible: 3
|
||||
}
|
||||
},
|
||||
scrollHeight: 0,
|
||||
shoppingCartUrl: window.checkout.shoppingCartUrl,
|
||||
|
||||
/**
|
||||
* Create sidebar.
|
||||
* @private
|
||||
*/
|
||||
_create: function () {
|
||||
this._initContent();
|
||||
},
|
||||
|
||||
/**
|
||||
* Update sidebar block.
|
||||
*/
|
||||
update: function () {
|
||||
$(this.options.targetElement).trigger('contentUpdated');
|
||||
this._calcHeight();
|
||||
},
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_initContent: function () {
|
||||
var self = this,
|
||||
events = {};
|
||||
|
||||
this.element.decorate('list', this.options.isRecursive);
|
||||
|
||||
/**
|
||||
* @param {jQuery.Event} event
|
||||
*/
|
||||
events['click ' + this.options.button.close] = function (event) {
|
||||
event.stopPropagation();
|
||||
/** OLEGNAX:Old
|
||||
$(self.options.targetElement).dropdownDialog('close');
|
||||
*/
|
||||
/** OLEGNAX:Start **/
|
||||
$(self.options.targetElement).OXmodalMinicart('close');
|
||||
/** OLEGNAX:End **/
|
||||
};
|
||||
events['click ' + this.options.button.checkout] = $.proxy(function () {
|
||||
var cart = customerData.get('cart'),
|
||||
customer = customerData.get('customer'),
|
||||
cookieOptions = {
|
||||
samesite: 'lax'
|
||||
},
|
||||
element = $(this.options.button.checkout);
|
||||
|
||||
if (!customer().firstname && cart().isGuestCheckoutAllowed === false) {
|
||||
// set URL for redirect on successful login/registration. It's postprocessed on backend.
|
||||
$.cookie('login_redirect', this.options.url.checkout, cookieOptions);
|
||||
|
||||
if (this.options.url.isRedirectRequired) {
|
||||
element.prop('disabled', true);
|
||||
location.href = this.options.url.loginUrl;
|
||||
} else {
|
||||
authenticationPopup.showModal();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
element.prop('disabled', true);
|
||||
location.href = this.options.url.checkout;
|
||||
}, this);
|
||||
|
||||
/**
|
||||
* @param {jQuery.Event} event
|
||||
*/
|
||||
events['click ' + this.options.button.remove] = function (event) {
|
||||
event.stopPropagation();
|
||||
confirm({
|
||||
content: self.options.confirmMessage,
|
||||
actions: {
|
||||
/** @inheritdoc */
|
||||
confirm: function () {
|
||||
self._removeItem($(event.currentTarget));
|
||||
},
|
||||
|
||||
/** @inheritdoc */
|
||||
always: function (e) {
|
||||
e.stopImmediatePropagation();
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {jQuery.Event} event
|
||||
*/
|
||||
events['keyup ' + this.options.item.qty] = function (event) {
|
||||
self._showItemButton($(event.target));
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {jQuery.Event} event
|
||||
*/
|
||||
events['change ' + this.options.item.qty] = function (event) {
|
||||
self._showItemButton($(event.target));
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {jQuery.Event} event
|
||||
*/
|
||||
events['click ' + this.options.item.button] = function (event) {
|
||||
event.stopPropagation();
|
||||
self._updateItemQty($(event.currentTarget));
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {jQuery.Event} event
|
||||
*/
|
||||
events['focusout ' + this.options.item.qty] = function (event) {
|
||||
self._validateQty($(event.currentTarget));
|
||||
};
|
||||
|
||||
this._on(this.element, events);
|
||||
this._calcHeight();
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elem
|
||||
* @private
|
||||
*/
|
||||
_showItemButton: function (elem) {
|
||||
var itemId = elem.data('cart-item'),
|
||||
itemQty = elem.data('item-qty');
|
||||
|
||||
if (this._isValidQty(itemQty, elem.val())) {
|
||||
$('#update-cart-item-' + itemId).show('fade', 300);
|
||||
} else if (elem.val() == 0) { //eslint-disable-line eqeqeq
|
||||
this._hideItemButton(elem);
|
||||
} else {
|
||||
this._hideItemButton(elem);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {*} origin - origin qty. 'data-item-qty' attribute.
|
||||
* @param {*} changed - new qty.
|
||||
* @returns {Boolean}
|
||||
* @private
|
||||
*/
|
||||
_isValidQty: function (origin, changed) {
|
||||
return origin != changed && //eslint-disable-line eqeqeq
|
||||
changed.length > 0 &&
|
||||
changed - 0 == changed && //eslint-disable-line eqeqeq
|
||||
changed - 0 > 0;
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {Object} elem
|
||||
* @private
|
||||
*/
|
||||
_validateQty: function (elem) {
|
||||
var itemQty = elem.data('item-qty');
|
||||
|
||||
if (!this._isValidQty(itemQty, elem.val())) {
|
||||
elem.val(itemQty);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elem
|
||||
* @private
|
||||
*/
|
||||
_hideItemButton: function (elem) {
|
||||
var itemId = elem.data('cart-item');
|
||||
|
||||
$('#update-cart-item-' + itemId).hide('fade', 300);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elem
|
||||
* @private
|
||||
*/
|
||||
_updateItemQty: function (elem) {
|
||||
var itemId = elem.data('cart-item');
|
||||
|
||||
this._ajax(this.options.url.update, {
|
||||
'item_id': itemId,
|
||||
'item_qty': $('#cart-item-' + itemId + '-qty').val()
|
||||
}, elem, this._updateItemQtyAfter);
|
||||
},
|
||||
|
||||
/**
|
||||
* Update content after update qty
|
||||
*
|
||||
* @param {HTMLElement} elem
|
||||
*/
|
||||
_updateItemQtyAfter: function (elem) {
|
||||
var productData = this._getProductById(Number(elem.data('cart-item')));
|
||||
|
||||
if (!_.isUndefined(productData)) {
|
||||
$(document).trigger('ajax:updateCartItemQty');
|
||||
|
||||
if (window.location.href === this.shoppingCartUrl) {
|
||||
window.location.reload(false);
|
||||
}
|
||||
}
|
||||
this._hideItemButton(elem);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elem
|
||||
* @private
|
||||
*/
|
||||
_removeItem: function (elem) {
|
||||
var itemId = elem.data('cart-item');
|
||||
|
||||
this._ajax(this.options.url.remove, {
|
||||
'item_id': itemId
|
||||
}, elem, this._removeItemAfter);
|
||||
},
|
||||
|
||||
/**
|
||||
* Update content after item remove
|
||||
*
|
||||
* @param {Object} elem
|
||||
* @private
|
||||
*/
|
||||
_removeItemAfter: function (elem) {
|
||||
var productData = this._getProductById(Number(elem.data('cart-item')));
|
||||
|
||||
if (!_.isUndefined(productData)) {
|
||||
$(document).trigger('ajax:removeFromCart', {
|
||||
productIds: [productData['product_id']],
|
||||
productInfo: [
|
||||
{
|
||||
'id': productData['product_id']
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
if (window.location.href.indexOf(this.shoppingCartUrl) === 0) {
|
||||
window.location.reload();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieves product data by Id.
|
||||
*
|
||||
* @param {Number} productId - product Id
|
||||
* @returns {Object|undefined}
|
||||
* @private
|
||||
*/
|
||||
_getProductById: function (productId) {
|
||||
return _.find(customerData.get('cart')().items, function (item) {
|
||||
return productId === Number(item['item_id']);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {String} url - ajax url
|
||||
* @param {Object} data - post data for ajax call
|
||||
* @param {Object} elem - element that initiated the event
|
||||
* @param {Function} callback - callback method to execute after AJAX success
|
||||
*/
|
||||
_ajax: function (url, data, elem, callback) {
|
||||
$.extend(data, {
|
||||
'form_key': $.mage.cookies.get('form_key')
|
||||
});
|
||||
|
||||
$.ajax({
|
||||
url: url,
|
||||
data: data,
|
||||
type: 'post',
|
||||
dataType: 'json',
|
||||
context: this,
|
||||
|
||||
/** @inheritdoc */
|
||||
beforeSend: function () {
|
||||
elem.attr('disabled', 'disabled');
|
||||
},
|
||||
|
||||
/** @inheritdoc */
|
||||
complete: function () {
|
||||
elem.attr('disabled', null);
|
||||
}
|
||||
})
|
||||
.done(function (response) {
|
||||
var msg;
|
||||
|
||||
if (response.success) {
|
||||
callback.call(this, elem, response);
|
||||
} else {
|
||||
msg = response['error_message'];
|
||||
|
||||
if (msg) {
|
||||
alert({
|
||||
content: msg
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
.fail(function (error) {
|
||||
console.log(JSON.stringify(error));
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Calculate height of minicart list
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_calcHeight: function () {
|
||||
var self = this,
|
||||
height = 0,
|
||||
counter = this.options.minicart.maxItemsVisible,
|
||||
target = $(this.options.minicart.list),
|
||||
outerHeight;
|
||||
/** OLEGNAX:Start **/
|
||||
var _counter = 0;
|
||||
/** OLEGNAX:End **/
|
||||
|
||||
self.scrollHeight = 0;
|
||||
target.children().each(function () {
|
||||
|
||||
if ($(this).find('.options').length > 0) {
|
||||
$(this).collapsible();
|
||||
}
|
||||
outerHeight = $(this).outerHeight(true);
|
||||
|
||||
/** OLEGNAX:Old
|
||||
if (counter-- > 0) {
|
||||
/** OLEGNAX:Start **/
|
||||
_counter++;
|
||||
if (counter > _counter) {
|
||||
/** OLEGNAX:End **/
|
||||
height += outerHeight;
|
||||
}
|
||||
self.scrollHeight += outerHeight;
|
||||
});
|
||||
|
||||
target.parent().height(height);
|
||||
/** OLEGNAX:Start **/
|
||||
$(this.options.targetElement).trigger('calculatedHeight', [target, counter]);
|
||||
/** OLEGNAX:End **/
|
||||
}
|
||||
});
|
||||
|
||||
return $.mage.sidebar;
|
||||
});
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
define([
|
||||
'uiComponent',
|
||||
'Magento_Customer/js/customer-data',
|
||||
'jquery',
|
||||
'ko',
|
||||
'underscore',
|
||||
'sidebar',
|
||||
'mage/translate',
|
||||
'Athlete2/modal'
|
||||
], function (Component, customerData, $, ko, _) {
|
||||
'use strict';
|
||||
|
||||
var sidebarInitialized = false,
|
||||
addToCartCalls = 0,
|
||||
miniCart;
|
||||
|
||||
miniCart = $('div.block.block-minicart[data-role="OXmodalMinicart"]');
|
||||
|
||||
/**
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function initSidebar() {
|
||||
if (miniCart.data('mageSidebar')) {
|
||||
miniCart.sidebar('update');
|
||||
}
|
||||
|
||||
if (!$('[data-role=product-item]').length) {
|
||||
return false;
|
||||
}
|
||||
miniCart.trigger('contentUpdated');
|
||||
|
||||
if (sidebarInitialized) {
|
||||
return false;
|
||||
}
|
||||
sidebarInitialized = true;
|
||||
miniCart.sidebar({
|
||||
'targetElement': 'div.block.block-minicart',
|
||||
'url': {
|
||||
'checkout': window.checkout.checkoutUrl,
|
||||
'update': window.checkout.updateItemQtyUrl,
|
||||
'remove': window.checkout.removeItemUrl,
|
||||
'loginUrl': window.checkout.customerLoginUrl,
|
||||
'isRedirectRequired': window.checkout.isRedirectRequired
|
||||
},
|
||||
'button': {
|
||||
'checkout': '#top-cart-btn-checkout',
|
||||
'remove': '#mini-cart a.action.delete',
|
||||
'close': '#btn-minicart-close'
|
||||
},
|
||||
'showcart': {
|
||||
'parent': 'span.counter',
|
||||
'qty': 'span.counter-number',
|
||||
'label': 'span.counter-label'
|
||||
},
|
||||
'minicart': {
|
||||
'list': '#mini-cart',
|
||||
'content': '#minicart-content-wrapper',
|
||||
'qty': 'div.items-total',
|
||||
'subtotal': 'div.subtotal span.price',
|
||||
'maxItemsVisible': window.checkout.minicartMaxItemsVisible
|
||||
},
|
||||
'item': {
|
||||
'qty': ':input.cart-item-qty',
|
||||
'button': ':button.update-cart-item'
|
||||
},
|
||||
'confirmMessage': $.mage.__('Are you sure you would like to remove this item from the shopping cart?')
|
||||
});
|
||||
}
|
||||
|
||||
initSidebar();
|
||||
|
||||
return Component.extend({
|
||||
shoppingCartUrl: window.checkout.shoppingCartUrl,
|
||||
maxItemsToDisplay: window.checkout.maxItemsToDisplay,
|
||||
cart: {},
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
initialize: function () {
|
||||
var self = this,
|
||||
cartData = customerData.get('cart');
|
||||
|
||||
this.update(cartData());
|
||||
cartData.subscribe(function (updatedCart) {
|
||||
addToCartCalls--;
|
||||
this.isLoading(addToCartCalls > 0);
|
||||
sidebarInitialized = false;
|
||||
this.update(updatedCart);
|
||||
initSidebar();
|
||||
}, this);
|
||||
$('[data-block="minicart"]').on('contentLoading', function () {
|
||||
addToCartCalls++;
|
||||
self.isLoading(true);
|
||||
});
|
||||
|
||||
if (
|
||||
cartData().website_id !== window.checkout.websiteId && cartData().website_id !== undefined ||
|
||||
cartData().storeId !== window.checkout.storeId && cartData().storeId !== undefined
|
||||
) {
|
||||
customerData.reload(['cart'], false);
|
||||
}
|
||||
|
||||
return this._super();
|
||||
},
|
||||
isLoading: ko.observable(false),
|
||||
initSidebar: initSidebar,
|
||||
|
||||
/**
|
||||
* Close mini shopping cart.
|
||||
*/
|
||||
closeMinicart: function () {
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {String} productType
|
||||
* @return {*|String}
|
||||
*/
|
||||
getItemRenderer: function (productType) {
|
||||
return this.itemRenderer[productType] || 'defaultRenderer';
|
||||
},
|
||||
|
||||
/**
|
||||
* Update mini shopping cart content.
|
||||
*
|
||||
* @param {Object} updatedCart
|
||||
* @returns void
|
||||
*/
|
||||
update: function (updatedCart) {
|
||||
_.each(updatedCart, function (value, key) {
|
||||
if (!this.cart.hasOwnProperty(key)) {
|
||||
this.cart[key] = ko.observable();
|
||||
}
|
||||
this.cart[key](value);
|
||||
|
||||
if(window.ajaxCartTransport === true) {
|
||||
$('div.block.block-minicart[data-role="OXmodalMinicart"]').OXmodalMinicart('open', 'added');
|
||||
}
|
||||
if(window.ajaxCartTransport === true || window.ajaxCartTransport === false) {
|
||||
$(document.body).trigger('closeOxQuickview');
|
||||
}
|
||||
window.ajaxCartTransport = null;
|
||||
}, this);
|
||||
},
|
||||
|
||||
/**
|
||||
* Get cart param by name.
|
||||
* @param {String} name
|
||||
* @returns {*}
|
||||
*/
|
||||
getCartParamUnsanitizedHtml: function (name) {
|
||||
if (!_.isUndefined(name)) {
|
||||
if (!this.cart.hasOwnProperty(name)) {
|
||||
this.cart[name] = ko.observable();
|
||||
}
|
||||
}
|
||||
|
||||
var value = this.cart[name]();
|
||||
if (!value) {
|
||||
switch (name) {
|
||||
case 'summary_count':
|
||||
value = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
},
|
||||
/**
|
||||
* @deprecated please use getCartParamUnsanitizedHtml.
|
||||
* @param {String} name
|
||||
* @returns {*}
|
||||
*/
|
||||
getCartParam: function (name) {
|
||||
return this.getCartParamUnsanitizedHtml(name);
|
||||
},
|
||||
/**
|
||||
* Returns array of cart items, limited by 'maxItemsToDisplay' setting
|
||||
* @returns []
|
||||
*/
|
||||
getCartItems: function () {
|
||||
var items = this.getCartParamUnsanitizedHtml('items') || [];
|
||||
|
||||
items = items.slice(parseInt(-this.maxItemsToDisplay, 10));
|
||||
|
||||
return items;
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns count of cart line items
|
||||
* @returns {Number}
|
||||
*/
|
||||
getCartLineItemsCount: function () {
|
||||
var items = this.getCartParamUnsanitizedHtml('items') || [];
|
||||
|
||||
return parseInt(items.length, 10);
|
||||
}
|
||||
});
|
||||
});
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
define([
|
||||
'jquery',
|
||||
'ko',
|
||||
'uiComponent',
|
||||
'underscore',
|
||||
'Magento_Checkout/js/action/select-shipping-address',
|
||||
'Magento_Checkout/js/model/quote',
|
||||
'Magento_Checkout/js/model/shipping-address/form-popup-state',
|
||||
'Magento_Checkout/js/checkout-data',
|
||||
'Magento_Customer/js/customer-data',
|
||||
"Magento_Ui/js/modal/modal"
|
||||
], function ($, ko, Component, _, selectShippingAddressAction, quote, formPopUpState, checkoutData, customerData, modal) {
|
||||
'use strict';
|
||||
|
||||
var countryData = customerData.get('directory-data');
|
||||
|
||||
return Component.extend({
|
||||
defaults: {
|
||||
template: 'Magento_Checkout/shipping-address/address-renderer/default'
|
||||
},
|
||||
|
||||
/** @inheritdoc */
|
||||
initObservable: function () {
|
||||
this._super();
|
||||
this.isSelected = ko.computed(function () {
|
||||
var isSelected = false,
|
||||
shippingAddress = quote.shippingAddress();
|
||||
|
||||
if (shippingAddress) {
|
||||
isSelected = shippingAddress.getKey() == this.address().getKey(); //eslint-disable-line eqeqeq
|
||||
}
|
||||
|
||||
return isSelected;
|
||||
}, this);
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {String} countryId
|
||||
* @return {String}
|
||||
*/
|
||||
getCountryName: function (countryId) {
|
||||
return countryData()[countryId] != undefined ? countryData()[countryId].name : ''; //eslint-disable-line
|
||||
},
|
||||
|
||||
/**
|
||||
* Get customer attribute label
|
||||
*
|
||||
* @param {*} attribute
|
||||
* @returns {*}
|
||||
*/
|
||||
getCustomAttributeLabel: function (attribute) {
|
||||
var label;
|
||||
|
||||
if (typeof attribute === 'string') {
|
||||
return attribute;
|
||||
}
|
||||
|
||||
if (attribute.label) {
|
||||
return attribute.label;
|
||||
}
|
||||
|
||||
if (_.isArray(attribute.value)) {
|
||||
label = _.map(attribute.value, function (value) {
|
||||
return this.getCustomAttributeOptionLabel(attribute['attribute_code'], value) || value;
|
||||
}, this).join(', ');
|
||||
} else if (typeof attribute.value === 'object') {
|
||||
label = _.map(Object.values(attribute.value)).join(', ');
|
||||
} else {
|
||||
label = this.getCustomAttributeOptionLabel(attribute['attribute_code'], attribute.value);
|
||||
}
|
||||
|
||||
return label || attribute.value;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get option label for given attribute code and option ID
|
||||
*
|
||||
* @param {String} attributeCode
|
||||
* @param {String} value
|
||||
* @returns {String|null}
|
||||
*/
|
||||
getCustomAttributeOptionLabel: function (attributeCode, value) {
|
||||
var option,
|
||||
label,
|
||||
options = this.source.get('customAttributes') || {};
|
||||
|
||||
if (options[attributeCode]) {
|
||||
option = _.findWhere(options[attributeCode], {
|
||||
value: value
|
||||
});
|
||||
|
||||
if (option) {
|
||||
label = option.label;
|
||||
}
|
||||
} else if (value.file !== null) {
|
||||
label = value.file;
|
||||
}
|
||||
|
||||
return label;
|
||||
},
|
||||
|
||||
/** Set selected customer shipping address */
|
||||
selectAddress: function () {
|
||||
selectShippingAddressAction(this.address());
|
||||
checkoutData.setSelectedShippingAddress(this.address().getKey());
|
||||
|
||||
// 美国之外不配送提示
|
||||
var selectedAddress = $('.shipping-address-item.selected-item .shipping-address-country').text();
|
||||
if(selectedAddress !=='United States'){
|
||||
$('#shipping-address-tip-modal').modal('openModal');
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Edit address.
|
||||
*/
|
||||
editAddress: function () {
|
||||
formPopUpState.isVisible(true);
|
||||
this.showPopup();
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Show popup.
|
||||
*/
|
||||
showPopup: function () {
|
||||
$('[data-open-modal="opc-new-shipping-address"]').trigger('click');
|
||||
}
|
||||
});
|
||||
});
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
define([
|
||||
'underscore',
|
||||
'ko',
|
||||
'mageUtils',
|
||||
'uiComponent',
|
||||
'uiLayout',
|
||||
'Magento_Customer/js/model/address-list'
|
||||
], function (_, ko, utils, Component, layout, addressList) {
|
||||
'use strict';
|
||||
|
||||
var defaultRendererTemplate = {
|
||||
parent: '${ $.$data.parentName }',
|
||||
name: '${ $.$data.name }',
|
||||
component: 'Magento_Checkout/js/view/shipping-address/address-renderer/default',
|
||||
provider: 'checkoutProvider'
|
||||
};
|
||||
|
||||
return Component.extend({
|
||||
defaults: {
|
||||
template: 'Magento_Checkout/shipping-address/list',
|
||||
visible: addressList().length > 0,
|
||||
rendererTemplates: []
|
||||
},
|
||||
|
||||
/** @inheritdoc */
|
||||
initialize: function () {
|
||||
this._super()
|
||||
.initChildren();
|
||||
|
||||
addressList.subscribe(function (changes) {
|
||||
var self = this;
|
||||
|
||||
changes.forEach(function (change) {
|
||||
if (change.status === 'added') {
|
||||
self.createRendererComponent(change.value, change.index);
|
||||
}
|
||||
});
|
||||
},
|
||||
this,
|
||||
'arrayChange'
|
||||
);
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
/** @inheritdoc */
|
||||
initConfig: function () {
|
||||
this._super();
|
||||
// the list of child components that are responsible for address rendering
|
||||
this.rendererComponents = [];
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
/** @inheritdoc */
|
||||
initChildren: function () {
|
||||
_.each(addressList(), this.createRendererComponent, this);
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
* Create new component that will render given address in the address list
|
||||
*
|
||||
* @param {Object} address
|
||||
* @param {*} index
|
||||
*/
|
||||
createRendererComponent: function (address, index) {
|
||||
var rendererTemplate, templateData, rendererComponent;
|
||||
|
||||
if (index in this.rendererComponents) {
|
||||
this.rendererComponents[index].address(address);
|
||||
} else {
|
||||
// rendererTemplates are provided via layout
|
||||
rendererTemplate = address.getType() != undefined && this.rendererTemplates[address.getType()] != undefined ? //eslint-disable-line
|
||||
utils.extend({}, defaultRendererTemplate, this.rendererTemplates[address.getType()]) :
|
||||
defaultRendererTemplate;
|
||||
templateData = {
|
||||
parentName: this.name,
|
||||
name: index
|
||||
};
|
||||
rendererComponent = utils.template(rendererTemplate, templateData);
|
||||
utils.extend(rendererComponent, {
|
||||
address: ko.observable(address)
|
||||
});
|
||||
layout([rendererComponent]);
|
||||
this.rendererComponents[index] = rendererComponent;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,410 @@
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
define([
|
||||
'jquery',
|
||||
'underscore',
|
||||
'Magento_Ui/js/form/form',
|
||||
'ko',
|
||||
'Magento_Customer/js/model/customer',
|
||||
'Magento_Customer/js/model/address-list',
|
||||
'Magento_Checkout/js/model/address-converter',
|
||||
'Magento_Checkout/js/model/quote',
|
||||
'Magento_Checkout/js/action/create-shipping-address',
|
||||
'Magento_Checkout/js/action/select-shipping-address',
|
||||
'Magento_Checkout/js/model/shipping-rates-validator',
|
||||
'Magento_Checkout/js/model/shipping-address/form-popup-state',
|
||||
'Magento_Checkout/js/model/shipping-service',
|
||||
'Magento_Checkout/js/action/select-shipping-method',
|
||||
'Magento_Checkout/js/model/shipping-rate-registry',
|
||||
'Magento_Checkout/js/action/set-shipping-information',
|
||||
'Magento_Checkout/js/model/step-navigator',
|
||||
'Magento_Ui/js/modal/modal',
|
||||
'Magento_Checkout/js/model/checkout-data-resolver',
|
||||
'Magento_Checkout/js/checkout-data',
|
||||
'uiRegistry',
|
||||
'mage/translate',
|
||||
'Magento_Checkout/js/model/shipping-rate-service'
|
||||
], function (
|
||||
$,
|
||||
_,
|
||||
Component,
|
||||
ko,
|
||||
customer,
|
||||
addressList,
|
||||
addressConverter,
|
||||
quote,
|
||||
createShippingAddress,
|
||||
selectShippingAddress,
|
||||
shippingRatesValidator,
|
||||
formPopUpState,
|
||||
shippingService,
|
||||
selectShippingMethodAction,
|
||||
rateRegistry,
|
||||
setShippingInformationAction,
|
||||
stepNavigator,
|
||||
modal,
|
||||
checkoutDataResolver,
|
||||
checkoutData,
|
||||
registry,
|
||||
$t
|
||||
) {
|
||||
'use strict';
|
||||
|
||||
var popUp = null;
|
||||
|
||||
return Component.extend({
|
||||
defaults: {
|
||||
template: 'Magento_Checkout/shipping',
|
||||
shippingFormTemplate: 'Magento_Checkout/shipping-address/form',
|
||||
shippingMethodListTemplate: 'Magento_Checkout/shipping-address/shipping-method-list',
|
||||
shippingMethodItemTemplate: 'Magento_Checkout/shipping-address/shipping-method-item',
|
||||
imports: {
|
||||
countryOptions: '${ $.parentName }.shippingAddress.shipping-address-fieldset.country_id:indexedOptions'
|
||||
}
|
||||
},
|
||||
visible: ko.observable(!quote.isVirtual()),
|
||||
errorValidationMessage: ko.observable(false),
|
||||
isCustomerLoggedIn: customer.isLoggedIn,
|
||||
isFormPopUpVisible: formPopUpState.isVisible,
|
||||
isFormInline: addressList().length === 0,
|
||||
isNewAddressAdded: ko.observable(false),
|
||||
saveInAddressBook: 1,
|
||||
quoteIsVirtual: quote.isVirtual(),
|
||||
|
||||
/**
|
||||
* @return {exports}
|
||||
*/
|
||||
initialize: function () {
|
||||
var self = this,
|
||||
hasNewAddress,
|
||||
fieldsetName = 'checkout.steps.shipping-step.shippingAddress.shipping-address-fieldset';
|
||||
|
||||
this._super();
|
||||
|
||||
if (!quote.isVirtual()) {
|
||||
stepNavigator.registerStep(
|
||||
'shipping',
|
||||
'',
|
||||
$t('Shipping'),
|
||||
this.visible, _.bind(this.navigate, this),
|
||||
this.sortOrder
|
||||
);
|
||||
}
|
||||
checkoutDataResolver.resolveShippingAddress();
|
||||
|
||||
hasNewAddress = addressList.some(function (address) {
|
||||
return address.getType() == 'new-customer-address'; //eslint-disable-line eqeqeq
|
||||
});
|
||||
|
||||
this.isNewAddressAdded(hasNewAddress);
|
||||
|
||||
this.isFormPopUpVisible.subscribe(function (value) {
|
||||
if (value) {
|
||||
self.getPopUp().openModal();
|
||||
}
|
||||
});
|
||||
|
||||
quote.shippingMethod.subscribe(function () {
|
||||
self.errorValidationMessage(false);
|
||||
});
|
||||
|
||||
registry.async('checkoutProvider')(function (checkoutProvider) {
|
||||
var shippingAddressData = checkoutData.getShippingAddressFromData();
|
||||
|
||||
if (shippingAddressData) {
|
||||
checkoutProvider.set(
|
||||
'shippingAddress',
|
||||
$.extend(true, {}, checkoutProvider.get('shippingAddress'), shippingAddressData)
|
||||
);
|
||||
}
|
||||
checkoutProvider.on('shippingAddress', function (shippingAddrsData, changes) {
|
||||
var isStreetAddressDeleted, isStreetAddressNotEmpty;
|
||||
|
||||
/**
|
||||
* In last modifying operation street address was deleted.
|
||||
* @return {Boolean}
|
||||
*/
|
||||
isStreetAddressDeleted = function () {
|
||||
var change;
|
||||
|
||||
if (!changes || changes.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
change = changes.pop();
|
||||
|
||||
if (_.isUndefined(change.value) || _.isUndefined(change.oldValue)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!change.path.startsWith('shippingAddress.street')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return change.value.length === 0 && change.oldValue.length > 0;
|
||||
};
|
||||
|
||||
isStreetAddressNotEmpty = shippingAddrsData.street && !_.isEmpty(shippingAddrsData.street[0]);
|
||||
|
||||
if (isStreetAddressNotEmpty || isStreetAddressDeleted()) {
|
||||
checkoutData.setShippingAddressFromData(shippingAddrsData);
|
||||
}
|
||||
});
|
||||
shippingRatesValidator.initFields(fieldsetName);
|
||||
});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
* Navigator change hash handler.
|
||||
*
|
||||
* @param {Object} step - navigation step
|
||||
*/
|
||||
navigate: function (step) {
|
||||
step && step.isVisible(true);
|
||||
},
|
||||
|
||||
/**
|
||||
* @return {*}
|
||||
*/
|
||||
getPopUp: function () {
|
||||
var self = this,
|
||||
buttons;
|
||||
|
||||
if (!popUp) {
|
||||
buttons = this.popUpForm.options.buttons;
|
||||
this.popUpForm.options.buttons = [
|
||||
{
|
||||
text: buttons.save.text ? buttons.save.text : $t('Save Address'),
|
||||
class: buttons.save.class ? buttons.save.class : 'action primary action-save-address',
|
||||
click: self.saveNewAddress.bind(self)
|
||||
},
|
||||
{
|
||||
text: buttons.cancel.text ? buttons.cancel.text : $t('Cancel'),
|
||||
class: buttons.cancel.class ? buttons.cancel.class : 'action secondary action-hide-popup',
|
||||
|
||||
/** @inheritdoc */
|
||||
click: this.onClosePopUp.bind(this)
|
||||
}
|
||||
];
|
||||
|
||||
/** @inheritdoc */
|
||||
this.popUpForm.options.closed = function () {
|
||||
self.isFormPopUpVisible(false);
|
||||
};
|
||||
|
||||
this.popUpForm.options.modalCloseBtnHandler = this.onClosePopUp.bind(this);
|
||||
this.popUpForm.options.keyEventHandlers = {
|
||||
escapeKey: this.onClosePopUp.bind(this)
|
||||
};
|
||||
|
||||
/** @inheritdoc */
|
||||
this.popUpForm.options.opened = function () {
|
||||
// Store temporary address for revert action in case when user click cancel action
|
||||
self.temporaryAddress = $.extend(true, {}, checkoutData.getShippingAddressFromData());
|
||||
};
|
||||
popUp = modal(this.popUpForm.options, $(this.popUpForm.element));
|
||||
}
|
||||
|
||||
return popUp;
|
||||
},
|
||||
|
||||
/**
|
||||
* Revert address and close modal.
|
||||
*/
|
||||
onClosePopUp: function () {
|
||||
checkoutData.setShippingAddressFromData($.extend(true, {}, this.temporaryAddress));
|
||||
this.getPopUp().closeModal();
|
||||
},
|
||||
|
||||
/**
|
||||
* Show address form popup
|
||||
*/
|
||||
showFormPopUp: function () {
|
||||
this.isFormPopUpVisible(true);
|
||||
},
|
||||
|
||||
/**
|
||||
* Save new shipping address
|
||||
*/
|
||||
saveNewAddress: function () {
|
||||
var addressData,
|
||||
newShippingAddress;
|
||||
|
||||
this.source.set('params.invalid', false);
|
||||
this.triggerShippingDataValidateEvent();
|
||||
|
||||
if (!this.source.get('params.invalid')) {
|
||||
addressData = this.source.get('shippingAddress');
|
||||
// if user clicked the checkbox, its value is true or false. Need to convert.
|
||||
addressData['save_in_address_book'] = this.saveInAddressBook ? 1 : 0;
|
||||
|
||||
// New address must be selected as a shipping address
|
||||
newShippingAddress = createShippingAddress(addressData);
|
||||
selectShippingAddress(newShippingAddress);
|
||||
checkoutData.setSelectedShippingAddress(newShippingAddress.getKey());
|
||||
checkoutData.setNewCustomerShippingAddress($.extend(true, {}, addressData));
|
||||
this.getPopUp().closeModal();
|
||||
this.isNewAddressAdded(true);
|
||||
|
||||
// 美国之外不配送提示
|
||||
var selectedAddress = $('.shipping-address-item.selected-item .shipping-address-country').text();
|
||||
if(selectedAddress !=='United States'){
|
||||
$('#shipping-address-tip-modal').modal('openModal');
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Shipping Method View
|
||||
*/
|
||||
rates: shippingService.getShippingRates(),
|
||||
isLoading: shippingService.isLoading,
|
||||
isSelected: ko.computed(function () {
|
||||
return quote.shippingMethod() ?
|
||||
quote.shippingMethod()['carrier_code'] + '_' + quote.shippingMethod()['method_code'] :
|
||||
null;
|
||||
}),
|
||||
|
||||
/**
|
||||
* @param {Object} shippingMethod
|
||||
* @return {Boolean}
|
||||
*/
|
||||
selectShippingMethod: function (shippingMethod) {
|
||||
selectShippingMethodAction(shippingMethod);
|
||||
checkoutData.setSelectedShippingRate(shippingMethod['carrier_code'] + '_' + shippingMethod['method_code']);
|
||||
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
* Set shipping information handler
|
||||
*/
|
||||
setShippingInformation: function () {
|
||||
if (this.validateShippingInformation()) {
|
||||
quote.billingAddress(null);
|
||||
checkoutDataResolver.resolveBillingAddress();
|
||||
registry.async('checkoutProvider')(function (checkoutProvider) {
|
||||
var shippingAddressData = checkoutData.getShippingAddressFromData();
|
||||
|
||||
if (shippingAddressData) {
|
||||
checkoutProvider.set(
|
||||
'shippingAddress',
|
||||
$.extend(true, {}, checkoutProvider.get('shippingAddress'), shippingAddressData)
|
||||
);
|
||||
}
|
||||
});
|
||||
setShippingInformationAction().done(
|
||||
function () {
|
||||
stepNavigator.next();
|
||||
}
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @return {Boolean}
|
||||
*/
|
||||
validateShippingInformation: function () {
|
||||
var shippingAddress,
|
||||
addressData,
|
||||
loginFormSelector = 'form[data-role=email-with-possible-login]',
|
||||
emailValidationResult = customer.isLoggedIn(),
|
||||
field,
|
||||
option = _.isObject(this.countryOptions) && this.countryOptions[quote.shippingAddress().countryId],
|
||||
messageContainer = registry.get('checkout.errors').messageContainer;
|
||||
|
||||
if (!quote.shippingMethod()) {
|
||||
this.errorValidationMessage(
|
||||
$t('The shipping method is missing. Select the shipping method and try again.')
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!customer.isLoggedIn()) {
|
||||
$(loginFormSelector).validation();
|
||||
emailValidationResult = Boolean($(loginFormSelector + ' input[name=username]').valid());
|
||||
}
|
||||
|
||||
if (this.isFormInline) {
|
||||
this.source.set('params.invalid', false);
|
||||
this.triggerShippingDataValidateEvent();
|
||||
|
||||
if (!quote.shippingMethod()['method_code']) {
|
||||
this.errorValidationMessage(
|
||||
$t('The shipping method is missing. Select the shipping method and try again.')
|
||||
);
|
||||
}
|
||||
|
||||
if (emailValidationResult &&
|
||||
this.source.get('params.invalid') ||
|
||||
!quote.shippingMethod()['method_code'] ||
|
||||
!quote.shippingMethod()['carrier_code']
|
||||
) {
|
||||
this.focusInvalid();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
shippingAddress = quote.shippingAddress();
|
||||
addressData = addressConverter.formAddressDataToQuoteAddress(
|
||||
this.source.get('shippingAddress')
|
||||
);
|
||||
|
||||
//Copy form data to quote shipping address object
|
||||
for (field in addressData) {
|
||||
if (addressData.hasOwnProperty(field) && //eslint-disable-line max-depth
|
||||
shippingAddress.hasOwnProperty(field) &&
|
||||
typeof addressData[field] != 'function' &&
|
||||
_.isEqual(shippingAddress[field], addressData[field])
|
||||
) {
|
||||
shippingAddress[field] = addressData[field];
|
||||
} else if (typeof addressData[field] != 'function' &&
|
||||
!_.isEqual(shippingAddress[field], addressData[field])) {
|
||||
shippingAddress = addressData;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (customer.isLoggedIn()) {
|
||||
shippingAddress['save_in_address_book'] = 1;
|
||||
}
|
||||
selectShippingAddress(shippingAddress);
|
||||
} else if (customer.isLoggedIn() &&
|
||||
option &&
|
||||
option['is_region_required'] &&
|
||||
!quote.shippingAddress().region
|
||||
) {
|
||||
messageContainer.addErrorMessage({
|
||||
message: $t('Please specify a regionId in shipping address.')
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!emailValidationResult) {
|
||||
$(loginFormSelector + ' input[name=username]').trigger('focus');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
* Trigger Shipping data Validate Event.
|
||||
*/
|
||||
triggerShippingDataValidateEvent: function () {
|
||||
this.source.trigger('shippingAddress.data.validate');
|
||||
|
||||
if (this.source.get('shippingAddress.custom_attributes')) {
|
||||
this.source.trigger('shippingAddress.custom_attributes.data.validate');
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user