WordPress – Google AdWords – Localized Information On Website

I created this plugin to be able to make a website look localized to the location defined on a Google AdWords or Facebook Ad Campaign. The plugin is able to detect the location based on the Final URL.  To Automate changing all of your Google Ads Tracking Templates take a look at this post.

To make this work you will need to define with Get Parameter the plugin will look at. For my case, it is the utm_campaign  in which it looks for the city name. An example campaign name would be Brandon School Campaign. The plugin would see the city name Brandon in the get parameter and change the information to be based on that city. This will allow you to essentially have multiple targeted/localized landing pages without actually having to create a new landing page for each location by dynamically switching the information.

<?php
/*
* Plugin Name: City Customizer ShortCode
* Description: This plugin will read get parameters and echo out the correct city information. Proper use is [grabCity output="localphone"] ouput can be any of the following, localphone, address, cityname
* Version: 1.2
* Author: Corey Jansen
* Author URI: https://coreyjansen.com
*/

//DEFINE CITY NAME AND CITY INFO
$defaultCityName = "Winnipeg";
$cityInfo = array(
	array(
		"cityname" => "Winnipeg",
		"localphone" => '<a href="tel:12045555555">(204) 555-5555</a>',
		"address" => '2080 Pembina Hwy, Winnipeg Manitoba R2E 1S5'
	) ,
	array(
		"cityname" => "Brandon",
		"localphone" => '<a href="tel:12045555555">(204) 555-5555</a>',
		"address" => '2080 Pembina Hwy, Winnipeg Manitoba R2E 1S5'
	) ,
	array(
		"cityname" => "Calgary",
		"localphone" => '<a href="tel:14035555555">(403) 555-5555</a>',
		"address" => '123 Fake Street, Calgary Alberta C2E 5S5'
	)
);



//MAIN FUNCTION THAT PULLS IN ATTRIBUTES FROM SHORTCODE AND RETURNS THE INFORMATION
function citycus_mainGetInformation($atts = [], $content = null, $tag = '')
{

	// ---------- START FUNCTION TO GET WHICH DATA WE WANT TO ECHO ----------
	// NORMALIZE ATTRIBUTE KEYS, LOWERCASE

	$atts = array_change_key_case((array)$atts, CASE_LOWER);

	// OVERRIDE DEFAULT ATTRIBUTES WITH USER ATTRIBUTES

	$wporg_atts = shortcode_atts(['output' => 'city', ], $atts, $tag);
	$output = esc_html__($wporg_atts['output']);


	$campaignName = citycus_getCityName();

	return citycus_getCityInfo($campaignName, $output);
}

//FUNCTION THAT PULLS THE CITY INFO BASED ON WHAT WAS DEFINED IN THE OUTPUT PARAMETER IN THE SHORT CODE
function citycus_getCityInfo($cityName, $returnInfo)
{
	global $cityInfo;
	foreach($cityInfo as $currentCity) {
		if ($currentCity['cityname'] == $cityName) {
			return $currentCity[$returnInfo];
		}
	}
}

// GETS CITY NAME FROM COOKIE IF SET. IF NOT GETS FROM GET PARAMETER AND
// SAVES TO COOKIE SO THAT THE PARAMETER INFORMATION IS STORED ON ALL PAGES
function citycus_getCityName()
{
	$campaignName = $_GET['utm_campaign'];
	$currentCityName = citycus_returnCityName($campaignName);
	$cookie_name = "WebsiteCampaignCity";

	// Check if cookie is set

	if (isset($_COOKIE[$cookie_name])) {
		return $_COOKIE[$cookie_name];
	}
	else {
		setcookie($cookie_name, $currentCityName, time() + (86400 * 30 * 7) , "/"); // 86400 * 7 = 7 DAYS
		return $currentCityName;
	}
}

