wc_get_shipping_method_count()
Gets number of shipping methods currently enabled. Used to identify if shipping is configured.
Хуков нет.
Возвращает
int.
Использование
wc_get_shipping_method_count( $include_legacy, $enabled_only );
- $include_legacy(true|false)
- Count legacy shipping methods too.
По умолчанию: false - $enabled_only(true|false)
- Whether non-legacy shipping methods should be restricted to enabled ones. It doesn't affect legacy shipping methods. @since 4.3.0.
По умолчанию: false
Список изменений
| С версии 2.6.0 | Введена. |
Код wc_get_shipping_method_count() wc get shipping method count WC 10.3.4
function wc_get_shipping_method_count( $include_legacy = false, $enabled_only = false ) {
global $wpdb;
$transient_name = 'wc_shipping_method_count';
$transient_version = WC_Cache_Helper::get_transient_version( 'shipping' );
$transient_value = get_transient( $transient_name );
$counts = array(
'legacy' => 0,
'enabled' => 0,
'disabled' => 0,
);
if ( ! isset( $transient_value['legacy'], $transient_value['enabled'], $transient_value['disabled'], $transient_value['version'] ) || $transient_value['version'] !== $transient_version ) {
// Count activated methods that don't support shipping zones if $include_legacy is true.
$methods = WC()->shipping()->get_shipping_methods();
$method_ids = array();
foreach ( $methods as $method ) {
$method_ids[] = $method->id;
if ( isset( $method->enabled ) && 'yes' === $method->enabled && ! $method->supports( 'shipping-zones' ) ) {
++$counts['legacy'];
}
}
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
$counts['enabled'] = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE is_enabled=1 AND method_id IN ('" . implode( "','", array_map( 'esc_sql', $method_ids ) ) . "')" ) );
$counts['disabled'] = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE is_enabled=0 AND method_id IN ('" . implode( "','", array_map( 'esc_sql', $method_ids ) ) . "')" ) );
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
$transient_value = array(
'version' => $transient_version,
'legacy' => $counts['legacy'],
'enabled' => $counts['enabled'],
'disabled' => $counts['disabled'],
);
set_transient( $transient_name, $transient_value, DAY_IN_SECONDS * 30 );
} else {
$counts = $transient_value;
}
$return = 0;
if ( $enabled_only ) {
$return = $counts['enabled'];
} else {
$return = $counts['enabled'] + $counts['disabled'];
}
if ( $include_legacy ) {
$return += $counts['legacy'];
}
return $return;
}