• Resolved sharmaabhi

    (@sharmaabhi)


    Hello,

    I have shipping methods that I created using Flexible Shipping plugins. I’ve added rule and rate for these methods.
    But when I trying to get data of shipping methods using WC()->shipping->get_shipping->methods(); It returns me empty rate array for every shipping method(Flat Rate, Local Pickup, Flexible Shipping).
    Output is below :-

    [flexible_shipping] => WPDesk_Flexible_Shipping Object
            (
                [default_method:WPDesk_Flexible_Shipping:private] => 
                [message_added:WPDesk_Flexible_Shipping:private] => 
                [supports] => Array
                    (
                        [0] => shipping-zones
                        [1] => instance-settings
                    )
    
                [id] => flexible_shipping
                [method_title] => Flexible Shipping
                [method_description] => Flexible Shipping
                [enabled] => yes
                [title] => Flexible Shipping
                [rates] => Array
                    (
                    )
    
                [tax_status] => taxable
                [fee] => 
                [minimum_fee] => 
                [instance_id] => 0
                [instance_form_fields] => Array
                    (
                        [0] => Array
                            (
                                [title] => Flexible Shipping
                                [type] => title
                                [description] => See how to configure Flexible Shipping.
                            )
    
                        [title] => Array
                            (
                                [title] => Shipping title
                                [type] => text
                                [description] => Visible only to admin in WooCommerce settings.
                                [default] => Flexible Shipping
                                [desc_tip] => 1
                            )
    
                        [tax_status] => Array
                            (
                                [title] => Tax Status
                                [type] => select
                                [class] => wc-enhanced-select
                                [default] => taxable
                                [options] => Array
                                    (
                                        [taxable] => Taxable
                                        [none] => None
                                    )
    
                            )
    
                        [title_shipping_methods] => Array
                            (
                                [title] => Shipping Methods
                                [type] => title_shipping_methods
                                [description] => 
                            )
    
                        [shipping_methods] => Array
                            (
                                [title] => Shipping Methods
                                [type] => shipping_methods
                                [desc_tip] => 1
                            )
    
                    )
    
                [instance_settings] => Array
                    (
                    )
    
                [availability] => 
                [countries] => Array
                    (
                    )
    
                [plugin_id] => woocommerce_
                [errors] => Array
                    (
                    )
    
                [settings] => Array
                    (
                        [0] => 
                        [title] => Flexible Shipping
                        [tax_status] => taxable
                        [title_shipping_methods] => 
                        [shipping_methods] => 
                        [1] => 
                        [availability] => 
                        [type] => class
                    )
    
                [form_fields] => Array
                    (
                        [0] => Array
                            (
                                [title] => Flexible Shipping
                                [type] => title
                                [description] => See how to configure Flexible Shipping.
                            )
    
                        [title] => Array
                            (
                                [title] => Shipping title
                                [type] => text
                                [description] => Visible only to admin in WooCommerce settings.
                                [default] => Flexible Shipping
                                [desc_tip] => 1
                            )
    
                        [tax_status] => Array
                            (
                                [title] => Tax Status
                                [type] => select
                                [class] => wc-enhanced-select
                                [default] => taxable
                                [options] => Array
                                    (
                                        [taxable] => Taxable
                                        [none] => None
                                    )
    
                            )
    
                        [title_shipping_methods] => Array
                            (
                                [title] => Shipping Methods
                                [type] => title_shipping_methods
                                [description] => 
                            )
    
                        [shipping_methods] => Array
                            (
                                [title] => Shipping Methods
                                [type] => shipping_methods
                                [desc_tip] => 1
                            )
    
                    )
    
                [data:protected] => Array
                    (
                    )
    
                [shipping_methods_option] => flexible_shipping_methods_0
                [shipping_method_order_option] => flexible_shipping_method_order_0
                [section_name] => flexible_shipping
                [type] => class
            )
    

    Wordpress Version 4.9.4
    WooCommerce version 3.3.3.

    Please help me with this

    Thanks

    • This topic was modified 6 years, 12 months ago by sharmaabhi.
    • This topic was modified 6 years, 12 months ago by sharmaabhi.
