WooCommerce (WordPress module) integration
A free ecommerce plugin that allows you to sell anything.
This integration with WooCommerce is really easy as you can connect the two using the WordPress plugin. When installed, configure it to define what do you want to track and how.
You can track per product commissions, order ID and coupons.
If you want to (or have to) integrate WooCommerce manually, here’s a guide.
Order tracking
To track whole order, use the following code. Edit file wp-content/plugins/woocommerce/templates/checkout/thankyou.php and place the code below line:
<?php do_action( 'woocommerce_thankyou', $order->id ); ?>
<script id="pap_x2s6df8d" src="https://URL_TO_PostAffiliatePro/scripts/trackjs.js" type="text/javascript"></script>
<script type="text/javascript">
PostAffTracker.setAccountId('Account_ID');
var sale = PostAffTracker.createSale();
sale.setTotalCost('<?php echo ($order->order_total - $order->order_shipping); ?>');
sale.setOrderID('<?php echo $order->id; ?>');
sale.setCurrency('<?php echo $order->get_order_currency(); ?>');
PostAffTracker.register();
</script>
Note:
In case you use a custom theme for WooCommerce and the thankyou.php file is located there as well (/themes/your_custom_theme/woocommerce/checkout/thankyou.php), you have to integrate it instead of the default one.
If you want more detailed tracking, follow the next step instead of this one.
If you wish to take use of Lifetime Commissions plugin then right above:
PostAffTracker.register();
add this:
sale.setData1('<?php echo $order->billing_email; ?>');
or this:
sale.setData1('<?php echo $order->user_id; ?>');
Per product integration
If you want to track commissions per product, use this code instead the one form step 1. You have to place it to the same file, at the same place:
wp-content/plugins/woocommerce/templates/checkout/thankyou.php
right below this line:
<?php do_action( 'woocommerce_thankyou', $order->id ); ?>
<script id="pap_x2s6df8d" src="https://URL_TO_PostAffiliatePro/scripts/trackjs.js" type="text/javascript"></script>
<script type="text/javascript">
PostAffTracker.setAccountId('Account_ID');
<?php
$i = 0;
foreach ($order->get_items() as $item) {
$itemprice = $item['line_total'];
$couponCode = '';
$_product = $order->get_product_from_item($item);
$p = $_product->id;
try { //if coupon has been used, set the last one in the setCoupon() parameter
$coupon = $order->get_used_coupons();
$couponToBeUsed = (count($coupon)>1 ? count($coupon)-1 : 0);
if (isset($coupon[$couponToBeUsed])) {
$itemcount = $order->get_item_count($type = '');
$orderdiscount = $order->get_order_discount();
if ($itemcount > 0) {
$discountperitem = $orderdiscount / $itemcount;
$itemprice = $item['line_total'] - $discountperitem;
}
$couponCode = $coupon[$couponToBeUsed];
}
}
catch (Exception $e) {
//echo "<!--Error: ".$e->getMessage()."-->";
}
if (!empty($_product->sku)) {
$p = $_product->sku;
}
echo "var sale".$i." = PostAffTracker.createSale();";
echo "sale".$i.".setTotalCost('".$itemprice."');";
echo "sale".$i.".setOrderID('".$order->id."($i)');";
echo "sale".$i.".setProductID('".$p."');";
echo "sale".$i.".setCurrency('".$order->get_order_currency()."');";
echo "sale".$i.".setCoupon('".$couponCode."');";
$i++;
}
?>
PostAffTracker.register();
</script>
Save your changes. That’s it, your shop has been integrated.
If you want to take use of Lifetime Commissions plugin, then right below:
echo "sale".$i.".setCoupon('".$couponCode."');";
add this:
echo "sale".$i.".setData1('".$order->billing_email."');";
or this:
echo "sale".$i.".setData1('".$order->user_id."');";
Integration of PayPal module in WooCommerce – part 1
Find and edit the following file:
- for WooCommerce 2.2.11 and lower: woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.php
- for WooCommerce 2.3.3 and higher: woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php
You can edit the plugin files either using the Plugin editor in WordPress or you can access the files of your WP installation via FTP.
In the above mentioned file find the function get_paypal_args() function and within it find a line with:
'return' =>
and a line with:
'notify_url' =>
Change the found lines by adding a special code:
'return' => esc_url( add_query_arg( 'utm_nooverride', '1', $this->get_return_url( $order ) ) ).'&paypal=1', // Post Affiliate Pro integration snippet
'notify_url' => $this->notify_url.'?pap_custom='.$_POST['pap_custom'], // Post Affiliate Pro integration snippet
Integration of PayPal module in WooCommerce – part 2
- for WooCommerce 2.2.11 and lower: in the same file (woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.php), find function check_ipn_request_is_valid and into that function below the line of function check_ipn_request_is_valid add the code found in the box below
- for WooCommerce 2.3.3 and higher: find and edit the file of (woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-ipn-handler.php) and in that file find the validate_ipn() function. Within that function find the following line:
if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) {
and right below that line add the code found in the box below:
/* Post Affiliate Pro integration snippet */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://URL_TO_PostAffiliatePro/plugins/PayPal/paypal.php?pap_custom='.$_GET['pap_custom']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
curl_exec($ch);
/* /Post Affiliate Pro integration snippet */
Save your work and continue with the next step.
Integration of PayPal module in WooCommerce – part 3
Edit the file: woocommerce/templates/checkout/form-checkout.php.
Find this line:
<?php do_action( 'woocommerce_checkout_order_review' ); ?>
and place this code below the line:
<!-- Post Affiliate Pro integration snippet -->
<input type="hidden" name="pap_custom" value="" id="pap_dx8vc2s5">
<script id="pap_x2s6df8d" src="https://URL_TO_PostAffiliatePro/scripts/notifysale.php" type="text/javascript">
</script>
<!-- /Post Affiliate Pro integration snippet -->
Integration of PayPal module in WooCommerce – part 4
If you only use PayPal as your payment integration, you can ignore this step.
To make sure the integration does not create duplicate orders (one from PayPal and one from the thank you page), we have to set the thank you page to ignore commissions for PayPal orders.
To do so, you have to modify the thank you page tracking code a little. First, edit the file woocommerce/templates/checkout/thankyou.php and find this line:
<?php do_action( 'woocommerce_thankyou', $order->id ); ?>
Place this code below that line:
<?php
if (empty($_GET['paypal'])) :
?>
Now, below the last integration lines:
PostAffTracker.register();
</script>
Add this code:
<?php endif; ?>
That’s it. Save your work and the very last step is to activate your PayPal IPN handling plugin in your Post Affiliate Pro.
Integration with Stripe
If you want to integrated Stripe recurring payments, you can do it in few small steps. First of all, you should have followed the step 1 or 2 and also using the customer email in data1:
sale.setData1('<?php echo $order->billing_email; ?>');
To make Stripe let your PAP know about a recurring payment, you have to add a new webhook in your Stripe account, Your Account (top right)> Account Settings> Webhooks … use this URL:
https://URL_TO_PostAffiliatePro/plugins/Stripe/stripe.php
Besides this, you have to enable (and configure) the Stripe plugin and Lifetime Commissions plugin.
Do not forget to insert the click tracking code to your WP header or footer file, using Appearance menu.
NOTE:
In case the sale tracking does not work for you event the sale tracking code is at it’s place, check if there is a special directory for woocommerce in your custom theme. If you found it, just integrate the /checkout/thankyou.php file there.