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 $requestTracker = new WPAM_Tracking_RequestTracker(); WPAM_Logger::log_debug('WooCommerce Subscription Integration - awarding commission for order ID: ' . $order_id . ', Purchase amount: ' . $purchaseAmount); $requestTracker->handleCheckoutWithRefKey($txn_id, $purchaseAmount, $wpam_refkey); } add_action('woocommerce_subscriptions_switch_completed', 'wpam_woocommerce_subscriptions_switch_completed', 20, 1); //Triggers when a subscription is upgraded/downgraded 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; WPAM_Logger::log_debug('WooCommerce Subscription Integration – checking all the subscriptions for order ID: '.$order->get_id()); 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 $requestTracker = new WPAM_Tracking_RequestTracker(); WPAM_Logger::log_debug('WooCommerce Subscription Integration – awarding commission for order ID: ' . $order_id . ', Purchase amount: ' . $purchaseAmount); $requestTracker->handleCheckoutWithRefKey($txn_id, $purchaseAmount, $wpam_refkey); }