wc_get_shipping_method_count

Gets number of shipping methods currently enabled.

Description

(int) wc_get_shipping_method_count( (bool) $include_legacy = false ); 

Used to identify if shipping is configured.

Returns (int)

Parameters (1)

0. $include_legacy — Optional. (bool) => false
Count legacy shipping methods too.

Usage

  1. if ( !function_exists( 'wc_get_shipping_method_count' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-core-functions.php'; 
  3.  
  4. // Count legacy shipping methods too. 
  5. $include_legacy = false; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_get_shipping_method_count($include_legacy); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-core-functions.php  
  1. function wc_get_shipping_method_count( $include_legacy = false ) { 
  2. global $wpdb; 
  3.  
  4. $transient_name = 'wc_shipping_method_count_' . ( $include_legacy ? 1 : 0 ) . '_' . WC_Cache_Helper::get_transient_version( 'shipping' ); 
  5. $method_count = get_transient( $transient_name ); 
  6.  
  7. if ( false === $method_count ) { 
  8. $method_count = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods" ) ); 
  9.  
  10. if ( $include_legacy ) { 
  11. // Count activated methods that don't support shipping zones. 
  12. $methods = WC()->shipping->get_shipping_methods(); 
  13.  
  14. foreach ( $methods as $method ) { 
  15. if ( isset( $method->enabled ) && 'yes' === $method->enabled && ! $method->supports( 'shipping-zones' ) ) { 
  16. $method_count++; 
  17.  
  18. set_transient( $transient_name, $method_count, DAY_IN_SECONDS * 30 ); 
  19.  
  20. return absint( $method_count );