React JS router not working on Nginx docker container
Problem Statement I developed a simple ReactJS application where I have used…
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.
Problem Statement I developed a simple ReactJS application where I have used…
I was using On page optimization of the article pages, and found that meta…
Bootstrap has a simple solution to have sticky blocks in your html. I’ve given a…
Introduction I had to write a CICD system for one of our project. I had to…
Introduction Consider a scenario where you are building a docker image on your…
Being a drupal user from last around 5 years, I used to know small codes for…
Introduction In this post we will see following: How to schedule a job on cron…
Introduction There are some cases, where I need another git repository while…
Introduction In this post, we will see how to fetch multiple credentials and…
Introduction I have an automation script, that I want to run on different…
Introduction I had to write a CICD system for one of our project. I had to…
Introduction Java log4j has many ways to initialize and append the desired…