Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  2.  
  3. class Woo_Subs_Cli extends WP_CLI_Command {
  4.  
  5. public function update_sub_pricing( $args ){
  6.  
  7. // Quick check to make sure all the require args are available
  8. if( count( $args ) != 2 && is_numeric( $args[1] ) && floor( $args[1] ) != $args[1] ){
  9. WP_CLI::line( 'Please provide 1 subscription ID and new price in this format:' );
  10. WP_CLI::line( 'woosubscriptions update_sub_pricing 1111 30' );
  11. return;
  12. }
  13.  
  14. WP_CLI::confirm( 'Do you want to update all future payments of subscription product #' . $args[0] . ' to "' . $args[1] .'"' );
  15.  
  16. // get all active subscription from product ID
  17. $subscriptions = wcs_get_subscriptions_for_product( $args[0] );
  18.  
  19. if ($subscriptions > 0) {
  20. foreach ($subscriptions as $key => $subscription) {
  21. $subscription = new WC_Subscription( $subscription );
  22.  
  23. $items = $subscription->get_items();
  24.  
  25. $product_found = false;
  26.  
  27. foreach ($items as $key => $product) {
  28.  
  29. if( $product->get_product_id() == $args[0] ){
  30.  
  31. $product_found = true;
  32.  
  33. $product->set_total( $args[1] );
  34. $product->save();
  35.  
  36. }
  37.  
  38. }
  39.  
  40. if( $product_found ){
  41. $subscription->set_total( $args[1] );
  42. $subscription->save();
  43. }
  44.  
  45. $subscription->add_order_note( 'Subscription price updated to ' . get_woocommerce_currency_symbol() . $args[1] );
  46.  
  47. WP_CLI::line( "Subscription #" . $subscription->get_id() . " updated" );
  48.  
  49. }
  50. }else{
  51. WP_CLI::line( 'No subscriptions found' );
  52. }
  53. }
  54. }
  55.  
  56. WP_CLI::add_command( 'woosubscriptions', 'Woo_Subs_Cli' );
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement