• Resolved tpay.com

    (@transferujpl)


    There is an error in generic flat-rate shipping module. When I try to get all available shipping methods in another module like this:

    $Shipping = new WC_Shipping();
    $shippingMethods = $Shipping->get_shipping_methods();

    I am getting fatal error:

    *Fatal error*: Uncaught Error: Call to a member function
    get_shipping_countries() on null in
    /home/markadius/domains/meblearkadius.pl/public_html/wp-content/plugins/woocommerce/includes/shipping/flat-rate/includes/settings-flat-rate.php:47Stack
    trace: #0
    /home/markadius/domains/meblearkadius.pl/public_html/wp-content/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php(47):
    include() #1
    /home/markadius/domains/meblearkadius.pl/public_html/wp-content/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php(38):
    WC_Shipping_Flat_Rate->init() #2
    /home/markadius/domains/meblearkadius.pl/public_html/wp-content/plugins/woocommerce/includes/class-wc-shipping.php(194):
    WC_Shipping_Flat_Rate->__construct() #3
    /home/markadius/domains/meblearkadius.pl/public_html/wp-content/plugins/woocommerce/includes/class-wc-shipping.php(172):
    WC_Shipping->register_shipping_method(‘WC_Shipping_Fla…’) #4
    /home/markadius/domains/meblearkadius.pl/public_html/wp-content/plugins/woocommerce/includes/c
    in
    */home/markadius/domains/meblearkadius.pl/public_html/wp-content/plugins/woocommerce/includes/shipping/flat-rate/includes/settings-flat-rate.php*
    on line *47

    There is also a missing return statement in register_shipping_method method in wp-content/plugins/woocommerce/includes/class-wc-shipping.php

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Julia Amosova

    (@jamosova)

    Hi @transferujpl,

    Would you please tell us a little bit more about the issue you are experiencing? It looks like you are trying to custom code the flat rate shipping method on your site?

    Thread Starter tpay.com

    (@transferujpl)

    Hi @jamosova
    Our goal is to give the merchant an option to choose if our payment gateway should be available for specific shipping method. So in init_form_fields method, we do something similar like generic (WC_Gateway_COD) module does – we try to get the list of shipping methods installed in the shop.
    So at init_form_fields we call our custom method getShippingMethods containing this code:

    public function getShippingMethods()
            {
                if ($this->wpbo_get_woo_version_number() < '3.0.0') {
                    return array();
                }
                $options = array();
                try {
                    $Shipping = new WC_Shipping();
                    $shippingMethods = $Shipping->get_shipping_methods();
                    foreach ($shippingMethods as $method) {
                        if (isset($method->id) && isset($method->method_title)) {
                            $options[$method->id] = $method->method_title;
                        }
                    }
                } catch (Exception $e) {
                    Util::log('Exception in getShippingMethods', print_r($e, true));
                }
    
                return $options;
            }
    

    As you can see we only call one method of WC_Shipping class but it fails in some Woocommerce instances. We was unable to reproduce this issue at our test servers but some merchants reported they do experience this issue.
    We assumed that this may be caused by lower Woocommerce versions so we do check if it is lower than 3.0 but this does not look like the source of problem.

    Plugin Support John Coy a11n

    (@johndcoy)

    Automattic Happiness Engineer

    Hi @transferujpl

    This thread in GitHub should with getting shipping data:

    https://github.com/woocommerce/woocommerce/issues/17056

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Fatal error WC_Shipping->get_shipping_methods’ is closed to new replies.