WordPress Affiliate Manager › Forums › Affiliate Manager Plugin › Recurring subscription switch(upgrade/downgrade) breaks the affiliate link? › Reply To: Recurring subscription switch(upgrade/downgrade) breaks the affiliate link?
Hi Shadow Labs, mbrsolution, wp.insider, affmngr
I’ve done some digging and came up with fixes in the addon to the issue reported:
1. Subscription upgrade (Switch) triggered gap payments are not rewarded to the affiliate.
2. Renewals after the upgrade don’t seem to be rewarded to affiliate.
Issue #1 is addressed by adding a new hook which takes care of the gap payment at time of switch complete:
add_action(‘woocommerce_subscriptions_switch_completed’,’wpam_woocommerce_subscriptions_switch_completed’,20,1);
The hook will do things similar to the existing wpam_woocommerce_subscription_payment_complete.
Issue #2 is actually not as bad. It only skips the renewals because I rushed the renewals on the same day of the initial subscription. As a result, the txn_id (parent oder ID + date) is the same as the key of the previous record, and got dropped silently when the plugin tried to insert it into DB. So I changed the hook logic a little to use renewal order id instead of the parent order id to build the txn_id.
With both fixes in, the issues seem to have been fixed.
I’m attaching the code as a reference. The fixes are only focusing on my specific needs. I trust you guys can come up with the right solution for the plugin. Please let me know the next release.
Thanks,
Jim
affiliatemgr-wc-subscription-integration.php:
add_action(‘woocommerce_subscriptions_switch_completed’,’wpam_woocommerce_subscriptions_switch_completed’,20,1);
function wpam_woocommerce_subscriptions_switch_completed($order) {
WPAM_Logger::log_debug(‘WooCommerce Subscription Integration – woocommerce_subscriptions_switch_completed hook triggered’);
$subscriptions = wcs_get_subscriptions_for_order($order);
$theSub=null;
foreach($subscriptions as $subscription) {
$sub_status = $subscription->get_status();
WPAM_Logger::log_debug(“WooCommerce Subscription Integration – subscription:” . $subscription->get_id() . ” status: ” . $sub_status);
// only one activate sub can be tied to an order.
if (strtolower($sub_status) == “active”) {
// this is our sub
$theSub=$subscription;
break;
}
}
$parent_order_id=$theSub->get_parent_id();
$parent_order=wc_get_order($parent_order_id);
$order_id = $order->get_id(); //an alternative is to use $subscription->get_parent_id(). old method: $order->id or $subscription->order->id;
//WPAM_Logger::log_debug(‘WooCommerce Subscription Integration – Parent Order ID: ‘.$order_id.’, Subscription Total: ‘.$subscription->get_total().’, Total: ‘.$order->order_total);
$total = $order->get_total(); //$order->order_total is better for a new subscription payment since it contains the actual amount charged. It also works well when there is a free trial.
$shipping = $order->get_total_shipping();
$tax = $order->get_total_tax();
WPAM_Logger::log_debug(‘WooCommerce Subscription Integration – Order ID: ‘.$order_id.’, Total amount: ‘ . $total . ‘, Total shipping: ‘ . $shipping . ‘, Total tax: ‘ . $tax);
WPAM_Logger::log_debug(‘WooCommerce Subscription Integration – Subscription ID: ‘ . $theSub->get_id());
WPAM_Logger::log_debug(‘WooCommerce Subscription Integration – Parent Order ID: ‘ . $parent_order_id);
$purchaseAmount = $total – $shipping – $tax;
$wpam_refkey = get_post_meta($parent_order_id, ‘_wpam_refkey’, true);
$wpam_id = get_post_meta($parent_order_id, ‘_wpam_id’, true);
if(!empty($wpam_id)){
$wpam_refkey = $wpam_id;
}
$wpam_refkey = apply_filters( ‘wpam_woo_override_refkey’, $wpam_refkey, $parent_order);
if (empty($wpam_refkey)) {
WPAM_Logger::log_debug(“WooCommerce Subscription Integration – could not get wpam_id/wpam_refkey from cookie. This is not an affiliate sale”);
return;
}
$order_status = $parent_order->get_status();
WPAM_Logger::log_debug(“WooCommerce Subscription Integration – Order status: ” . $order_status);
if (strtolower($order_status) != “completed” && strtolower($order_status) != “processing”) {
WPAM_Logger::log_debug(“WooCommerce Subscription Integration – Order status for this transaction is not in a ‘completed’ or ‘processing’ state. Commission will not be awarded at this stage.”);
WPAM_Logger::log_debug(“WooCommerce Subscription Integration – Commission for this transaciton will be awarded when you set the order status to completed or processing.”);
return;
}
//$txn_id = $order_id . “_” . date(“Y-m-d”); //Add the subscription charge date to make this unique
$txn_id = $order_id . “_” . date(“Y-m-d”); //Add the subscription charge date to make this unique
$requestTracker = new WPAM_Tracking_RequestTracker();
//WPAM_Logger::log_debug(‘WooCommerce Subscription Integration – awarding commission for order ID: ‘ . $order_id . ‘, Purchase amount: ‘ . $purchaseAmount);
WPAM_Logger::log_debug(‘WooCommerce Subscription Integration – awarding commission for witch order ID: ‘ . $order_id . ‘, Purchase amount: ‘ . $purchaseAmount);
$requestTracker->handleCheckoutWithRefKey($txn_id, $purchaseAmount, $wpam_refkey);
}
add_action(‘woocommerce_subscription_payment_complete’, ‘wpam_woocommerce_subscription_payment_complete’); //Triggered when a subscription payment is made
function wpam_woocommerce_subscription_payment_complete($subscription) {
WPAM_Logger::log_debug(‘WooCommerce Subscription Integration – woocommerce_subscription_payment_complete hook triggered’);
if (!is_object($subscription)) {
$subscription = new WC_Subscription($subscription);
}
$order = $subscription->get_parent(); //Getting an instance of the related WC_Order object old method: $subscription->order;
if (!is_object($order)) {
$order = new WC_Order($order);
}
$order_id = $order->get_id(); //an alternative is to use $subscription->get_parent_id(). old method: $order->id or $subscription->order->id;
//WPAM_Logger::log_debug(‘WooCommerce Subscription Integration – Parent Order ID: ‘.$order_id.’, Subscription Total: ‘.$subscription->get_total().’, Total: ‘.$order->order_total);
$total = $subscription->get_total();
$relatedOrders = $subscription->get_related_orders();
if (count($relatedOrders) == 1) { //new subscription payment notification
$total = $order->get_total(); //$order->order_total is better for a new subscription payment since it contains the actual amount charged. It also works well when there is a free trial.
}
$shipping = $order->get_total_shipping();
$tax = $order->get_total_tax();
WPAM_Logger::log_debug(‘WooCommerce Subscription Integration – Parent Order ID: ‘.$order_id.’, Total amount: ‘ . $total . ‘, Total shipping: ‘ . $shipping . ‘, Total tax: ‘ . $tax);
$purchaseAmount = $total – $shipping – $tax;
$wpam_refkey = get_post_meta($order_id, ‘_wpam_refkey’, true);
$wpam_id = get_post_meta($order_id, ‘_wpam_id’, true);
if(!empty($wpam_id)){
$wpam_refkey = $wpam_id;
}
$wpam_refkey = apply_filters( ‘wpam_woo_override_refkey’, $wpam_refkey, $order);
if (empty($wpam_refkey)) {
WPAM_Logger::log_debug(“WooCommerce Subscription Integration – could not get wpam_id/wpam_refkey from cookie. This is not an affiliate sale”);
return;
}
$order_status = $order->get_status();
WPAM_Logger::log_debug(“WooCommerce Subscription Integration – Order status: ” . $order_status);
if (strtolower($order_status) != “completed” && strtolower($order_status) != “processing”) {
WPAM_Logger::log_debug(“WooCommerce Subscription Integration – Order status for this transaction is not in a ‘completed’ or ‘processing’ state. Commission will not be awarded at this stage.”);
WPAM_Logger::log_debug(“WooCommerce Subscription Integration – Commission for this transaciton will be awarded when you set the order status to completed or processing.”);
return;
}
//$txn_id = $order_id . “_” . date(“Y-m-d”); //Add the subscription charge date to make this unique
$txn_id = $subscription->get_last_order() . “_” . date(“Y-m-d”); //Add the subscription charge date to make this unique
$requestTracker = new WPAM_Tracking_RequestTracker();
//WPAM_Logger::log_debug(‘WooCommerce Subscription Integration – awarding commission for order ID: ‘ . $order_id . ‘, Purchase amount: ‘ . $purchaseAmount);
WPAM_Logger::log_debug(‘WooCommerce Subscription Integration – awarding commission for order ID: ‘ . $subscription->get_last_order() . ‘, Purchase amount: ‘ . $purchaseAmount);
$requestTracker->handleCheckoutWithRefKey($txn_id, $purchaseAmount, $wpam_refkey);
}