4

I have installed a store pick up extension in our website. This extension showing two option as radio buttons

Collect in Store

  1. Yes
  2. No

So if we select 'NO', then in shipping method,

There is an additional shipping method showing like below.

Store pickup 0.00

Can we hide this option from the list if the extension installed is enabled?

In which file we can hide specific shipping method based on condition.

Can anyone help me with the issue?

I need to hide the specific shipping method from the list of options.

  • check it's template file , it should be there – fmsthird Apr 26 at 10:51
  • @magefms, No its not there in the template file, can we hide in an other way,using the condition – jafar pinjar Apr 26 at 10:55
  • can you share link for that extension? – fmsthird Apr 26 at 10:57
  • @magefms, here s the link wyomind.com/store-pickup-magento.html – jafar pinjar Apr 26 at 11:00
  • @magefms, the shipping method begins with this word "pickupatstore_", can we hide this by using plugin or any other methods – jafar pinjar Apr 26 at 11:26
5

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!!

  • Hiding with CSS is really not the right way. Technically, the shipping method is still selectable and you can run into a problem with this. You can use an observer or plugin to truly remove it based on your condition or at a minimum you can use a javascript mixin to remove it from the list before it reaches the html template. – Shawn Abramson May 3 at 11:37
  • @ShawnAbramson, yes i tried that way, that will not hide the shipping method instead it will not render, if not render even pick up store also adding price, so i used above method – jafar pinjar May 6 at 6:30
0

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.

  • hi @Yash, this code will just hide the method from list is it? – jafar pinjar Apr 29 at 6:32
  • yes, it wont show this method when checkout. – Yash Shah Apr 29 at 6:34
  • But will it apply when pick up store is selected? – jafar pinjar Apr 29 at 6:34
  • I am trying with ko binding like below, <!--ko ifnot: method.method_code == pickupatstore_1 --> in methods.html file – jafar pinjar Apr 29 at 6:35
  • No it wont apply as it will not show in frontend, so user will not be able to select it. – Yash Shah Apr 29 at 6:35
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.