get_last_order( 'all', 'any' ); $id_order = $last_order->id; $items = $subscription->get_items(); foreach( $items as $v ) { $product_variation_id = $v['variation_id']; // Checks if product has variation. if ( true == $product_variation_id ) { $product = new WC_Product($v['variation_id']); } else { $product = new WC_Product($v['product_id']); } // Gets the SKU code $sku_code = $product->get_sku(); // line_subtotal is including the full not discounted price, line_total is the discounted $skus[] = array( 'id'=>$sku_code, // sku 'quantity'=>$v['qty'], 'price'=>round( $v['line_total']/$v['qty'], 2 ) ); $price_order += $v['line_total']; } $coupons = $subscription->applied_coupons; if( is_array( $coupons ) ) { $coupons = implode(',', $coupons); } $customer_email = $subscription->get_user(); $customer_email = $customer_email->data->user_email; //=== $renewed_id_order = $id_order; require_once( AFFILIATLY_PLUGIN_DIR.'api_affiliatly.php' ); affiliatly_mark_purchase($affiliatly_admin_code, $renewed_id_order, $price_order, $affiliatly_security_hash, $coupons, $skus, $customer_email); return true; } /* * gathers the required order info and sends the data to the Affiliatly server * triggered when new order is completed in WooCommerce * * @version 3.2.0 * @since 3.2.0 * * @return type Bool. */ function affiliatly_loader_new_order($id_order) { $curl = extension_loaded( 'curl' ); if( false == $curl ) { return false; } $affiliatly_admin_code = get_option( 'affiliatly_admin_code' ); $affiliatly_security_hash = get_option( 'affiliatly_security_hash' ); //==== gets ID order and data $order = new WC_Order( $id_order ); global $woocommerce; $cart = $woocommerce->cart; $get_order_data_details = $order->get_data(); if( 'subscription' == $get_order_data_details['created_via'] || ( isset( $get_order_data_details['parent_id'] ) && $get_order_data_details['parent_id'] > 0 ) ) { return false; } $skus = array(); foreach($cart->cart_contents as $v) { $product_variation_id = $v['variation_id']; // Check if product has variation. if ( true == $product_variation_id) { $product = wc_get_product( $product_variation_id ); } else { $product = wc_get_product( $v['product_id'] ); } // Get SKU $sku_code = $product->get_sku(); // line_subtotal is including the full not discounted price, line_total is the discounted $skus[] = array( 'id'=>$sku_code, // sku 'quantity'=>$v['quantity'], 'name'=>$product->get_title(), 'price'=>round( $v['line_total']/$v['quantity'], 2 ) ); } $coupons = $cart->applied_coupons; if( is_array($coupons) ) { $coupons = implode(',', $coupons); } $customer_email = $order->get_billing_email(); // this price returns the subtotal minus discount minus shipping $price_order = $cart->cart_contents_total; // recalculates the price if the plugin Currency Switcher Pro for WooCommerce is enabled if ( function_exists( 'alg_convert_price' ) == true ) { $price_order = alg_convert_price( array('price'=>$price_order, 'currency_from'=>alg_get_current_currency_code(), 'currency'=>get_option('alg_currency_switcher_currency_shop_default'), 'format_price'=>'no' ) ); $price_order = round($price_order, 2); } require_once( AFFILIATLY_PLUGIN_DIR.'api_affiliatly.php' ); affiliatly_mark_purchase( $affiliatly_admin_code, $id_order, $price_order, $affiliatly_security_hash, $coupons, $skus, $customer_email ); // mark as incomplete $result = affiliatly_order_status( $affiliatly_admin_code, $id_order, 2, $affiliatly_security_hash ); return true; } /* * sends the order status ID to Affiliatly, the ID is depending on the status in WooCommerce * triggered when the status of an order in WooCommerce is changed * * @version 3.2.0 * @since 3.2.0 * * @return type Bool. */ function affiliatly_loader_order_changed( $id_order ) { $curl = extension_loaded( 'curl' ); if( false == $curl ) { return false; } $order = new WC_Order($id_order); // get the status $data = $order->get_data(); $order_status = $data['status']; $numeric_status = 0; if( 'completed' == $order_status ) $numeric_status = 1; elseif( 'refunded' == $order_status ) $numeric_status = 4; elseif( 'failed' == $order_status ) $numeric_status = 2; elseif( 'cancelled' == $order_status ) $numeric_status = 3; $affiliatly_admin_code = get_option( 'affiliatly_admin_code' ); $affiliatly_security_hash = get_option( 'affiliatly_security_hash' ); require_once( AFFILIATLY_PLUGIN_DIR.'api_affiliatly.php' ); affiliatly_order_status( $affiliatly_admin_code, $id_order, $numeric_status, $affiliatly_security_hash ); return true; } /* * sends the order refund data to Affiliatly when there is a partial refund in WooCommerce * differs from standard refunds, as it deducts only the price of the refunded product * * @version 3.2.0 * @since 3.2.0 * * @return type Bool. */ function affiliatly_loader_order_refunded( $id_order ) { $curl = extension_loaded('curl'); if( false == $curl ) { return false; } $order = new WC_Order( $id_order ); $order_total = $order->get_total(); $order_total_refunds = $order->get_total_refunded(); $exchange_rate = 1; // gets the status $data = $order->get_data(); $order_status = $data['status']; if( 'refunded' == $order_status ) { // the refund status change will be handled by affiliatly_loader_order_changed() return true; } $price_order = $order->get_total(); $price_order = $price_order / $exchange_rate; $order_products = array(); $order_refunds = array(); // Iterating through each "line" items in the order foreach ( $order->get_items() as $item_id => $item ) { // Get an instance of corresponding the WC_Product object $product = $item->get_product(); $single_price = round( $item->get_total() / $item->get_quantity(), 2); $total_refunds = $order->get_total_refunded_for_item( $item_id ); $qty_refunds = $order->get_qty_refunded_for_item( $item_id ); if( !empty($total_refunds) && $qty_refunds < 0 ) { $order_refunds[$item_id] = array( 'id'=>$product->get_sku(), 'price'=>$single_price, 'qty_refunded'=>$qty_refunds, ); } $order_products[$item_id] = array( 'id'=>$product->get_sku(), 'name'=>$product->get_title(), 'price'=> $single_price, 'quantity'=> $item->get_quantity(), ); } $data = array( 'id_order'=>$id_order, 'order_price'=>$price_order, 'refunds_total'=>$order_total_refunds, 'refunds'=>$order_refunds, 'skus'=>$order_products ); // prepares the account data for the call to Affiliatly's server $affiliatly_admin_code = get_option( 'affiliatly_admin_code' ); $affiliatly_security_hash = get_option( 'affiliatly_security_hash' ); // gets the custom order number $id_order = $order->get_order_number(); require_once( AFFILIATLY_PLUGIN_DIR.'api_affiliatly.php' ); affiliatly_price_update( $affiliatly_admin_code, $id_order, $data, $affiliatly_security_hash ); return true; } /* gets the discount code from the URL and applies it to the customer session * * @version 3.3.1 * @since 3.3.0 * * @return type bool. */ function affiliatly_discount_link() { $affiliatly_discount_link_status = get_option( 'affiliatly_discount_link_status' ); $affiliatly_discount_url_parameter = get_option( 'affiliatly_discount_url_parameter' ); // checks if this functionality is enabled if( '0' == $affiliatly_discount_link_status ) { return false; } // gets the discount code from the URL $coupon_code = isset($_GET[$affiliatly_discount_url_parameter]) ? sanitize_text_field( $_GET[$affiliatly_discount_url_parameter] ) : false; if( false == $coupon_code || empty( $coupon_code ) ) { return false; } // checks and creates the customer session if empty if ( ! WC()->session->has_session() ) { WC()->session->set_customer_session_cookie( true ); } // sets the used coupon code in a session WC()->session->set( 'affiliatly_custom_discount', $coupon_code ); } /* gets the discount code from the customer session and applies it to the cart * * @version 3.3.1 * @since 3.3.1 * * @return type bool. */ function affiliatly_apply_discount_code() { $coupon_code = WC()->session->get( 'affiliatly_custom_discount', null ); // checks if there is set coupon code if( empty($coupon_code) ) { return false; } // checks if the coupon code is already applied if ( WC()->cart->has_discount( $coupon_code ) ) { return true; } // applies the coupon code WC()->cart->apply_coupon( $coupon_code ); } /* * checks if the required extensions are loaded * * @version 3.2.0 * @since 3.2.0 * * @return type String. */ function affiliatly_check_requirements() { $curl = extension_loaded('curl'); // Checks if WooCommerce plugin is actived $woo = array_search( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ); if( false == $woo ) // check one by one { $all = apply_filters( 'active_plugins', get_option( 'active_plugins' ) ); foreach($all as $k=>$v) { if( strpos( $v, 'woocommerce.php' ) ) { $woo = true; break; } } } $html = ''; $error = array(); if( false == $curl ) { $error[] = '

This plugin needs CURL extension to be enabled, please contact your hosting provider and enabled it!

'; } if( false == $woo ) { $error[] = '

This plugin needs WooCommerce to be active!

'; } if( ! empty( $error ) ) { $html .= '
'; $html .= implode(', ', $error); $html .= '
'; } echo esc_html( $html, 'affiliatly' ); } /* * shows a link to the plugin in the Wordpress left menu * * @version 3.2.0 * @since 3.2.0 */ function affiliatly_options_menu() { add_plugins_page( 'Affiliatly Options', 'Affiliatly Integration', 'manage_options', 'affiliatly_options', 'affiliatly_plugin_options' ); } /* * ouputs the settings page of the plugin * for complete integration we need the Affiliatly ID and Security hash, both found in page Account in the Affiliatly admin panel * * @version 3.3.0 * @since 3.2.0 * * @return type Bool. */ function affiliatly_plugin_options() { if ( true == !current_user_can( 'manage_options' ) ) { wp_die( esc_html_e( 'You do not have sufficient permissions to access this page.', 'affiliatly' ) ); } // checks the requirements for the plugin affiliatly_check_requirements(); $hidden_field_name = 'affiliatly_options'; $affiliatly_admin_code_name = 'affiliatly_admin_code'; $affiliatly_security_hash_name = 'affiliatly_security_hash'; // discount link options $affiliatly_discount_link_status_name = 'affiliatly_discount_link_status'; $affiliatly_discount_url_parameter_name = 'affiliatly_discount_url_parameter'; if( true == check_admin_referer() && isset( $_POST[ $hidden_field_name ] ) && 'Y' == $_POST[ $hidden_field_name ] ) { $opt_admin_code_val = isset( $_POST[ $affiliatly_admin_code_name ] ) ? sanitize_text_field( wp_unslash ( $_POST[ $affiliatly_admin_code_name ] ) ) : ''; $opt_admin_code_val = esc_attr( $opt_admin_code_val ); $opt_security_hash_val = isset( $_POST[ $affiliatly_security_hash_name ] ) ? sanitize_text_field( wp_unslash ( $_POST[ $affiliatly_security_hash_name ] ) ) : ''; $opt_security_hash_val = esc_attr( $opt_security_hash_val ); $affiliatly_discount_link_status_val = isset( $_POST[ $affiliatly_discount_link_status_name ] ) ? sanitize_text_field( wp_unslash ( $_POST[ $affiliatly_discount_link_status_name ] ) ) : ''; $affiliatly_discount_link_status_val = esc_attr( $affiliatly_discount_link_status_val ); $affiliatly_discount_url_parameter_val = isset( $_POST[ $affiliatly_discount_url_parameter_name ] ) ? sanitize_text_field( wp_unslash ( $_POST[ $affiliatly_discount_url_parameter_name ] ) ) : ''; $affiliatly_discount_url_parameter_val = esc_attr( $affiliatly_discount_url_parameter_val ); update_option( $affiliatly_admin_code_name, $opt_admin_code_val ); update_option( $affiliatly_security_hash_name, $opt_security_hash_val ); update_option( $affiliatly_discount_link_status_name, $affiliatly_discount_link_status_val ); update_option( $affiliatly_discount_url_parameter_name, $affiliatly_discount_url_parameter_val ); // Put an settings updated message on the screen echo '

'; esc_html_e('settings saved.', 'affiliatly' ); echo '

'; } // Reads the existing option value from the database $opt_admin_code_val = get_option( $affiliatly_admin_code_name ); $opt_security_hash_val = get_option( $affiliatly_security_hash_name ); $affiliatly_discount_link_status_val = get_option( $affiliatly_discount_link_status_name ); $affiliatly_discount_url_parameter_val = get_option( $affiliatly_discount_url_parameter_name ); if( empty($affiliatly_discount_url_parameter_val) ) { $affiliatly_discount_url_parameter_val = 'coupon'; } echo '
'; echo '
'; echo ''; ?>

Your Affiliatly admin code:
You can find your code, when you login into your account at Affiliatly.com and check tab "Integration", it should be something like AF-XXXXX

Your Affiliatly secure hash:
You can find your code, when you login into your account at Affiliatly.com and check tab "Account", section Security hash

Affiliatly Discount Link: />
Enable the discount link functionality. This functionality will enable your affiliates to use a discount tracking links which will automatically apply their discount codes in your store. The discount URL will be something like https://www.your-store.com?aff=xx&coupon=yy. You can change "coupon" to something else in the field below.
Note: You will also need to enable the discount link in the Settings page of the Coupon code tracking method in your Affiliatly admin panel

Affiliatly Discount Link Tracking Parameter Name:
Enter the URL parameter which will be used for the discount link, we recommend using "coupon".
Note: You will also need to set the same parameter name in option "Affiliatly Discount Link Tracking Parameter Name" in your Affiliatly admin panel


'; echo '
'; echo '
'; return true; } // sends a new order notification to Affiliatly's server add_action( 'woocommerce_new_order', 'affiliatly_loader_new_order', 1, 1 ); // updates the status in Affiliatly on status change in WooCommerce add_action( 'woocommerce_order_status_changed', 'affiliatly_loader_order_changed'); // updates the order price in Affiliatly when there is a partial refund update in WooCommerce add_action( 'woocommerce_order_refunded', 'affiliatly_loader_order_refunded'); // // sends a new order notification to Affiliatly's server when subscriptions is made in WooCommerce Subscriptions add_action( 'woocommerce_subscription_renewal_payment_complete', 'affiliatly_loader_subscription_renewal_complete'); // outputs the JavaScript visitors tracking code in the Wordpress front site's footer add_action('wp_footer', 'affiliatly_output_js'); // shows the link to the plugin's settings page in Wordpress add_action('admin_menu', 'affiliatly_options_menu' ); // applies a coupon code from the URL add_action( 'wp_footer', 'affiliatly_discount_link' ); // gets the coupon code from the URL add_action( 'woocommerce_init', 'affiliatly_discount_link' ); // add coupon code when a product is added to cart once add_action('woocommerce_add_to_cart', 'affiliatly_apply_discount_code' ); ?>