I want to set the shipping method to each cart to free shipping without choosing the shipping method, so when customer doing checkout, the shipping method will set by default to free shipping without choosing it. How can i do it?
Magento 2 – How to Set Shipping Method to All Cart Items
event-observerfree-shippingmagento2PHPshipping-methods
Related Solutions
I used this method just so I could change the css
on the <select>
element but it makes it easier to "filter" countries and what not as well, which could help you.
Go to /app/design/frontend/THEME_NAME/default/template/checkout/cart/shipping.phtml
Try replacing:
<?php echo Mage::getBlockSingleton('directory/data')->getCountryHtmlSelect($this->getEstimateCountryId()) ?>
With this:
<?php $_countries = Mage::getResourceModel('directory/country_collection')->loadByStore()->toOptionArray(false) ?>
<?php if (count($_countries) > 0): ?>
<select name="country_id" id="country" class="validate-select">
<option value="GB">United Kingdom</option>
<?php foreach($_countries as $_country): ?>
if($_country['value']=="GB")
{
continue
}
else
{
<option value="<?php echo $_country['value'] ?>">
<?php echo $_country['label'] ?>
</option>
}
<?php endforeach; ?>
</select>
<?php endif; ?>
One approach can be filtering of shipping methods based on sub-total. This way you can activate or deactivate required shipping methods.
For filtering shipping methods:
1> Rewrite the shipping model class: Mage_Shipping_Model_Shipping
File: app/code/local/MagePsycho/Shipmentfilter/etc/config.xml
Code:
...
<global>
...
<models>
<shipping>
<rewrite>
<shipping>MagePsycho_Shipmentfilter_Model_Shipping</shipping>
</rewrite>
</shipping>
</models>
...
</global>
2> Override the method: collectCarrierRates()
File: app/code/local/MagePsycho/Shipmentfilter/Model/Shipping.php
Code:
<?php
/**
* @category MagePsycho
* @package MagePsycho_Shipmentfilter
* @author magepsycho@gmail.com
* @website http://www.magepsycho.com
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class MagePsycho_Shipmentfilter_Model_Shipping extends Mage_Shipping_Model_Shipping
{
public function collectCarrierRates($carrierCode, $request)
{
if (!$this->_checkCarrierAvailability($carrierCode, $request)) {
return $this;
}
return parent::collectCarrierRates($carrierCode, $request);
}
protected function _checkCarrierAvailability($carrierCode, $request = null)
{
$subtotal = Mage::getSingleton('checkout/session')->getQuote()->getSubtotal();
$amountToCheck = 250; //Edit this amount
if ($subtotal > $amountToCheck) {
if($carrierCode == 'flatrate'){ #Hide Flat Rate
return false;
}
}
return true;
}
}
Best Answer
To auto select
free shipping
on checkout page, you can override a javascript fromMagento_Checkout
module. You can achieve this by two way1] By creating your module,
Namespace/Module/registration.php
Namespace/Module/etc/module.xml
then create the
requirejs
to overriding thecheckout-data-resolver
js,for this
Namespace/Module/view/frontend/requirejs-config.js
Now copy
checkout-data-resolver.js
fromMagento_Checkout/view/frontend/web/js/model
in our module with the same pathNamespace_Module/view/frontend/web/js/model
After that, change a condition inside the function
resolveShippingRates
From:
To
Replace
Namespace/Module/
with yourYOUR_NAMESPACE
andYOUR_MODULENAME
php bin/magento module:enable Namespace_Module
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
2] By using theme
Just copy
checkout-data-resolver.js
into your custom theme but make sure It path should be,app\design\yourvendor\yourthemename\Magento_Checkout\view\frontend\web\js\model
After copying update
checkout-data-resolver.js
withresolveShippingRates
function,After overriding js file into your custom theme, execute below commands,