WordPress Affiliate Manager

Affiliates Management Plugin for WordPress

  • Home
  • Features
    • Screen Shots
  • Download
  • Documentation
  • Addons
  • Support
    • Support Forum
    • Forum Login
    • Forum Registration
    • Contact
    • Premium Addon/Plugin Support

[Support request] Recurring subscription switch(upgrade/downgrade) breaks the affiliate link?

by

WordPress Affiliate Manager › Forums › Affiliate Manager Plugin › Recurring subscription switch(upgrade/downgrade) breaks the affiliate link?

Tagged: WooCommerce Subscriptions renewal payments wpam_id

  • This topic has 10 replies, 2 voices, and was last updated 7 years, 10 months ago by Shadow Labs.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • May 29, 2018 at 10:02 pm #3651
    Jim
    Participant

    Hi there,

    I have WooCommerce Subscriptions + WP Affiliate Manager on a site. Initial payments and renewal payments for subscriptions are all captured, and awarded the affiliate properly as expected.

    But I found that switch-sub (upgrade – an feature offered by the Woo-Subscriptions plugin) triggered gap payments don’t get awarded to affiliate. In addition later renewal payments of such subs are not being awarded to affiliate either. It seems that the upgrade breaks the link.

    I thought the way WPAM tracks renewal payments is this:

    WPAM at some point of time adds a post meta data (‘_wpam_id’) on the original (parent) order of the subscription. This way for renewals, the WPAM addon for subscription integration would check this piece of data, instead of the cookie (‘wpam_id’) to verify that the purchase is linked to the affiliate.

    Now the switch/upgrade doesn’t really change the parent order of the sub. So the addon should be able to find the original parent order, find the post meta data, and recognize such upgrades, and later renewals and find the link to the affiliate. But obviously the above understanding is not accurate as the upgraded subs were unlinked from the affiliate in my test.

    Could you help me correct the above understanding? Is this considered a bug and would be fixed in the next releases? In case the fix is going to take time, what pointers can you give me so I could temporarily fix it on my site before the official fix is in place?

    Thanks,
    Jim

    May 30, 2018 at 3:45 am #3652
    Shadow Labs
    Keymaster

    Hi, We sent you an email 6 days ago to test our WooCommerce subscription integration add-on. Can you please reply to that email?

    May 30, 2018 at 6:08 am #3653
    Jim
    Participant

    Hi Shadow Labs,

    I replied to an email on 5/25, and also got a reply from mbrsolution as well. I hope this is the one you are referring to. Let me know if we are not talking about the same message.

    I’m very happy with the timely response on the updated addon. I thank you guys for doing a great job!

    The issue/question I raised in the above message may or may not be related to the addon though. It might be within the WPAM itself.

    Thanks again!
    Jim

    May 31, 2018 at 7:39 am #3655
    Shadow Labs
    Keymaster

    Thank you. The plugin only checks for the cookie when the first payment of a subscription is charged. For renewal payments, it checks the order metadata to see if the subscription was referred by an affiliate.

    May 31, 2018 at 9:16 pm #3657
    Jim
    Participant

    Hi Shadow Labs,

    Thanks for your response!

    I did some more digging, looks like the addon correctly identified the Switch Subscription/Upgrade order. But somehow the award is never showing up on the affiliate’s account after it says “recurring payemnt api call” as the last one in the below log message.

    Something seems to be missing here.

    Could you please look into this?
    Thanks,
    Jim

    [05/31/2018 9:04 PM] – SUCCESS : WooCommerce Integration – Saving wpam_id (1) with order. Order ID: 1914
    [05/31/2018 9:04 PM] – SUCCESS : WooCommerce Integration – Order processed. Order ID: 1914
    [05/31/2018 9:04 PM] – SUCCESS : WooCommerce Integration – Checking if affiliate commission needs to be awarded.
    [05/31/2018 9:04 PM] – SUCCESS : WooCommerce Integration – Order CPT name: %e8%ae%a2%e5%8d%95-may-31-2018-0904-pm
    [05/31/2018 9:04 PM] – SUCCESS : WooCommerce Integration – Order status: pending
    [05/31/2018 9:04 PM] – NOTICE : WooCommerce Integration – Order status for this transaction is not in a ‘completed’ or ‘processing’ state. Commission will not be awarded at this stage.
    [05/31/2018 9:04 PM] – NOTICE : WooCommerce Integration – Commission for this transaction will be awarded when you set the order status to completed or processing.
    [05/31/2018 9:04 PM] – SUCCESS : WooCommerce Integration – Order processed. Order ID: 1914
    [05/31/2018 9:04 PM] – SUCCESS : WooCommerce Integration – Checking if affiliate commission needs to be awarded.
    [05/31/2018 9:04 PM] – NOTICE : WooCommerce Integration – This is a subscription payment order since the _subscription_switch_data meta is set.
    [05/31/2018 9:04 PM] – NOTICE : The commission will be calculated via the recurring payemnt api call.

    June 3, 2018 at 8:42 am #3669
    Shadow Labs
    Keymaster

    Can you please explain how you set up the subscription and the upgrade process in WooCommerce?

    June 3, 2018 at 7:20 pm #3670
    Jim
    Participant

    Variable subscriptions are setup this way on our site:
    Several levels of subscriptions are setup based on data limits: level 0-4;
    Each subscription is variable recurring monthly, or yearly

    Users can choose say the level 1 subscription and select monthly. Later they might want to choose to upgrade to the more expensive level 2. They’ll go to their account page under Subscriptions to pick this current subscription, and click the “Switch Subscription” button and then choose level 2 subscription, pay the gap payment, and the upgraded subscription would take effect immediately.

    Site is setup to allow upgrades/downgrades, but only prorate upgrade, which means upgrade takes effect immediately but downgrade won’t happen until the next payment date of the current subscription.

    I hope this helps.

    June 3, 2018 at 7:26 pm #3671
    Jim
    Participant

    Those switchable subscriptions are in the same group to allow them to switch. Here’s the documentation: https://docs.woocommerce.com/document/subscriptions/switching-guide/

    June 9, 2018 at 1:34 am #3678
    Jim
    Participant

    Any findings?

    June 14, 2018 at 11:57 pm #3693
    Jim
    Participant

    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);
    }

    June 15, 2018 at 6:14 am #3694
    Shadow Labs
    Keymaster

    Thank you for providing the fixes. We will include it in our integration addon in the next release.

  • Author
    Posts
Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.
Log In

Featured Addons

  • WooCommerce Coupon Tracking
  • WooCommerce Product Specific Commission

Copyright © 2026 | WordPress Affiliate Manager | Privacy Policy