浏览文章
文章信息
Magento2 默认选中物流方式
13324
要
free shipping
在结帐页面上自动选择,您可以覆盖Magento_Checkout
模块中的javascript 。您可以通过两种方式实现1]通过创建模块,
Namespace/Module/registration.php
<?php\Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Namespace_Module', __DIR__);
Namespace/Module/etc/module.xml
<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Namespace_Module" setup_version="1.0.0"> <sequence> <module name="Magento_Checkout"/> </sequence> </module></config>然后创建
requirejs
来覆盖checkout-data-resolver
js,为了这
Namespace/Module/view/frontend/requirejs-config.js
var config = { map: { '*': { 'Magento_Checkout/js/model/checkout-data-resolver': 'Namespace_Module/js/model/checkout-data-resolver' } }};现在使用相同的路径
checkout-data-resolver.js
从Magento_Checkout/view/frontend/web/js/model
我们的模块中复制Namespace_Module/view/frontend/web/js/model
之后,在函数内部更改条件
resolveShippingRates
从:
............................ if (ratesData.length == 1) { //set shipping rate if we have only one available shipping rate selectShippingMethodAction(ratesData[0]); return; }.............................至
................................. var selectedShippingRate = checkoutData . getSelectedShippingRate(); var availableRate = false; if (selectedShippingRate == null) { // custom code start here for (var i = 0;i < ratesData . length;i++){ if (ratesData[i] . method_code == 'freeshipping') { selectShippingMethodAction(ratesData[i]); return; } } } else { // custom code end here if (ratesData . length == 1) { //set shipping rate if we have only one available shipping rate selectShippingMethodAction(ratesData[0]); return; } }.....................................更换
Namespace/Module/
你的YOUR_NAMESPACE
和YOUR_MODULENAME
- 激活模块:
php bin/magento module:enable Namespace_Module
- 运行安装程序升级:
php bin/magento setup:upgrade
- 进行静态部署:
php bin/magento setup:static-content:deploy
2]通过使用主题
只需复制
checkout-data-resolver.js
到您的自定义主题中,但请确保它的路径应该是,
app\design\yourvendor\yourthemename\Magento_Checkout\view\frontend\web\js\model
复制
checkout-data-resolver.js
具有resolveShippingRates
功能的更新后,resolveShippingRates: function (ratesData) { var selectedShippingRate = checkoutData . getSelectedShippingRate(); var availableRate = false; if (selectedShippingRate == null) { // custom code start here for (var i = 0;i < ratesData . length;i++){ if (ratesData[i] . method_code == 'freeshipping') { selectShippingMethodAction(ratesData[i]); return; } } } else { // custom code end here if (ratesData . length == 1) { //set shipping rate if we have only one available shipping rate selectShippingMethodAction(ratesData[0]); return; } } if (quote.shippingMethod()) { availableRate = _.find(ratesData, function (rate) { return rate.carrier_code == quote.shippingMethod().carrier_code && rate.method_code == quote.shippingMethod().method_code; }); } if (!availableRate && selectedShippingRate) { availableRate = _.find(ratesData, function (rate) { return rate.carrier_code + '_' + rate.method_code === selectedShippingRate; }); } if (!availableRate && window.checkoutConfig.selectedShippingMethod) { availableRate = true; selectShippingMethodAction(window.checkoutConfig.selectedShippingMethod); } //Unset selected shipping method if not available if (!availableRate) { selectShippingMethodAction(null); } else { selectShippingMethodAction(availableRate); } },将js文件覆盖为您的自定义主题后,执行以下命令,
- php bin / magento缓存:刷新
- php bin / magento设置:静态内容:部署