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 .= ''; esc_html_e('settings saved.', 'affiliatly' ); echo '