Home » Php » php – Woocommerce is returning empty array list while fetching shipping methods

php – Woocommerce is returning empty array list while fetching shipping methods

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am building a custom shipping method plugin for which i need to access shipping method variables .

      echo  "<pre>";
        print_r( $woocommerce->shipping->get_shipping_methods);

      echo"</pre>";

doing this in my plugin is returning an empty set or array
while,

echo  "<pre>";
    print_r( $woocommerce->shipping->get_shipping_methods);

  echo"</pre>";

returns expected value(available shipping methods)
any ideas is it due to some kind of error or what ?

here is shipping object which am getting –>

WC_Shipping Object
(
    [enabled] => 1
    [shipping_methods] => Array
        (
        )

    [shipping_total] => 0
    [shipping_taxes] => Array
        (
        )

    [shipping_label] => 
    [shipping_classes] => Array
        (
        )

    [packages] => Array
        (
        )

)
How to&Answers:

I am using the following snippet:
$shipping_methods = $woocommerce->shipping->load_shipping_methods();

This returns the shipping methods for me.

Answer:

try before call

$woocommerce->shipping->load_shipping_methods();

Answer:

I don’t see any difference between the two blocks of code that you posted but I think the correct way of doing this should be to make a function call rather than accessing it as a property of the class.

echo  "<pre>";

print_r( $woocommerce->shipping->get_shipping_methods() );

echo "</pre>";

(Note the brackets just after get_shipping_methods)