Select some shipping methods in your addon for some reason

The idea is that you want to know in your addon some shipping methods that you can do something with them. For example when creating an integration with a carrier company and you want to let admin create the shipment from the backend but your integration has nothing to do with shipping cost, because it is fixed then you don’t have to create a real time processor and all that complex things, just show the button in order view for admin to press and crea the shipment.

In this scenario you just need to know for which shipping method or methods the button will be shown.

A clean approach to do it is to have in your addon settings a multiple checkboxes setting to select the desired shipping methods. First step is to add the setting in your addon.xml

<item id="shipping_methods">
<name>Shipping methods</name>
<type>multiple checkboxes</type>
</item>

In order to get the variants for that you need a function in your func.php. The naming convention is

fn_settings_variants_addons_[YOUR_ADDON_ID]_[YOUR_SETTING_ID]

In our example if the addon ID is my_carrier the function name must be

fn_settings_variants_addons_my_carrier_shipping_methods

The function is simple

function fn_settings_variants_addons_my_carrier_shipping_methods() {
    $out = fn_get_shippings(true);
    return $out;
}

Now in our addon settings we can check the desired shipping methods.

Those shipping IDs will be available via Registry.

$methods = Registry::get('addons.my_carrier.shipping_methods');

Beware the you will get an array with keys the shipping ID and value ‘Y’, something like this

[ '6' => 'Y', '7' => 'Y']

So in order to check if a shipping Id is in the selected ones you can create a function

function my_carrier_is_shipping_selected($shipping_id) {
    $out = false;
    $methods = Registry::get('addons.my_carrier.shipping_methods');
    if (is_array($methods)) {
        $shipping_ids = array_keys($methods);
        $out = in_array($shipping_id, $methods);
    }
    return $out;
}

You can call it in your templates like:

{if  my_carrier_is_shipping_selected($some_shipping_id)} Do my thing {/if}