New Shopify Certification now available: Liquid Storefronts for Theme Developers

Programmatically change logo file periodically

pramodraam
Shopify Partner
77 2 9

I just need some preliminary idea on how to programmatically change the logo of the site. For example, I want the site to change the logo of the site to a different one designed for Christmas. Once the Christmas is over, I want it to revert to the normal one. I don’t want to do it manually. We have had a cron job in our Magento site that would update the logo programmatically throughout the year. We now moved to Shopify.

 

I want to implement what the logo updater script below does in Shopify.

 

 
<?php
require_once 'includes/poms_config.php';
require_once 'poms/classes/db_mysql.php';
require_once 'includes/common_functions.php';
require_once 'libs/phpfastcache_config.php';
require_once 'includes/advanced_functions.php';
require_once "includes/autoload.php";

// Instantiate a database object/connection.
$dbh = new DB_Sql;		// Database handle.

// the image files are in public_html\utility\images AND THEY ARE NOT IN git
// switches the header logo on appropriate dates
$path = LUXE_HOME_ABSOLUTE_PATH . 'pub/media/logo/stores/1';

// Current file is one level below public_html
// the above was correct when it used realpath - realpath removes the final slash.

$today = date("md");
$currentYear = date("Y");
$day_of = "";

//  ----------------  changing-date holidays ------------------
// 3 weeks before Easter
$b4_easter = DateTime::createFromFormat('Y z', date("Y") . " " . strval(intval(date("z", easter_date())) - 21))->format('md');
if ($b4_easter <= "0318") {
	// prevents easter logo appearing before St. Patrick's day
	$b4_easter = "0319";
}

// Day after easter
function get_easter_datetime($year)
{
	$base = new DateTime("$year-03-21");
	$days = easter_days($year);

	return $base->add(new DateInterval("P{$days}D"));
}
$easter = str_pad(intval(date("md", easter_date())) + 1, 4, '0', STR_PAD_LEFT);
//$easter_day = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>',easter_date($currentYear));
$easter_day = get_easter_datetime($currentYear)->format('l, F j\<\s\u\p\>S\<\/\s\u\p\>');

// 3 weeks before Mother's Day
$b4_mothers = date('md', strtotime("-21 days second sunday of may $currentYear"));
$mothers = date('md', strtotime("+1 day second sunday of may $currentYear"));
$mothers_day = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("second sunday of may $currentYear"));

// 2 weeks before Memorial Day - last Monday in May
$b4_memorial = date('md', strtotime("-14 days last monday of may $currentYear"));

if ($b4_memorial == $mothers) {
	$b4_memorial = str_pad(intval($b4_memorial) + 1, 4, '0', STR_PAD_LEFT); // prevents overlap
}
$memorial = date('md', strtotime("+1 day last monday of may $currentYear"));
$memorial_day = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("last monday of may $currentYear"));

// 18 days before Father's Day - 3rd Sunday in June
$b4_fathers = date('md', strtotime("-18 days third sunday of june $currentYear"));
if ($b4_fathers == $memorial) {
	$b4_fathers = str_pad(intval($b4_fathers) + 1, 4, '0', STR_PAD_LEFT); // prevents overlap
}
$fathers = date('md', strtotime("+1 days third sunday of june $currentYear"));
$fathers_day = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("third sunday of june $currentYear"));

// 14 days before Labor Day  - 1st Monday in September
$b4_labor = date('md', strtotime("-14 days first monday of september $currentYear"));
$labor = date('md', strtotime("+1 days first monday of september $currentYear"));
$labor_day = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("first monday of september $currentYear"));

// 3 weeks before Thanksgiving
$b4_thanksgiving = date('md', strtotime("-21 days fourth thursday of november $currentYear"));
$thanksgiving = date('md', strtotime("+1 day fourth thursday of november $currentYear"));
$thanksgiving_day = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("fourth thursday of november $currentYear"));

// Black Friday Weekend - Friday, Saturday, Sunday
$b4_black_friday = date('md', strtotime("fourth friday of november $currentYear"));
$black_friday = date('md', strtotime("+3 day fourth friday of november $currentYear"));
$black_friday_day = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("fourth friday of november $currentYear"));

// Cyber Monday
$b4_cyber_monday = date('md', strtotime("+4 day fourth thursday of november $currentYear"));
$cyber_monday = date('md', strtotime("+5 day fourth thursday of november $currentYear"));  // sometimes there's not a Monday in November after Thanksgiving
$cyber_monday_day = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("+4 day fourth thursday of november $currentYear"));

//  ----------------  end changing-date holidays ---------------

//print("b4_easter = $b4_easter, easter = $easter, b4_mothers = $b4_mothers, mothers = $mothers, b4_memorial = $b4_memorial, memorial = $memorial, b4_fathers = $b4_fathers, fathers = $fathers, b4_labor = $b4_labor, labor = $labor, b4_thanksgiving = $b4_thanksgiving, thanksgiving = $thanksgiving, before_blackfriday = $b4_black_friday, black_friday = $black_friday, today = $today<br>");
//exit;

$source_file = "";          // By default, no file to be copied. Leave things as is.

