Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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' );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement