In magento2 checkout page i'm using free shipping method and i need to hide free shipping method only for non stocking customer group and rest of all customer group i want to display free shipping method.So anyone please guide me in which file i have to put condition.Please check attached image.
Magento2.1.9 Shipping Methods – How to Apply Condition in Checkout Shipping Method
customer-groupmagento2shipping-methods
Related Solutions
I got an answer to this same question on StackOverflow by user Elvarasan. His answer provided me with everything I needed to get this working.
To hide some payment methods based on shipping method you don't need to observe things at all:
Every methods(in one page check out) post the methods which are chosen to the next level. So you can get the shipping method which are chosen, in payment method level. Just print the post things in
app/design/frontend/base/default/template/checkout/onepage/payment/methods.phtml
in this add below one,
<?php print_r($_POST); ?>
So now you can get the shipping methods which are chosen previous step. And note it, so now, you can add just simple logic (if else) condition in same file for hiding payment.
For example here I want hide
check / money order
payment method, if shipping method isflat
. Here the payment method code ischeckmo
. you can get payment method code by simply printing that variable likeecho $_code = $_method->getCode();
in same file. so here just add simple if else ,<?php $methods = $this->getMethods(); $oneMethod = count($methods) <= 1; ?> <?php if (empty($methods)): ?> <dt> <?php echo $this->__('No Payment Methods') ?> </dt> <?php else: foreach ($methods as $_method): echo $_code = $_method->getCode(); if($_POST['shipping_method'] == 'flatrate_flatrate') { if($_code == 'checkmo') { continue; } } ?>
Here,
if($_POST['shipping_method'] == 'flatrate_flatrate') { if($_code == 'checkmo') { continue; } }
checks the shipping method and skip the payment method which we don't want to display. That's it. Please comment here if you have any doubt.
Note:
shipping_method => flatrate_flatrate payment_method => checkmo
You can try below code and if it works for you then you can modify and make it better for fulfilling your requirement.
Code hides free-shipping method for wholesale customer group.
Overwritten class Mage_Shipping_Model_Shipping
class Anshu_ShippingFilter_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) {
$isLoggedIn = Mage::getSingleton('customer/session')->isLoggedIn();
if ($isLoggedIn) {
$groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
if (2 == $groupId && 'freeshipping' == $carrierCode) {
return false;
}
return true;
}
}
}
Best Answer
Hello_World
di.xml
file,app/code/Hello/World/di.xml
Create model file
app/code/Hello/World/Model/Carrier/Freeshipping.php