switch ($today) {
	case "0102":
		day_after();
		break;
	case "0124":
		$source_file = "logoVALENTINE.png";
		$day_of = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("02/14/" . $currentYear));
		break;
	case "0215":
		day_after();
		break;
	case "0303":
		$source_file = "logoSTPATRICKS.png";
		$day_of = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("03/17/" . $currentYear));
		break;
	case "0318":
		day_after();
		break;
	case $b4_easter:
		$source_file = "logoEASTER.png";
		$day_of = $easter_day;
		break;
	case $easter:
		day_after();
		break;
	case "0331":
		$source_file = "logoAPRILFOOL.png";
		$day_of = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("04/01/" . $currentYear));
		break;
	case "0402":
		if ($easter > "0402") {
			$source_file = "logoEASTER.png";  // puts Easter logo back if Easter falls in April
			$day_of = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', easter_date());
		} else {
			day_after();
		}
		break;
	case $b4_mothers:
		$source_file = "logoMOTHERS.png";
		$day_of = $mothers_day;
		break;
	case $mothers:
		day_after();
		break;
	case $b4_memorial:
		$source_file = "logoMEMORIAL.png";
		$day_of = $memorial_day;
		break;
	case $memorial:
		day_after();
		break;
	case $b4_fathers:
		$source_file = "logoFATHERS.png";
		$day_of = $fathers_day;
		break;
	case $fathers:
		$source_file = "logoJULY4.png";
		$day_of = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("07/04 $currentYear"));
		break;
	case "0705":
		day_after();
		break;

	case $b4_labor:
		$source_file = "logoLABOR.png";
		$day_of = $labor_day;
		break;
	case $labor:
		day_after();
		break;
	case "1015":
		$source_file = "logoHALLOWEEN.png";
		$day_of = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("10/31 $currentYear"));
		break;
	case "1101":
		day_after();
		break;
	case $b4_thanksgiving:
		$source_file = "logoTHANKSGIVING.png";
		$day_of = $thanksgiving_day;
		break;
	case "1111":
		$source_file = "logoVETERANS.png";
		$day_of = $day_of = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("11/11 $currentYear"));
		break;
	case "1112":
		$source_file = "logoTHANKSGIVING.png";
		$day_of = $day_of = $thanksgiving_day;
		break;
	case $b4_black_friday:
		$source_file = "logoBLACKFRIDAY.png";
		$day_of = $black_friday_day;
		break;
	case $b4_cyber_monday:
		$source_file = "logoCYBER.png";
		$day_of = $cyber_monday_day;
		break;
	case $cyber_monday:
		$source_file = "logoCHRISTMAS.png";
		$day_of = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime("12/25 $currentYear"));
		break;
	case "1226":
		$source_file = "logoNEWYEAR2.png";
		$day_of = date('l, F j\<\s\u\p\>S\<\/\s\u\p\>', strtotime('01/01/' . (date("Y") + 1)));
		break;
}

if ($source_file != "") {
	if (copy($path . "/" . $source_file, $path . "/logo300.png")) {
		// We need to overwrite the logo file with the source file.
		// $day_of text is placed beneath the holiday logo in argento-custom.txt
		$file_name_path = LUXE_HOME_ABSOLUTE_PATH . 'pub/media/logo/logo_text.txt';
		if ($source_file !== "logoNORMAL.png") {
			$logo_day_txt = '<div style="font-size:.9em;margin-top:-4px">' . $day_of . '</div>';
			file_put_contents($file_name_path, $logo_day_txt);
		} else {
			// Need to ensure the date disappears once the holiday logo switches back to normal.
			// Delete the file.
			if (file_exists($file_name_path)) {
				unlink($file_name_path);
			}
		}
	} else {
		inform_sl_operations(
			"Logo $source_file could not be copied",
			get_system_info(SYSTEM_ADMIN_MAIL_ADDRESS_VAR),
			__FILE__ . ": Could not copy Logo"
		);
	}
}

// Update the Batch Timestamp to indicate Batch job done.
update_batch_job_run_timestamp(BATCH_JOB_LOGO_UPDATER);

function day_after()
{
	global $source_file;
	$source_file = "logoNORMAL.png";
}
Replies 4 (4)
alexpriftuli
Shopify Partner
10 1 3

Maybe instead of using a cronjob you can add the christmas version of the logo in the customizer, and you put a liquid if to check what date is today.
So for example you can display the christmas logo in december and all the other months the normal logo

pramodraam
Shopify Partner
77 2 9

Hi Alex, thanks for looking into my concern. My example is just the tip of the iceberg. I added the entire PHP script in my post that will change the logo for several holidays dates for which will vary. Please have a look. Thank you.

alexpriftuli
Shopify Partner
10 1 3

It's a bit longer but you can apply the same approach as I wrote before.


Or you can put all the logos together in a unique image. Then in the frontend, using css, you show only a part of the image and with js or liquid you play a bit to center the correct logo based on the period of the year.

sabbirshouvo
Shopify Partner
1 0 0

Well, I just read your entire `php` code. I will suggest you stick with liquid. It's more powerful than you think. Let me share a small example of how you may handle the scenario:

{% assign nowDate = "today" | date: "%m-%d" %}

<!-- Adjust dates: mm-dd -->
{% assign eventEasterStart = "03-18" %}
{% assign eventEasterEnd = "04-20" %}

<!-- Adjust dates: mm-dd -->
{% assign eventMothersStart = "03-18" %}
{% assign eventMothersEnd = "04-20" %}

<!-- Adjust dates: mm-dd -->
{% assign eventFathersStart = "03-18" %}
{% assign eventFathersEnd = "04-20" %}

<!-- Let's do the condition -->
{% if (nowDate | date_between: eventEasterStart, eventEasterEnd) %}
  Render Eater Logo
{% elsif (nowDate | date_between: eventMothersStart, eventMothersEnd) %}
  Render MothersDay Logo
{% elsif (nowDate | date_between: eventFathersStart, eventFathersEnd) %}
  Render FathersDay Logo
{% else %}
  Render Default Logo
{% endif %}

 

You still need to figure out how you can get relative dates like "2nd Sunday of month"