I want to hide free shipping when purchase order payment is selected and then show free shipping when any other payment method is selected.
2 Answers
the easiest method is to add a afterPlugin to the function \Magento\OfflineShipping\Model\Carrier\Freeshipping::getAllowedMethods
In this function, you will need to list the active payment methods.
\Magento\Payment\Model\PaymentMethodList::getActiveList
If the payment method you want to use to filter free_shipping
is returned as part of the active payment methods, then you may remove free_shipping
from the getAllowedMethods
thanks to this plugin.
In my experience, you are attempting to hide some payment options depending on the shipping method. You don't even need to pay attention to anything for this. Simply follow my lead and you can do this.
Every technique (in a single page check out) posts the strategies that are picked for the following phase. so that you can see the chosen shipping method at the level of the payment method. Print the post items in
app/design/frontend/base/default/template/checkout/onepage/payment/methods.phtml
in this, add below code,
<?php print_r($_POST); ?>
You can now obtain the shipping options you selected in the previous step. And take note of it so that you can directly add a straightforward logic (if else) condition to the same file to hide the payment.
For example, here I want to hide check / money order
payment method if the shipping method is flat. Here the payment method code is checkmo
. you can get payment method code by simply printing that variable like echo $_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 skips the payment method which we don't want to display. That's it.
Note:
shipping_method => flatrate_flatrate
paymet_method => checkmo
Free Shipping
don't show the Purchase Order payment method. Is this work for You?