Commit 82e8a20e by dhn

购物车优化

parent 64dfa112
...@@ -119,4 +119,10 @@ class Shipping extends \Magento\Checkout\Block\Cart\AbstractCart ...@@ -119,4 +119,10 @@ class Shipping extends \Magento\Checkout\Block\Cart\AbstractCart
{ {
return $this->jsonHexTagSerializer->serialize($this->getCheckoutConfig()); return $this->jsonHexTagSerializer->serialize($this->getCheckoutConfig());
} }
public function getCountryHtmlSelect($defValue = null, $name = 'country_id', $id = 'country', $title = 'Country') {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Directory\Block\Data $block */
$block = $objectManager->get(\Magento\Directory\Block\Data::class);
return $block->getCountryHtmlSelect($defValue, $name, $id, $title);
}
} }
...@@ -7,32 +7,20 @@ ...@@ -7,32 +7,20 @@
?> ?>
<?php /** @var $block \Magento\Checkout\Block\Cart\Shipping */ ?> <?php /** @var $block \Magento\Checkout\Block\Cart\Shipping */ ?>
<?php /** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */ ?> <?php /** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */ ?>
<div id="block-shipping" <div id="block-shipping" class="block shipping">
class="block shipping" <div id="block-summary" style="display: none;">
data-mage-init='{"collapsible":{"openedState": "active", "saveState": true}}' <form method="post" id="shipping-zip-form">
> <fieldset class="fieldset estimate">
<div class="title" data-role="title"> <div class="field" name="shippingAddress.country_id">
<strong id="block-shipping-heading" role="heading" aria-level="2"> <label class="label" for="shipping_country_id">
<?= $block->getQuote()->isVirtual() <span><?= __('Country')?></span>
? $block->escapeHtml(__('Estimate Tax')) </label>
: $block->escapeHtml(__('Estimate Shipping and Tax')) <div class="control">
?> <?= $block->getCountryHtmlSelect(null, 'country_id', 'shipping_country_id')?>
</strong> </div>
</div> </div>
<div id="block-summary" </fieldset>
data-bind="scope:'block-summary'" </form>
class="content"
data-role="content"
aria-labelledby="block-shipping-heading"
>
<!-- ko template: getTemplate() --><!-- /ko -->
<script type="text/x-magento-init">
{
"#block-summary": {
"Magento_Ui/js/core/app": <?= /* @noEscape */ $block->getJsLayout() ?>
}
}
</script>
<?php $serializedCheckoutConfig = /* @noEscape */ $block->getSerializedCheckoutConfig(); <?php $serializedCheckoutConfig = /* @noEscape */ $block->getSerializedCheckoutConfig();
$scriptString = <<<script $scriptString = <<<script
......
...@@ -29,6 +29,7 @@ define([ ...@@ -29,6 +29,7 @@ define([
billingAddress = ko.observable(null), billingAddress = ko.observable(null),
shippingAddress = ko.observable(null), shippingAddress = ko.observable(null),
shippingMethod = ko.observable(null), shippingMethod = ko.observable(null),
shippingSaveMethod = ko.observable(null),
paymentMethod = ko.observable(null), paymentMethod = ko.observable(null),
paymentMethodlist = ko.observable(null), paymentMethodlist = ko.observable(null),
quoteData = window.checkoutConfig.quoteData, quoteData = window.checkoutConfig.quoteData,
...@@ -43,6 +44,7 @@ define([ ...@@ -43,6 +44,7 @@ define([
totals: totals, totals: totals,
shippingAddress: shippingAddress, shippingAddress: shippingAddress,
shippingMethod: shippingMethod, shippingMethod: shippingMethod,
shippingSaveMethod:shippingSaveMethod,
billingAddress: billingAddress, billingAddress: billingAddress,
paymentMethod: paymentMethod, paymentMethod: paymentMethod,
paymentMethodlist:paymentMethodlist, paymentMethodlist:paymentMethodlist,
...@@ -113,12 +115,20 @@ define([ ...@@ -113,12 +115,20 @@ define([
getPaymentMethod: function () { getPaymentMethod: function () {
return paymentMethod; return paymentMethod;
}, },
setShippingSaveMethod: function (Method) {
shippingSaveMethod(Method);
},
getShippingSaveMethod: function () {
return shippingSaveMethod;
},
setPaymentMethodList: function (MethodList) { setPaymentMethodList: function (MethodList) {
paymentMethodlist(MethodList); paymentMethodlist(MethodList);
}, },
getPaymentMethodList: function () { getPaymentMethodList: function () {
return paymentMethodlist; return paymentMethodlist;
}, },
/** /**
* @return {*} * @return {*}
*/ */
......
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([
'Magento_Checkout/js/model/resource-url-manager',
'Magento_Checkout/js/model/quote',
'mage/storage',
'Magento_Checkout/js/model/shipping-service',
'Magento_Checkout/js/model/error-processor',
'Magento_Checkout/js/action/select-shipping-address',
'Magento_Checkout/js/model/address-converter',
'Magento_Checkout/js/view/cart/shipping-estimation'
], function (resourceUrlManager, quote, storage, shippingService, errorProcessor,selectShippingAddress,addressConverter) {
'use strict';
return {
/**
* Get shipping rates for specified address.
* @param {Object} address
*/
getRates: function (address) {
var cache, serviceUrl, payload;
shippingService.isLoading(true);
serviceUrl = resourceUrlManager.getUrlForEstimationShippingMethodsForNewAddress(quote);
payload = JSON.stringify({
address: {
'street': address.street,
'city': address.city,
'region_id': address.regionId,
'region': address.region,
'country_id': address.countryId,
'postcode': address.postcode,
'email': address.email,
'customer_id': address.customerId,
'firstname': address.firstname,
'lastname': address.lastname,
'middlename': address.middlename,
'prefix': address.prefix,
'suffix': address.suffix,
'vat_id': address.vatId,
'company': address.company,
'telephone': address.telephone,
'fax': address.fax,
'custom_attributes': address.customAttributes,
'save_in_address_book': address.saveInAddressBook
}
}
);
storage.post(
serviceUrl, payload, false
).done(function (result) {
var new_sh = [];
new_sh.push(result[0]);
if(result.length > 1)
{
quote.setShippingSaveMethod(result[1]);
}
selectShippingAddress(addressConverter.formAddressDataToQuoteAddress(address));
//shippingService.setShippingRates(new_sh);
}).fail(function (response) {
shippingService.setShippingRates([]);
errorProcessor.process(response);
}).always(function () {
shippingService.isLoading(false);
});
}
};
});
...@@ -2,26 +2,12 @@ define([ ...@@ -2,26 +2,12 @@ define([
'uiComponent', 'uiComponent',
'uiRegistry', 'uiRegistry',
'underscore', 'underscore',
'Magento_Checkout/js/model/quote', 'Magento_Checkout/js/model/shipping-rate-processor/get-address'
'Magento_Checkout/js/action/select-billing-address',
'Magento_Checkout/js/model/payment-service',
'Magento_Checkout/js/model/totals',
'Magento_Checkout/js/action/set-shipping-information',
'Magento_Checkout/js/model/shipping-service',
'Magento_Customer/js/model/customer',
'Magento_Checkout/js/model/update-item-service'
], function ( ], function (
Component, Component,
registry, registry,
_, _,
quote, getAddress
selectBillingAddress,
paymentService,
totalsService,
setShippingInformationAction,
shippingService,
customer,
updateItemService
) { ) {
'use strict'; 'use strict';
...@@ -30,48 +16,10 @@ define([ ...@@ -30,48 +16,10 @@ define([
/** @inheritdoc */ /** @inheritdoc */
initialize: function () { initialize: function () {
this._super(); this._super();
var self = this; var address={};
quote.shippingMethod.subscribe(function (method) { address.countryId = window.checkoutConfig.defaultCountryId;
if (method && !updateItemService.hasUpdateResult()) { getAddress.getRates(address);
var shippingRates = shippingService.getShippingRates();
var availableRate = _.find(shippingRates(), function (rate) {
if (rate['method_code'] === null && method['method_code'] === null) {
return false;
}
return rate['carrier_code'] + '_' + rate['method_code'] === method['carrier_code'] + '_' + method['method_code'];
});
if (availableRate) {
if (self._isAddressSameAsShipping()) {
selectBillingAddress(quote.shippingAddress());
}
paymentService.isLoading(true);
totalsService.isLoading(true);
setShippingInformationAction().done(
function () {
paymentService.isLoading(false);
totalsService.isLoading(false);
}
);
}
}
}, this);
if (!customer.isLoggedIn()) {
quote.shippingAddress.subscribe(function (address) {
if (_.isUndefined(address.street) || address.street.length == 0) {
address.street = ["", ""];
}
}, this);
}
},
/**
* @returns {*}
*/
_isAddressSameAsShipping: function () {
return true;
//return registry.get('checkout.steps.billing-step.payment.payments-list.billing-address-form-shared').isAddressSameAsShipping();
} }
}); });
}); });
...@@ -29,6 +29,21 @@ define([ ...@@ -29,6 +29,21 @@ define([
if (!this.isCalculated()) { if (!this.isCalculated()) {
return ''; return '';
} }
var price;
if (!this.isCalculated()) {
return this.notCalculatedMessage;
}
price = this.totals()['shipping_amount'];
if(price == 0)
{
if(quote.shippingSaveMethod())
{
return quote.shippingSaveMethod()['method_title'];
}
}
shippingMethod = quote.shippingMethod(); shippingMethod = quote.shippingMethod();
if (!_.isArray(shippingMethod) && !_.isObject(shippingMethod)) { if (!_.isArray(shippingMethod) && !_.isObject(shippingMethod)) {
...@@ -57,7 +72,7 @@ define([ ...@@ -57,7 +72,7 @@ define([
/** /**
* @return {*} * @return {*}
getValue: function () { getValue: function () {
var price; var price;
if (!this.isCalculated()) { if (!this.isCalculated()) {
...@@ -78,7 +93,10 @@ define([ ...@@ -78,7 +93,10 @@ define([
price = this.totals()['shipping_amount']; price = this.totals()['shipping_amount'];
if(price == 0) if(price == 0)
{ {
return this.getFormattedPrice(6.99); if(quote.shippingSaveMethod())
{
return this.getFormattedPrice(quote.shippingSaveMethod()['amount']);
}
} }
return this.getFormattedPrice(price); return this.getFormattedPrice(price);
}, },
...@@ -91,7 +109,11 @@ define([ ...@@ -91,7 +109,11 @@ define([
price = this.totals()['shipping_amount']; price = this.totals()['shipping_amount'];
if(price == 0) if(price == 0)
{ {
return '-'+this.getFormattedPrice(6.99); if(quote.shippingSaveMethod())
{
return '-'+this.getFormattedPrice(quote.shippingSaveMethod()['amount']);
}
return 0;
} }
}, },
......
...@@ -51,7 +51,7 @@ define([ ...@@ -51,7 +51,7 @@ define([
}, },
isSaveShowed: function () { isSaveShowed: function () {
var price = this.totals()['shipping_amount']; var price = this.totals()['shipping_amount'];
if(price) if(price != 0)
{ {
return false; return false;
} }
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
<referenceContainer name="content"> <referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="checkout.cart.footer" template="Magento_Checkout::footer.phtml" /> <block class="Magento\Framework\View\Element\Template" name="checkout.cart.footer" template="Magento_Checkout::footer.phtml" />
</referenceContainer> </referenceContainer>
<!--
<referenceBlock name="checkout.cart.totals"> <referenceBlock name="checkout.cart.totals">
<arguments> <arguments>
<argument name="jsLayout" xsi:type="array"> <argument name="jsLayout" xsi:type="array">
...@@ -53,6 +53,6 @@ ...@@ -53,6 +53,6 @@
</argument> </argument>
</arguments> </arguments>
</referenceBlock> </referenceBlock>
-->
</body> </body>
</page> </page>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment