Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gedex/eb68baf0c5be490b6b2a5165dbbfecca to your computer and use it in GitHub Desktop.
Save gedex/eb68baf0c5be490b6b2a5165dbbfecca to your computer and use it in GitHub Desktop.
<?php
if ( ! defined( 'WP_CLI' ) ) {
return;
}
if ( ! WP_CLI ) {
return;
}
function _wc_clear_shipping_method_in_shipping_zones( $args ) {
if ( empty( $args[0] ) ) {
_wc_clear_shipping_method_usage();
}
$shipping_method_id = 'usps';
$zones = WC_Shipping_Zones::get_zones();
$found = 0;
// First, check zone 0 (Rest of the world).
$rest_of_the_world = WC_Shipping_Zones::get_zone( 0 );
foreach ( $rest_of_the_world->get_shipping_methods() as $shipping_method ) {
if ( $shipping_method_id === $shipping_method->id ) {
$found++;
}
}
// Zones added by user.
foreach ( $zones as $zone_id => $zone ) {
foreach ( $zone['shipping_methods'] as $shipping_method ) {
if ( 'usps' === $shipping_method->id ) {
$found++;
}
}
}
WP_CLI::success( sprintf( 'Cleared %d USPS method across shipping zones.', $found ) );
}
function _wc_clear_shipping_method_usage() {
WP_CLI::line( 'Usage: wp clear_shipping_method <shipping_method_id>' );
exit(1);
}
WP_CLI::add_command( 'clear_shipping_method', '_wc_clear_shipping_method_in_shipping_zones' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment