Forked from strangerstudios/pmpro_isOrderRecurring.php
Created
August 23, 2018 19:16
-
-
Save pbrocks/d0bb14c07443eb22d39a27e542aec028 to your computer and use it in GitHub Desktop.
Perform an action on Paid Memberships Pro (PMPro) recurring orders only.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Perform an action on PMPro recurring orders only. | |
A recurring order here is one that | |
(1) Has an earlier order with the same subscription_transaction_id. | |
(2) Is not created at PMPro checkout. | |
Note that we are checking if function_exists for pmpro_isOrderRecurring incase | |
we add this to PMPro core. Also note that the $test_checkout param there is used | |
here to avoid #2 above. So this parameter will check if the currently running PHP | |
script is being fired at checkout, not necessarily if the order was created at checkout, | |
which is not tracked by PMPro right now. | |
If you use pmpro_isOrderRecurring on a batch of existing orders without the $test_checkout | |
param, it will only check #1 which for some gateways like Stripe and Braintree may | |
return false positives since we use the customer id for the subscription transaction id | |
for those gateways and follow up checkouts will look like recurring orders even if | |
they aren't. | |
*/ | |
//function to test if an order is recurring or not | |
if(!function_exists("pmpro_isOrderRecurring")) | |
{ | |
function pmpro_isOrderRecurring($order, $test_checkout = false) | |
{ | |
global $wpdb; | |
//must have a subscription_transaction_id | |
if(empty($order->subscription_transaction_id)) | |
return false; | |
//check that we aren't processing at checkout | |
if($test_checkout && !empty($_REQUEST['submit-checkout'])) | |
return false; | |
//check for earlier orders with the same gateway, user_id, membership_id, and subscription_transaction_id | |
$sqlQuery = "SELECT id FROM $wpdb->pmpro_membership_orders WHERE | |
gateway = '" . esc_sql($order->gateway) . "' AND | |
gateway_environment = '" . esc_sql($order->gateway_environment) . "' AND | |
user_id = '" . esc_sql($order->user_id) . "' AND | |
membership_id = '" . esc_sql($order->membership_id) . "' AND | |
subscription_transaction_id = '" . esc_sql($order->subscription_transaction_id) . "' AND | |
timestamp < '" . date("Y-m-d", $order->timestamp) . "' "; | |
if(!empty($order->id)) | |
$sqlQuery .= " AND id <> '" . esc_sql($order->id) . "' "; | |
$sqlQuery .= "LIMIT 1"; | |
$earlier_order = $wpdb->get_var($sqlQuery); | |
if(empty($earlier_order)) | |
return false; | |
//must be recurring | |
return true; | |
} | |
} | |
//find first order from a given order | |
function get_first_order( $order ) { | |
global $wpdb; | |
//do you even subscribing, bro? | |
if( empty( $order->subscription_transaction_id ) ) { | |
return false; | |
} | |
// get the order ID of the first payment of this subscription | |
$query = "SELECT MIN(id), payment_transaction_id FROM $wpdb->pmpro_membership_orders WHERE | |
gateway = '" . esc_sql( $order->gateway ) . "' AND | |
gateway_environment = '" . esc_sql( $order->gateway_environment ) . "' AND | |
user_id = '" . esc_sql( $order->user_id) . "' AND | |
membership_id = '" . esc_sql( $order->membership_id ) . "' AND | |
subscription_transaction_id = '" . esc_sql( $order->subscription_transaction_id ) . "' "; | |
//if this is an existing order, make sure we don't select our self | |
if(!empty($order->id)) | |
$query .= "AND id < '" . esc_sql( $order->id ) . "' "; | |
//just the first | |
$query .= "LIMIT 1"; | |
return $wpdb->get_row( $query ); | |
} | |
//before orders are added | |
function my_pmpro_add_order($morder) | |
{ | |
if(pmpro_isOrderRecurring($morder, true)) | |
{ | |
//this code should only fire before new recurring orders are added | |
echo "(recurring order being added)"; | |
} | |
} | |
add_action("pmpro_add_order", "my_pmpro_add_order"); | |
//after orders are added | |
function my_pmpro_added_order($morder) | |
{ | |
if(pmpro_isOrderRecurring($morder, true)) | |
{ | |
//this code should only fire after new recurring orders are added | |
global $wpdb; | |
$wpdb->query("UPDATE $wpdb->pmpro_membership_orders SET notes = CONCAT(notes, ' Added after recurring order is saved.') WHERE id = '" . esc_sql($morder->id) . "' LIMIT 1"); | |
} | |
} | |
add_action("pmpro_added_order", "my_pmpro_added_order"); | |
/* | |
add recurring column to orders table | |
*/ | |
//table header | |
function my_pmpro_orders_extra_cols_header() | |
{ | |
?> | |
<th>Recurring?</th> | |
<?php | |
} | |
add_action('pmpro_orders_extra_cols_header', 'my_pmpro_orders_extra_cols_header'); | |
//table body | |
function my_pmpro_orders_extra_cols_body($order) | |
{ | |
$forder = get_first_order($order); | |
?> | |
<td> | |
<?php | |
if(pmpro_isOrderRecurring($order)) | |
echo "Yes"; | |
else | |
echo "No"; | |
?> | |
</td> | |
<?php | |
} | |
add_action('pmpro_orders_extra_cols_body', 'my_pmpro_orders_extra_cols_body'); | |
/* | |
add recurring column to orders export | |
*/ | |
function my_pmpro_orders_csv_extra_columns($columns) | |
{ | |
$columns['recurring'] = 'my_pmpro_orders_csv_recurring_column'; | |
return $columns; | |
} | |
add_filter('pmpro_orders_csv_extra_columns', 'my_pmpro_orders_csv_extra_columns'); | |
function my_pmpro_orders_csv_recurring_column($order) | |
{ | |
if(pmpro_isOrderRecurring($order)) | |
return "Yes"; | |
else | |
return "No"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment