Remove specific shipping method based on condition in magento 2
I am able to resolve this error like below.
Added id for each tr that is, each shipping rates.
<tr class="row" data-bind="attr: {
'id': 'shippingval_' + method.method_code}
,click: $parent.selectShippingMethod" >
Then using css, hidden the specific shipping method that is "pickupstore" in my case.
Hope this may help for others.
Cheers!!
In order to dynamically disable any given shipping method, we need to create 2 plugins. One will be in charge of shipping method validation( Enable or Disable ), Other one for filtering out disabled shipping methods.
Create these files in a custom module.
etc/di.xml
<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Shipping\Model\Rate\Result">
<plugin name="bytepattern_customshipping_update_rate_result" type="Bytepattern\Customshipping\Plugin\ShippingMethod" sortOrder="500" />
<plugin name="bytepattern_customshipping_update_disabled_or_enabled_rates" type="Bytepattern\Customshipping\Plugin\GetAllRates" sortOrder="600" />
</type></config>
Plugin/ShippingMethod.php
<?php
namespace Bytepattern\Customshipping\Plugin;
class ShippingMethod {
public $__code = array();
public function beforeAppend($subject, $result) {
if (!$result instanceof \Magento\Quote\Model\Quote\Address\RateResult\Method) {
return [$result];
}
$this->getShipCode($result);
if ($this->isMethodRestricted($result)) {
try{
$result->setIsDisabled(true);
} catch(Exception $e) {
echo $e->getMessage();
}
}
return [$result];
}
public function getShipCode($shippingModel) {
$this->__code[] = $shippingModel->getCarrier();
}
public function isMethodRestricted($shippingModel) {
$code = $shippingModel->getCarrier();
// Here your desired logic should be implemented!
// Here we are filtering out table rate for free shipping
if(in_array('simpleshipping', $this->__code) && in_array('tablerate', $this->__code) && $code == 'tablerate') {
return true;
}
return false;
} }
Plugin/GetAllRates.php
<?php
namespace Bytepattern\Customshipping\Plugin;
class GetAllRates{
/**
* Disable the marked shipping rates.
*
* NOTE: If you can not see some of the shipping rates, start debugging from here. At first, check 'is_disabled'
* param in the shipping rate object.
*
* @param \Magento\Shipping\Model\Rate\Result $subject
* @param array $result
* @return array
*/
public function afterGetAllRates($subject, $result)
{
foreach ($result as $key => $rate) {
if ($rate->getIsDisabled()) {
unset($result[$key]);
}
}
return $result;
}}
Note: Replace Bytepattern/Customshipping with your Vendor/Module
Create a custom module and create below files.
etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Shipping\Model\Rate\Result">
<plugin name="restrict_shippingmethod" type="[Vendor]\[Module]\Plugin\ShippingMethod" sortOrder="500" />
</type>
</config>
Plugin/ShippingMethod.php
<?php
namespace [Vendor]\[Module]\Plugin;
class ShippingMethod
{
public function beforeAppend($subject, $result)
{
if (!$result instanceof \Magento\Quote\Model\Quote\Address\RateResult\Method) {
return [$result];
}
if ($this->isMethodRestricted($result)) {
$result->setIsDisabled(true);
}
return [$result];
}
public function isMethodRestricted($shippingModel)
{
$code = $shippingModel->getCarrier();
$restrictedMethod = ['replace_method_code'];
if ($restrictedMethod && in_array($code, $restrictedMethod)) {
return true;
}
return false;
}
}
This should work.