Skip to content

Instantly share code, notes, and snippets.

@mitchkramez
Created July 31, 2015 17:02
Show Gist options
  • Save mitchkramez/b39bdf7eb59948dd01a2 to your computer and use it in GitHub Desktop.
Save mitchkramez/b39bdf7eb59948dd01a2 to your computer and use it in GitHub Desktop.
<?php
// ==============================================================================
// = MAGENTO MAGIC EMAIL IMPORTER =
// ==============================================================================
// this will save you so much time... you'll likely cry. instead of going one by
// one through each of the magento transactional email templates, this will go
// through each of the en_US templates in the magento/app/locale and import them
// into the database.
//
// You can use the prefix below to prefix you're entries with the name of your
// store to make them easier to find
ini_set('memory_limit', '1024M');
require_once dirname(__FILE__).'/../magento/app/Mage.php';
umask(0);
/* not Mage::run(); */
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$storeId = Mage::app()->getStore()->getId();
// Get a database object from Magento
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
// set the prefix for the magento email template name (Prefix - Template Name) to make them easy to find
$prefix = 'Prefix - ';
// add or modify paths to search for email templates (these are the defaults)
$paths = array(
dirname(__FILE__).'/../magento/app/locale/en_US/template/email/',
dirname(__FILE__).'/../magento/app/locale/en_US/template/email/sales/'
);
// =====================
// = end configuration =
// =====================
?>
<style type="text/css" media="screen">
.magento-email {
width:800px;
margin:0 auto 60px;
border:1px dotted #ccc;
padding:20px;
}
.headers {
background:#f7f7f7;
padding:10px;
border-bottom:1px solid #f0f0f0;
}
.headers p {
margin:0;
padding:0;
}
.headers strong {
float:left;
width:140px;
}
.debug {
border:1px solid #ccc;
background:#f0f0f0;
padding:10px;
font-size:10px;
line-height:12px;
color:#cc0000;
font-weight:bold;
}
h1 {
text-align:center;
margin:20px 0;
}
</style>
<h1>Magento Email Importer</h1>
<?
foreach($paths as $path) {
if ($files = scandir($path)) {
foreach ($files as $file) {
if ($file != "." && $file != ".." && !is_dir($path.$file)) {
$myfile = file_get_contents($path.$file);
preg_match('/<!--@subject (.*?)@-->/', $myfile, $matches);
$template = $prefix.ucwords(str_replace('_',' ',str_replace('.html','',$file)));
$subject = trim($matches[1]);
$body = preg_replace('/<!--@subject (.*?)@-->/','', $myfile);
?>
<div class="magento-email">
<div class="headers">
<p><strong>Path:</strong> <?= $path ?></p>
<p><strong>Template Name:</strong> <?= $template ?></p>
<p><strong>Subject:</strong> <?= $subject ?></p>
</div>
<p><?= $body ?></p>
<div class="debug">
<pre><?
$query = "INSERT INTO magento_core_email_template SET
`template_code` = ".$db->quote($template).",
`template_text` = ".$db->quote($body).",
`template_type` = '2',
`template_subject` = ".$db->quote($subject).",
`added_at` = NOW(),
`modified_at` = NOW()
";
$db->query($query);
print mysql_error();
?></pre>
</div>
</div>
<?
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment