Moustafa Elqabbany
Moustafa Elqabbany

Reputation: 1140

WooCommerce Payment Gateway: Subscription Renewal Date Not Updated

I'm writing a payment gateway that handles WooCommerce subscriptions. Here is the sample code that handles the renewal:

        public function mcc_subscription_renewal($amount, $order) {
            $tid = 123;
            $order->payment_complete( $tid );
            $order->update_status( 'completed' );
            WC_Subscriptions_Manager::process_subscription_payments_on_order( $order );
        }

This is for a simple subscription that charges the customer a fixed fee every year. When I manually run the renewal process, a new order and scheduled action are created successfully, but the date isn't updated to the next year. What am I missing? Why isn't a successfully completed renewal order updating the expiry date of a subscription?

Upvotes: 0

Views: 624

Answers (1)

Debbie Kurth
Debbie Kurth

Reputation: 481

Assuming you have not found an answer yet, a couple of things. Are you just writing the gateway OR are you writing the gateway and a subscription process?

Woocommerce has a addon called Woocommerce subscription. It does all the billing to a gateway, which at the moment are only Stripe/PayPal. If you are writing a new gateway and have a woocommerce subscription add on, then all you need to do is to look at the subscription id and the next payment date:

 public function mcc_subscription_renewal($amount, $order) 
   {
   $order               = new WC_Order( $order );               
   $OrderNumber         = $order->parent_id;
   $ParentOrder         = new WC_Order( $OrderNumber ); // ORDER NUMBER
   $SubscriptionNumber  = $order->get_order_number();
   $PaymentDate         = $order->get_date_created()->format ('Y-m-d');

    $items  = $order->get_items();
    foreach( $items as $item_id => $product )
       {    
       $ProductName         = $product->get_name();        
       $ProductId           = $product->get_product_id(); 
       $ExpireDate          = WC_Subscriptions_Order::get_next_payment_date ( $ParentOrder, $ProductId  );  
       }
    }

You don't have to do anything else. If you are writing your own subscription package, then you need to write the calls and store the purchases ..thus calculate your own expiration dates.

The woocommerce subscription, the length of time and the renewal period is all set under the products definition.

Upvotes: 2

Related Questions