Skip to content

Instantly share code, notes, and snippets.

@daviddarke
Last active October 2, 2019 10:51
Show Gist options
  • Save daviddarke/dbf4f9fceae755b9779d477026474f3b to your computer and use it in GitHub Desktop.
Save daviddarke/dbf4f9fceae755b9779d477026474f3b to your computer and use it in GitHub Desktop.
Update WooCommerce subscription pricing for future subscription orders
if ( defined( 'WP_CLI' ) && WP_CLI ) {
class Woo_Subs_Cli extends WP_CLI_Command {
public function update_sub_pricing( $args ){
// Quick check to make sure all the require args are available
if( count( $args ) != 2 && is_numeric( $args[1] ) && floor( $args[1] ) != $args[1] ){
WP_CLI::line( 'Please provide 1 subscription ID and new price in this format:' );
WP_CLI::line( 'woosubscriptions update_sub_pricing 1111 30' );
return;
}
WP_CLI::confirm( 'Do you want to update all future payments of subscription product #' . $args[0] . ' to "' . $args[1] .'"' );
// get all active subscription from product ID
$subscriptions = wcs_get_subscriptions_for_product( $args[0] );
if ($subscriptions > 0) {
foreach ($subscriptions as $key => $subscription) {
$subscription = new WC_Subscription( $subscription );
$items = $subscription->get_items();
$product_found = false;
foreach ($items as $key => $product) {
if( $product->get_product_id() == $args[0] ){
$product_found = true;
$product->set_total( $args[1] );
$product->save();
}
}
if( $product_found ){
$subscription->set_total( $args[1] );
$subscription->save();
}
$subscription->add_order_note( 'Subscription price updated to ' . get_woocommerce_currency_symbol() . $args[1] );
WP_CLI::line( "Subscription #" . $subscription->get_id() . " updated" );
}
}else{
WP_CLI::line( 'No subscriptions found' );
}
}
}
WP_CLI::add_command( 'woosubscriptions', 'Woo_Subs_Cli' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment