An Effective GIT Branching Strategy
Its essential to prepare a git branching strategy. This helps greatly in…
May 13, 2020
Assume you have a drupal website and using cloudflare. You are having an Amazon affiliate drupal content type, and you are collecting ASIN code of two countries:
Now you want to display only one link of amazon which will redirect to country specific website. i.e. either amazon.com or amazon.in Lets see how you can do it in Drupal.
I did this code because I have an amazon affiliate website, and I want to redirect in respective country. As, there is no point of sending an American to amazon.in
There are drupal modules available which can help you detecting country where your website is opening. Some of good modules are:
The problem with these module is that they will ask you to download big databases and then query from that. And, manytimes they say that the IP is not associated with any country. And, they are a bit complex to setup.
I did not use them at all.
Cloudflare is an excellent CDN, which gives an awesome free plan. When you connect your website through cloudflare. Cloudflare will send some header information to your server. See cloudflare article for http headers, what they send.
To detect country, they send a header name: CF-IPCountry.
# if your website is opening in USA, the header value is:
CF-IPCountry: US
# for india
CF-IPCountry: IN
I will use this information to redirect to country specific site.
For this, I defined a controller in my custom module. Note: With Controllers you can define an URL which acts as orchestrator to your controller code.
I will be doing following steps:
In your module base directory, create a file in src/Controller/AmazonLinkController.php
<?php
namespace Drupal\gyanblog\Controller;
use Drupal\Core\Controller\ControllerBase;
class AmazonLinkController extends ControllerBase {
public function redirectToAmazon() {
$india_code = "your affiliate code for india";
$usa_code = "your affiliate code for usa";
$india_asin = \Drupal::request()->get('i');
$usa_asin = \Drupal::request()->get('u');
$product_title = \Drupal::request()->get('t');
$country_code = $_SERVER['HTTP_CF_IPCOUNTRY'];
$amazon_link = "";
if ($country_code === 'IN') {
$amazon_link = "https://www.amazon.in/dp/" . $india_asin . "/?tag=" . $india_code;
}
else if ($country_code === 'US') {
if (isset($usa_asin)) {
$amazon_link = "https://www.amazon.com/dp/" . $usa_asin . "/?tag=" . $usa_code;
}
else {
$amazon_link = "https://www.amazon.com/s/?k=" . $product_title . "&tag=" . $usa_code;
}
}
else {
$amazon_link = "https://www.amazon.com/s/?k=" . $product_title . "&tag=" . $usa_code;
}
return new \Drupal\Core\Routing\TrustedRedirectResponse($amazon_link, 302);
}
}
There are some amazon specific code which opens up a search box if you are not passing any ASIN code of product. Lets leave that details in this article. In above code, I’m expecting few query parameters:
Lets say your module name is: gyanblog. Create a new file named: gyanblog.routing.yml, or edit if you already have such file.
gyanblog_amazon_link.controller:
path: '/recommend/amazon'
defaults:
_title: 'Amazon Buying Guide'
_controller: '\Drupal\gyanblog\Controller\AmazonLinkController::redirectToAmazon'
requirements:
_permission: 'access content'
In above yaml file, we defined a route url, and a handler function for that url. Permission is defined for all people having access content permission.
When you save this, and redirect users to this URL: /recomment/amazon, you will see that user will be redirected to always one URL. This is the problem caused due to caching.
Lets edit our routing file
gyanblog_amazon_link.controller:
path: '/recommend/amazon'
defaults:
_title: 'Amazon Buying Guide'
_controller: '\Drupal\gyanblog\Controller\AmazonLinkController::redirectToAmazon'
requirements:
_permission: 'access content'
options:
no_cache: 'TRUE'
Note the no_cache option above. This will configure no caching for this page. Which is what we require.
After saving this, goto your twig file. And, just put an a href tag, with target as _blank
{% raw %}
{% set product_title = "#{paragraph.getParentEntity().field_product_title.value}" %}
{% set india_asin = content.field_india_asin[0] | render|striptags|trim %}
{% if content.field_usa_asin[0] %}
{% set usa_asin = content.field_usa_asin[0] | render|striptags|trim %}
{% endif %}
{% set options = "/recommend/amazon?i=" ~ (india_asin) ~ "&t=" ~ (encoded_product_title) %}
{% if content.field_usa_asin[0] %}
{% set options = "/recommend/amazon?i=" ~ (india_asin) ~ "&u=" ~ (usa_asin) ~ "&t=" ~ (encoded_product_title) %}
{% endif %}
{# render link #}
<a class="btn btn-primary btn-lg active" href={{ options }} role="button" target="_blank">Buy on Amazon</a>
{% endraw %}
The above post focuses on how to detect country of user, and do something country specific. Other code assumes, you have certain knowledge of drupal code.
Let me know if you have some comments or query in comments box.
Its essential to prepare a git branching strategy. This helps greatly in…
Introduction I have a host running mysql (not on a container). I have to run an…
Introduction When I migrated all of my drupal-7 website to drupal-8, I wrote…
I wanted to fetch all image tags from a big html, and wanted to perform some…
I was testing a bug where a field was limited to 255 characters only. I needed…
Introduction Suppose you have a view, and you have configured your display as a…
In this post, we will see some of the frequently used concepts/vocabulary in…
System design interview is pretty common these days, specially if you are having…
Introduction You are given an array of integers with size N, and a number K…
Graph Topological Sorting This is a well known problem in graph world…
Problem Statement Given a Binary tree, print out nodes in level order traversal…
Problem Statement Given an array nums of n integers and an integer target, are…