• Resolved lipisis

    (@lipisis)


    I can’t find a hook to append shipping method form in admin panel so I can add extra input per shipping class. Or at least append something to the end of whole form.

    Does anyone know a hook for it?

    • This topic was modified 3 years ago by lipisis.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • André

    (@chiape)

    Hi @lipisis

    This sounds like a bit complex development topic, however, could you share with us a few more details about what that fields are up to? Is it just for some kind of admin note on an existing shipping method or it should also be used on the checkout page?

    I’m going to leave it open for a bit to see if anyone can chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack.

    We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    My very best!

    Thread Starter lipisis

    (@lipisis)

    Essentially I want to amend prices for shipping classes in a same way this answer does but on that answer you can see it’s hardcoded to only apply to one specific shipping class (TShirt) and threshold of “5”. So I want to have individual thresholds for every shipping class (that’s why I need those extra inputs).

    With that I would eventually be able to say “Charge £5 for every 3 TShirts + charge £2 for every 5 Jeans” and so on.

    I didn’t quite try it yet, but I think the following code is pretty close to what I need (source)

    
    function shipping_instance_form_add_extra_fields($settings)
    {
        $settings['custom_shipping_id'] = [
            'title' => 'Custom Shipping ID',
            'type' => 'number',
            'description' => '',
        ];
    
        return $settings;
    }
    
    function shipping_instance_form_fields_filters()
    {
        $shipping_methods = WC()->shipping->get_shipping_methods();
        foreach ($shipping_methods as $shipping_method) {
            add_filter('woocommerce_shipping_instance_form_fields_' . $shipping_method->id, 'shipping_instance_form_add_extra_fields');
        }
    }
    
    add_action('woocommerce_init', 'shipping_instance_form_fields_filters');
    
    • This reply was modified 3 years ago by lipisis.
    Mirko P.

    (@rainfallnixfig)

    Hi @lipisis,

    I think the following code is pretty close to what I need

    Glad to hear it 👍 – if you do require more help with the actual coding and do not get much input in the above channels for developers, we’d recommend consulting one of the customization experts listed at https://woocommerce.com/customizations/.

    Cheers.

    Thread Starter lipisis

    (@lipisis)

    See my previous answer if you want to add custom fields to the end of the form. If you want to add them for each shipping class, I used the following:

    
    function shipping_instance_form_add_extra_fields($settings)
    {
        $new_settings = [];
        foreach($settings as $key => $setting){
            $new_settings[$key] = $setting;
            if(str_starts_with($key, 'class_cost_')){
                $new_settings[str_replace('class_cost_', 'class_pack_', $key)] = [
                    'title' => str_replace('shipping class cost', 'units per package', $setting['title']),
                    'type' => 'number',
                    'description' => '',
                ];
            }
        }
        return $new_settings;
    }
    
    function shipping_instance_form_fields_filters()
    {
        $shipping_methods = WC()->shipping->get_shipping_methods();
        foreach ($shipping_methods as $shipping_method) {
            add_filter('woocommerce_shipping_instance_form_fields_' . $shipping_method->id, 'shipping_instance_form_add_extra_fields');
        }
    }
    
    add_action('woocommerce_init', 'shipping_instance_form_fields_filters');
    

    You don’t need to do any saving. It will be automatically saved.

    Later, if you have WC_Shipping_Method instance, you can retrieve your custom fields with the following:
    $method->get_instance_option('class_pack_'.$class_id);

    Hi @lipisis!

    That’s great! Thank you for sharing it with the community; it is appreciated ??

    If you have any further questions, we recommend creating a new topic.

    Cheers!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Woocommerce hook to append shipping method form’ is closed to new replies.