Viewing 11 replies - 1 through 11 (of 11 total)
  • Hi @sharmaabhi,

    I get an array of shipping methods when I use WC()->shipping->get_shipping_methods();

    Maybe try disabling all other plugins except WooCommerce to see if something is causing an issue? Also where are you loading this?

    Thread Starter sharmaabhi

    (@sharmaabhi)

    Thanks for the reply
    I’m loading this on checkout page which uses custom template. I need to show a cost of the shipping method that user selected on the cart page. But the rates array for the shipping method in above output is empty.

    Here’s how I’m doing this :-

    $rate_table = array();
    
    $shipping_methods = WC()->shipping->get_shipping_methods();
    
    foreach($shipping_methods as $shipping_method){
         
           foreach($shipping_method->rates as $key=>$val){		 
                  $rate_table[$key]= $val;
           }	
    }
    	
    $shipping_price = $rate_table[WC()->session->get( 'chosen_shipping_methods' )[0]]->cost;

    I’ll try with disabling other plugins.

    Hi @sharmaabhi,

    Yeah I would try disabling other plugins. Also maybe try running this function on an existing page like Cart as a test to see if it’s the context you are in that’s the problem or the actual code.

    Thread Starter sharmaabhi

    (@sharmaabhi)

    Hi @mikey
    I tried disabling other plugins but still getting empty rates array.

    Also maybe try running this function on an existing page like Cart as a test to see if it’s the context you are in that’s the problem or the actual code.

    I’ve already tried this on cart page but still got the same result.

    Could it be something to do with version ? Because before the website was on old version of both wordpress and woocommerce, then I was getting the desired result.

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    What are you trying to do exactly? There might be a better way to go about getting the result you need.

    And when you say this still happens with other plugins disabled, is that counting the WPDesk_Flexible_Shipping plugin? Because that sounds rather involved with what you’re doing.

    Thread Starter sharmaabhi

    (@sharmaabhi)

    I’ve created a custom checkout template page. In this template I’m using custom sidebar to show cart widget. Cart widget only shows items detail that user added to cart. I also want to show tax price and shipping price with it. Now tax price, I’m able to show but not shipping price.

    No, I did not disabled WPDesk_Flexible_Shipping plugin as I’ve added shipping method using this plugin. I’m not using flat-rate or free shipping method.

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    So it seems likely to me the interactions with WC()->shipping->get_shipping_methods() are problematic because of the WPDesk_Flexible_Shipping plugin. As neither Mikey nor I could replicate the problems you’re describing.

    For what you need, I’d recommend just picking apart this template perhaps: https://github.com/woocommerce/woocommerce/blob/master/templates/cart/cart-totals.php

    But yea, if you can test your customizations without the WPDesk_Flexible_Shipping by temporarily adding core flat rate/free shipping and still replicate this problem, then there might be a bug. But seems far more likely that plugin is the cause of this.

    Thread Starter sharmaabhi

    (@sharmaabhi)

    I tried using flat rate and disabling WPDesk_Flexible_Shipping plugin, still it didn’t work.
    I tried it even with fresh installed new project still got the same result.

    Thread Starter sharmaabhi

    (@sharmaabhi)

    Ok, It’s a bit strange but if I enable the shipping debug mode then I got the desired “rates” array in the result of WC()->shipping->get_shipping_methods().

    Array
    (
        [2] => WC_Shipping_Flat_Rate Object
            (
                [fee_cost:protected] => 55
                [supports] => Array
                    (
                        [0] => shipping-zones
                        [1] => instance-settings
                        [2] => instance-settings-modal
                    )
    
                [id] => flat_rate
                [method_title] => Flat rate
                [method_description] => 
    Lets you charge a fixed rate for shipping.
    
                [enabled] => yes
                [title] => Flat rate
                [rates] => Array
                    (
                        [flat_rate:2] => WC_Shipping_Rate Object
                            (
                                [data:protected] => Array
                                    (
                                        [id] => flat_rate:2
                                        [method_id] => flat_rate
                                        [instance_id] => 2
                                        [label] => Flat rate
                                        [cost] => 10.00
                                        [taxes] => Array
                                            (
                                            )
    
                                    )
    
                                [meta_data:protected] => Array
                                    (
                                        [Items] => Belt × 1
                                    )
    
                            )
    
                    )

    I’m also getting this in result

    [flat_rate] => WC_Shipping_Flat_Rate Object
            (
                [fee_cost:protected] => 
                [supports] => Array
                    (
                        [0] => shipping-zones
                        [1] => instance-settings
                        [2] => instance-settings-modal
                    )
    
                [id] => flat_rate
                [method_title] => Flat rate
                [method_description] => Lets you charge a fixed rate for shipping.
                [enabled] => yes
                [title] => 
                [rates] => Array
                    (
                    )
    

    I don’t know why there are two object for flat rate shipping method?

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    One if the method, and the other is the rate attached to an actual package (items).

    Do take a look at how WC using these methods and try to follow that. Need to be aware of where/when you are hooking into the process as well. https://github.com/woocommerce/woocommerce/blob/64bcabf0af9a05274d53ec833a4e8c9153509bc4/includes/wc-cart-functions.php#L205

    This isn’t something we can really support as it’s a customization and not a core feature set. If you need development help, I would recommend hiring a developer for further insights and help:

    https://jobs.wordpress.net/
    https://codeable.io/
    https://woocommerce.com/experts/

    Thread Starter sharmaabhi

    (@sharmaabhi)

    I used WC()->shipping->get_packages() this method to get the desired result.
    Appreciate the help. Thanks ??

    • This reply was modified 6 years, 11 months ago by sharmaabhi.
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘get_shipping_methods() return empty rate arary’ is closed to new replies.