// CHECKS GET PARAMETER AND RETURNS CITY NAME WILL RETURN DEFAULT IF NOT SET
function citycus_returnCityName($campaignName)
{
	foreach($GLOBALS['cityInfo'] as $currentCity) {
		$result = citycus_checkCityName($campaignName, $currentCity['cityname']);
		if ($result) {
			return $currentCity['cityname'];
		}
	}

	return $GLOBALS['defaultCityName'];
}
//CHECKS IF CITY NAME MATCHES
function citycus_checkCityName($campaignName, $cityName)
{
	if (strpos(strtolower($campaignName), strtolower($cityName)) !== false) {
		return true;
	}
	else {
		return false;
	}
}

//INITIATES SHORTCODE FUNCTION
function wpgrabCity_shortcodes_init()
{

	add_shortcode('grabCity', 'citycus_mainGetInformation');

}

add_action('init', 'wpgrabCity_shortcodes_init');


Continue Reading

WooCommerce – Differentiating WordPress New Order Notification Recipient Based on City and Province or State

WooCommerce New Order Notification

I have a  customer who when they get an e-commerce order through their website built on WordPress using WooCommerce they want the admin new order notification to be emailed to the branch that is closest to them. There wasn’t any solution I could find for this so I created this script to allow me to define a different email address for order notifications based on the customer’s province. For Alberta and Saskatchewan, my client has two locations in each of those provinces so if the user is from Alberta and Saskatchewan the script tries to determine if the user is in the city where the branch is located and return that email address. If the customer is not in those cities the script will return that provinces default email address.

Installation:

  1. Change the variables below to correlate with the states and/or provinces with defined email addresses between  //EMAIL ADDRESSES FOR EACH PROVINCE/CITY DEFINED START  and  //EMAIL ADDRESSES FOR EACH PROVINCE/CITY DEFINED END
  2. If you want to set specific city email addresses change variables under //IF USER PROVINCE IS SASKATCHEWAN DETERMINE IF CITY You can define these cities in the provinces variable
  3. Copy this code to the bottom of your functions.php file in your Child Theme directory

 

<?
add_filter('woocommerce_email_recipient_new_order', 'diff_recipients_email_notifications', 10, 2);

function diff_recipients_email_notifications($recipient, $order)
{
    $provinces = array();
    //EMAIL ADDRESSES FOR EACH PROVINCE/CITY DEFINED START
    $provinces["alberta"] = "[email protected]";
    $provinces["calgary"] = "[email protected]";
    $provinces["edmonton"] = "[email protected]";
    $provinces["saskatchewan"] = "[email protected]";
    $provinces["regina"] = "[email protected]";
    $provinces["saskatoon"] = "[email protected]";
    $provinces["ontario"] = "[email protected]";
    $provinces["quebec"] = "[email protected]";
    $provinces["british columbia"] = "[email protected]";
    $provinces["manitoba"] = "[email protected]";

    $provinces["default"] = "[email protected]";
     //EMAIL ADDRESSES FOR EACH PROVINCE/CITY DEFINED END
     
     
    //IF USER PROVINCE IS SASKATCHEWAN DETERMINE IF CITY
    //IS REGINA OR SASKATOON IF NOT DEFAULT TO PROVINCE EMAIL ADDRESS
    $user_state = strtolower($order->shipping_state);
    if (empty($user_shipping_country)) $user_state = strtolower($order->billing_state);
    if ($user_state == "saskatchewan")
    {
        if (strtolower($order->shipping_city) == "regina" || strtolower($order->shipping_city) == "saskatoon")
        {
            $user_state = strtolower($order->shipping_city);
        }
    }
    //IF USER PROVINCE IS ALBERTA DETERMINE IF CITY
    //IS CALGARY OR EDMONTON IF NOT DEFAULT TO PROVINCE 
    if ($user_state == "alberta")
    {
        if (strtolower($order->shipping_city) == "calgary" || strtolower($order->shipping_city) == "edmonton")
        {
            $user_state = strtolower($order->shipping_city);
        }
    }
    if (array_key_exists($user_state, $provinces))
    {
        $recipient = $provinces[$user_state];
    }
    else
    {
        $recipient = $provinces["default"];
    }
    //RETURN EMAIL ADDRESS
    return $recipient;
}
Continue Reading