Skip to content

Instantly share code, notes, and snippets.

@justinstern
Created October 30, 2013 03:25
Show Gist options
  • Save justinstern/7226790 to your computer and use it in GitHub Desktop.
Save justinstern/7226790 to your computer and use it in GitHub Desktop.
Subscriptions test handler code. Adds a "Renew Subscription" button to the Admin Edit Order page for orders containing a subscription product, which when clicked causes the subscriptions renewal code to be fired. This can be added to functions.php
<?php
add_action( 'woocommerce_order_actions_start', function() {
global $post_id;
$order = new WC_Order( $post_id );
if ( class_exists( 'WC_Subscriptions_Order' ) && WC_Subscriptions_Order::order_contains_subscription( $order ) ) {
?>
<li class="wide" style="text-align: center;">
<a class="button tips" href="<?php echo add_query_arg( array( 'wc_subscriptions_action' => 'renew' ) ); ?>" data-tip="<?php esc_attr_e( 'Renew this subscription' ); ?>" style="cursor: pointer !important;"><?php _e( 'Renew This Subscription' ); ?></a>
</li>
<?php
}
} );
add_action( 'admin_footer-post.php', function() {
if ( isset( $_GET['wc_subscriptions_action'] ) && 'renew' == $_GET['wc_subscriptions_action'] ) {
$order = new WC_Order( $_GET['post'] );
$subscription = WC_Subscriptions_Manager::get_subscription( WC_Subscriptions_Manager::get_subscription_key( $order->id ) );
$order = new WC_Order( $subscription['order_id'] );
$amount_to_charge = WC_Subscriptions_Order::get_recurring_total( $order );
$outstanding_payments = WC_Subscriptions_Order::get_outstanding_balance( $order, $subscription['product_id'] );
if ( $outstanding_payments > 0 )
$amount_to_charge += $outstanding_payments;
do_action( 'scheduled_subscription_payment_' . $order->payment_method, $amount_to_charge, $order, $subscription['product_id'] );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment