Tracking Conversions in WooCommerce with Simple Link Codes the Easy Way & More

woocommerce-tracking-code-plugin-simpleI wrote about this as I was building it out.  It’s evolved a bit and is now on the WordPress Plugin Repository.

It’s simple.  You setup codes you want to track for conversions in WooCommerce.  If you have an email blast coming up then just create a code for it and attach that code to the end of all links in the email blast.  This works for affiliates, whatever.

The link looks like this:

Of course you can replace “testcode” with anything you want and have as many tracking codes as you’d like.  Then in the admin, you input the code you want to see the results of.  You get a total and # of conversions.  I have every intention of adding more in depth analytics but this is a proof of concept.

Passwords to Conversions Analytics

What I encountered the following week at work presented a new problem.  We are starting a site for retailers and professionals in our industry.  We sell a specialized tool only to licensed professionals and we want them to be able to carry our consumer product on their shelves, so they need to be able to buy at a discounted, bulk price.

Fortunately this site is on its own domain and I can make the functionality global.  So here’s what I needed to happen:

  • All shopping functionality should be hidden behind a password/access code that we hand out.
  • Different rep groups selling to professionals need to have their own code that can be tracked
  • We want to see total conversion for each code used

My thoughts lead to why not use the existing conversions plugin I built?  I’ll just hide all of WooCommerce behind a password wall.  Any password the admin creates can be used to access it, I just drop a cookie.

I also drop a cookie for the Quick Tracking Conversions plugin and voila, our shoppers have access and we can see what password/access code they are using to make their purchase.  The new extension can be found here:


Quick Campaign Conversion Tracking in WooCommerce

My company hired a marketing firm that is starting our Constant Contact campaigns amongst other things.  We want to track conversions and at this point we don’t need anything too fancy.  Between that, Facebook ads/posts, Google Analytics and our affiliates, there’s a lot of pixels, JavaScript and php functions that need to be added to our system.  It’s a bit overwhelming and each has its own installation instructions/methods.

Right now, we just want to see conversions.  We want to know what source provide how many purchases and at what value.

I searched for some quick methods and plugins that might help but again, too much complicated setup for simple conversion tracking.  So I built a thing.

Quick Tracking Conversions for WooCommerce

I envisioned a super simple system where you can take on a code to the end of a link and all visits to your site with that code would be tracked and recorded when converted.  I wanted to start with simply recorded full, purchase conversions.

I didn’t want to have to setup a campaign with a code and details in an admin before generating links.  I want to just tell our marketing person put something like ?tracking_code=MyEmailCampaign on the end of all links in a Constant Contact.

So I built a plugin.  It’s light weight, secure and solid.  It does it’s job and it does it right.  You add whatever campaign code you want to links for Facebook, Email blasts, affiliates or anything at all that you want to track conversions from a link.  Here’s an example link that could be in an email:

After the email goes out and orders come in I can simply go to the admin page of the plugin, type in Oct2016-email-blast and submit the form.  It will show me number of conversions and total money generated so far.

How It Works

The plugin checks for $_GET[‘qtc_woo_tracking_code’] and sets its value in a cookie.  If the user converts it adds a post_meta to the order for that tracking code.

This allows me to pull all details of every order made.  We can get products purchases, shipping methods, totals, taxes and everything.

Here’s the repo if you want to check it out.  It’s simple right now and has Google Analytics Enhanced Ecommerce option you can enable as well.  There’s room to add all sorts of conversions such as product viewing, add to cart and so on.  This plugin will grow.

Beatrix Has A YouTube Channel Now

My daughter watches these crazy videos where it’s just someone opening up toys, or pulling them out of play dough, or little toy surprises out of plastic eggs.  It’s the weirdest thing.  Apparently kids can watch these for hours as they have millions and millions of views.  Anyway, she asked me if she could make some so why not!  Here’s her first too:


Getting Up To Speed With NativeScript 2.3, Xcode 8 and iOS 10

tns livesync ios --emulator --watch

Unsupported version Xcode 8

Oh no …

It’s That Time

The iPhone 7 is out along with iOS 10 and things have changed.  It hit me today when I was toying with my company’s app for the first time since upgrading Xcode.

So what do? Well, the first thing I needed to solve was getting my apps running in Simulator again.  I opened up Xcode and built my app in there instead of the command line tns.  I ran it on iPhone 7 Simulator and everything popped up and seemed to be working until it crashed right away on launch.

Upgrade NativeScript to “Next”

I uninstalled NS and the tns modules and reinstalled completely using the next version (2.4).

npm uninstall nativescript -g
npm uninstall tns-core-modules
npm install nativescript@next -g
npm install tns-core-modules@next

Then I removed the ios platform from my app and added it back.  Voila, things were loading up.  Thing is, I’m lying a little, I forgot to install the @next version of tns-core-modules and even though the app was opening certain things weren’t working like trying to set the src of an Image via javascript.

Properties Not Methods

So something big is happening.  Xcode is changing some big core items from using methods to initiate and instead using properties.  For example I use local-notifications plugin to send phone notifications.  I was erroring out with:

