I created an event :
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch_checkout_cart_index">
<observer name="set-default-shipping-method" instance="Vendor\Checkout\Observer\SetDefaultShippingMethod"/>
</event>
</config>
And in my event :
<?php
namespace Vendor\Checkout\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\Quote\Address;
use \Magento\Checkout\Model\Session as CheckoutSession;
class SetDefaultShippingMethod implements ObserverInterface
{
/** @var CheckoutSession */
protected $checkoutSession;
/**
* @param CheckoutSession $checkoutSession
*/
public function __construct(CheckoutSession $checkoutSession) {
$this->checkoutSession = $checkoutSession;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$quote = $this->checkoutSession->getQuote();
$shippingAddress = $quote->getShippingAddress();
if(!$shippingAddress->getCountryId()){
$shippingAddress->setCountryId('FR'); // Default country for calculate shipping rates if no one given
}
if(!$shippingAddress->getShippingMethod()){
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('mondialrelay_pickup'); // Default Shipping method
}
}
}
This event is called when the user call the checkout cart index action
To get only applicable shipping methods for a given quote (based on the quote id) you can utilize Mage_Checkout_Model_Cart_Shipping_Api
's method getShippingMethodsList
.
This method expects two (one) params:
It will return an array of available shipping methods looking like this:
array(1) {
[0]=>
array(9) {
["carrier"]=>
string(8) "flatrate"
["carrier_title"]=>
string(9) "Flat Rate"
["code"]=>
string(17) "flatrate_flatrate"
["method"]=>
string(8) "flatrate"
["method_description"]=>
NULL
["price"]=>
string(6) "5.0000"
["error_message"]=>
NULL
["method_title"]=>
string(5) "Fixed"
["carrierName"]=>
string(9) "Flat Rate"
}
Yet, there are two points that need your attention:
You may not display table rates shipping methods if not applicable (set Show Method if Not Applicable
to No
as this may bring up some unwanted results [untested - might not even be necessary!]).
According to your description of the existing shipping methods at one point there is more then one shipping method applicable - namely, when the cart amount is above 80 USD. In this case (depending on the shipping country) Free
AND either Fixed Rate
or Tablerate
is (or may be) applicable (since conditions may be fulfilled for both methods). This in return would provide an array count of 2.
However, you could address this issue by setting up a sort order where the preferred shipping method would be placed before the second/third possible one (this is possible because the sort order is respected in the output of getShippingMethodsList()
already). Hence you would be able to always get the shipping method most relevant to you from array key [0]
of the method's output.
Since the setup of your methods (sort orders and so on) may require a tiny bit of testing and/or fine tuning - and to see it in action - I'd like to invite you to try it out directly in your checkout, i.e. by placing something like this in your frontend/{theme}/{theme}/template/checkout/onepage/shipping_method/available.phtml
<?php $quoteId = Mage::getSingleton('checkout/session')->getQuote()->getId(); ?>
<?php $storeId = Mage::app()->getStore()->getStoreId(); ?>
<pre>
<?php var_dump(Mage::getModel('checkout/cart_shipping_api')->getShippingMethodsList($quoteId,$storeId)); ?>
</pre>
Best Answer
Use owebia shipping extension. install from
https://marketplace.magento.com/owebia-owebia-shipping-2.html
https://en.store.owebia.com/magento2-module-advanced-shipping.html
Here's documentation for configuration.
https://owebia.com/os2/en/doc
condition 1:-For any zipcode,if order value $200 after it's free shiping_method1. and if value is less than $200 ( shipping_method2).
} }
this is how you create multiple shipping methods with multiple conditions.
Hope it help you ! if any issue I love to Help you further..