Created
August 14, 2016 17:00
-
-
Save gedex/e9432329ab94a42ac985d1d97160c149 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 = $args[0]; | |
$zones = WC_Shipping_Zones::get_zones(); | |
$found = 0; | |
$deleted = 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 ) { | |
if ( _wc_clear_shipping_method_delete_method( 0, $shipping_method ) ) { | |
$deleted++; | |
} | |
$found++; | |
} | |
} | |
// Zones added by user. | |
foreach ( $zones as $zone_id => $zone ) { | |
foreach ( $zone['shipping_methods'] as $shipping_method ) { | |
if ( 'usps' === $shipping_method->id ) { | |
if ( _wc_clear_shipping_method_delete_method( $zone_id, $shipping_method ) ) { | |
$deleted++; | |
} | |
$found++; | |
} | |
} | |
} | |
if ( $found > 0 ) { | |
if ( $found === $deleted ) { | |
WP_CLI::success( sprintf( 'Cleared %d USPS method across shipping zones.', $found ) ); | |
} else { | |
WP_CLI::warning( sprintf( 'Only cleared %d USPS method across shipping zones from total of %d.', $deleted, $found ) ); | |
} | |
} else { | |
WP_CLI::warning( sprintf( 'Couldn\'t found instance with shipping method id %s.', $shipping_method_id ) ); | |
} | |
} | |
function _wc_clear_shipping_method_usage() { | |
WP_CLI::line( 'Usage: wp clear_shipping_method <shipping_method_id>' ); | |
exit(1); | |
} | |
function _wc_clear_shipping_method_delete_method( $zone_id, $shipping_method ) { | |
global $wpdb; | |
// Suppress errors. | |
$wpdb->suppress_errors(); | |
$instance_id = $shipping_method->get_instance_id(); | |
$method_id = $shipping_method->id; | |
$option_key = $shipping_method->get_instance_option_key(); | |
$is_deleted = false; | |
WP_CLI::log( sprintf( 'Trying to delete method_id %s with instance_id %d from zone_id %d', $method_id, $instance_id, $zone_id ) ); | |
if ( $wpdb->delete( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'instance_id' => $instance_id ) ) ) { | |
delete_option( $option_key ); | |
do_action( 'woocommerce_shipping_zone_method_deleted', $instance_id, $method_id, $zone_id ); | |
$is_deleted = true; | |
WP_CLI::success( sprintf( 'instance_id %d is successfully deleted.', $instance_id ) ); | |
} else { | |
WP_CLI::warning( sprintf( 'Unable to delete method_id %s with instance_id %d from zone_id %d', $method_id, $instance_id, $zone_id ) ); | |
} | |
return $is_deleted; | |
} | |
add_action( 'woocommerce_init', function() { | |
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