6

I've been unable to crack this nut, but can't help feel that I'm missing something simple.

I'm developing a WooCommerce plugin that should provide a list of shipping classes on its admin settings page. The following code as suggested in another question's answer indicated that the following should work:

$shipping           = new \WC_Shipping();
$shipping_classes   = $shipping->get_shipping_classes();
var_dump($shipping_classes);
die();

Unfortunately the output is an empty array.

I am using Wordpress 4.9.5 and WooCommerce 3.3.5. Thanks for any help!

UPDATE I have the exact same problem as outlined here: get_terms() returns Invalid Taxonomy and have provided a work-around. However, I do not feel that is a solution.

dbc
  • 104,963
  • 20
  • 228
  • 340
mike.bronner
  • 1,203
  • 1
  • 20
  • 39
  • Are you trying to get all of the shipping classes, or the shipping methods? Also, have you tried removing the slash in front of `WC_Shipping();`? – Frits Apr 15 '18 at 17:42

3 Answers3

6

To get all the shipping classes you just need the following:

$shipping_classes = get_terms( array('taxonomy' => 'product_shipping_class', 'hide_empty' => false ) );

Tested and works. This will give you an array of the WP_Term objects of all shipping classes.

In Woocommerce the shipping classes are under product_shipping_class custom taxonomy.


Or you can use this custom function with a simple SQL query:

function wc_get_shipping_classes(){
    global $wpdb;
    $return $wpdb->get_results( "
        SELECT * FROM {$wpdb->prefix}terms as t
        INNER JOIN {$wpdb->prefix}term_taxonomy as tt ON t.term_id = tt.term_id
        WHERE tt.taxonomy LIKE 'product_shipping_class'
    " );
}

Code goes in function.php file of your active child theme (or active theme).

USAGE (test example):

$shipping_classes = wc_get_shipping_classes(); // Get Shipping Classes
echo '<pre>'; print_r($shipping_classes); echo '</pre>'; // Test raw output   
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • This does not actually work in my case. I have tried this, and it only returns a single shipping class for some reason, not all of them. I added a link in the original question to another question I found that more precisely poses the exact issue. I also provided a work-around there, but I do not think that was a proper solution. – mike.bronner Apr 16 '18 at 16:25
  • @mike.bronner Very, strange because all shipping classes are terms from "product_shipping_class" custom taxonomy in Woocommerce. So **I have updated my answer, adding a 2nd way**… Try it. – LoicTheAztec Apr 16 '18 at 16:46
  • Thanks for taking the effort. :) I'm happy looping through the results of `get_terms()` (without any arguments) and filtering out my results. I'm fairly confident there is a bug or conflict somewhere that is wreaking havok. :| – mike.bronner Apr 17 '18 at 00:09
1

This is actually really easy...

/** 
 * @var WP_Terms[] $shipping_classes
 */
$shipping_classes = WC()->shipping()->get_shipping_classes();
Daniel Morell
  • 2,308
  • 20
  • 34
0

Maybe someone is still looking for a solution :)

$WC_Shipping = new WC_Shipping();
$shipping_classes = $WC_Shipping->get_shipping_classes();
var_dump($shipping_classes);