NSNotificationCenter.defaultCenter() is not a function

Apparently that is now a property so I had to adjust the module’s code in local-notifications.ios.js to


And it works just fine now.  Or so I believe, no more errors but haven’t tested it thoroughly. Permission Changes

Next the app crashes when I try to open the camera.  I’m getting errors saying there’s permission issues.  Turns out I need to have some new info in app/App_Resources/

<string>Photo Library Access Warning</string>

More To Come

This is all so far but I’ll be sure to add more information as I encounter it.


WooCommerce Per Product Shipping Price Without Needing Extension or Plugin

I’m a big fan of WooCommerce, seriously.  I hate how much they nickel and dime for small features like this though.  It’s easier for me to move past it because I’m able to do a lot of tweaking in the code with hooks to get what I need done thereby saving a few dollars.  But sometimes, like this example, you can do it with no coding at all.

The shipping system in general can be pretty confusing so I’m going to walk you through how you can have custom, flat rate shipping prices for different products.

Shipping Zones & Methods

Under WooCommerce Settings click the Shipping tab.  Here you need to add a new zone.  For my example I call it US Flat Rate with a Flat Rate shipping method.

Now the confusing interface part.  In order to set details for flat rate you need to click on the words “Flat Rate” in your newly saved Shipping Zone row.  Let’s not do that yet though, click over to “Shipping Classes” option on this page.

Shipping Classes

This is where we will create our per item shipping prices.  Let’s say we have three products: T-Shirt, Mug and Hat.  We are going to create three shipping classes.


Now repeat for the other two items.

Back to Shipping Zones

Bounce back over to shipping zones, click on the “Flat Rate” link on your new shipping class row and let’s do some pricing.

Set the main cost to $0.00 then for each shipping class add the price for that item.  It’s that simple.  Go to your T-Shirt product and under shipping select the T-Shirt class.  Now it will  have that flat price added to the product for shipping on checkout.

Remove Item from PayPal Invoice with the REST API

I’m building a pretty crazy system right now around the PayPal Invoice REST API.  I want the owners of the site to be able to adjust all sorts of stuff on invoices whether the status is DRAFT or SENT.  If PAID, no go.  I’m going to give them the option to copy and cancel the original if that’s possible but I haven’t dove that far in yet.

But what I did need at this point was for them to allow users and admins to adjust an invoice by removing items.  So I google searched without much luck then finally looked through the API’s code to find what I needed.

PayPal Invoice Class

Inside the class for Invoice.php is a method removeItem().  Well, I tried this and kept getting a 400 error with no description of why it wouldn’t work.  I was using a foreach loop.

foreach ( $invoice->items as $item ) {
    if( blah blah ) {
        $invoice->removeItem( $item );

This was just not working and there were no clues as to why.  I tried adding a count variable $cnt and using removeItem( $invoice->items[ $cnt ] ) but that was producing the same error.

Adjust The Quantity

So I finally settled on just changing the quantity to 0 and, low and behold, that worked just fine.  So end of day, the line item will still be there but you can set the quantity to 0 and it will charge nothing for it.  Final code was basically:

foreach ( $invoice->items as $item ) {
   if ( strpos( $item->name, 'Item ' . $id . ':' ) !== false ) {
      $item->setQuantity( 0 );
      $invoice->update( $api_context );

WooCommerce Subscriptions Limit Total Quantity of Subscriptions or Payment Plans in Cart

We use WooCommerce Subscriptions to setup payment plans for our products.  Deposits was our first choice but it only sent “Invoices” for future payments and did not auto charge the credit card used at initial purchase.  Not ok as our products are very expensive and people tended to ignore those invoices.

We also need to limit these purchase to one per customer.  Easier said than done.  I found a WooCommerce extension that would do the trick but we already pay so much a year for extensions we need I try to avoid that for simple functionality.

Limit Subscription

This won’t work for my needs.  The option on a subscription product only limits users from having multiple subscriptions to that particular product.  We only want one subscript for ANY product.

Solution Limits

My solution works for one shopping cart session.  I’m working on preventing others which shouldn’t be too hard by just checking the database at checkout time for active subscriptions to that user.

The Hook & Function

I went with woocommerce_add_to_cart_validation after going through quite a few other hooks and filters.  The hardest thing about finding a solution is finding what all hooks and filters are available.  Takes some serious google-fu to find the right one quickly.

This filter allows you to run a function before an item is validated and sent to the cart.  We tell our add_filter that we need two of the parameters so we can get the product id as well as the validity of the admission.  This is done with the 4th parameter we set to “2.”

Detect Variations Too

If you are using variable Subscriptions you will need to plan ahead that some items in the cart may be variations.  These have a separate class.  We must first detect if the product being added is an instance of WC_Product_Subscription which is a “Simple Subscription” in WooCommerce.  Or is an instance of WC_Product_Variable_Subscription which is like it says a version of a subscription product with variations.

The odd part that caught me off guard is when we are checking items already in the cart.  We check for WC_Product_Subscription like normal but once in the cart the product is of the WC_Product_Subscription_Variation class meaning it’s just a variation of a product and not the full product variable class.