wc_get_shipping_method_count( bool $include_legacy = false, bool $enabled_only = false )
Gets number of shipping methods currently enabled. Used to identify if shipping is configured.
Description Description
Parameters Parameters
- $include_legacy
-
(Optional) Count legacy shipping methods too.
Default value: false
- $enabled_only
-
(Optional) Whether non-legacy shipping methods should be restricted to enabled ones. It doesn't affect legacy shipping methods. @since 4.3.0.
Default value: false
Return Return
(int)
Source Source
File: includes/wc-core-functions.php
function wc_get_shipping_method_count( $include_legacy = false, $enabled_only = false ) {
global $wpdb;
$transient_name = $include_legacy ? 'wc_shipping_method_count_legacy' : 'wc_shipping_method_count';
$transient_version = WC_Cache_Helper::get_transient_version( 'shipping' );
$transient_value = get_transient( $transient_name );
if ( isset( $transient_value['value'], $transient_value['version'] ) && $transient_value['version'] === $transient_version ) {
return absint( $transient_value['value'] );
}
$where_clause = $enabled_only ? 'WHERE is_enabled=1' : '';
$method_count = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods ${where_clause}" ) );
if ( $include_legacy ) {
// Count activated methods that don't support shipping zones.
$methods = WC()->shipping()->get_shipping_methods();
foreach ( $methods as $method ) {
if ( isset( $method->enabled ) && 'yes' === $method->enabled && ! $method->supports( 'shipping-zones' ) ) {
$method_count++;
}
}
}
$transient_value = array(
'version' => $transient_version,
'value' => $method_count,
);
set_transient( $transient_name, $transient_value, DAY_IN_SECONDS * 30 );
return $method_count;
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |