<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NemoPS &#187; development</title>
	<atom:link href="http://nemops.com/tag/development-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://nemops.com</link>
	<description>Prestashop Tutorials, Modules and More!</description>
	<lastBuildDate>Wed, 05 Dec 2018 13:25:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.35</generator>
	<item>
		<title>Setting up a staging site for PrestaShop and Thirtybees</title>
		<link>http://nemops.com/staging-site-prestashop-thirtybees/</link>
		<comments>http://nemops.com/staging-site-prestashop-thirtybees/#comments</comments>
		<pubDate>Wed, 10 Jan 2018 08:18:49 +0000</pubDate>
		<dc:creator><![CDATA[Nemo]]></dc:creator>
				<category><![CDATA[Tips n Tricks]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[prestashop]]></category>
		<category><![CDATA[staging]]></category>
		<category><![CDATA[thirtybees]]></category>

		<guid isPermaLink="false">http://nemops.com/?p=3116</guid>
		<description><![CDATA[<p>It’s always better not to directly modify your live site, especially when core changes are involved. In this video, we will see how to create a development site for PrestaShop 1.6 and ThirtyBees Watch the screencast</p>
<p>The post <a rel="nofollow" href="http://nemops.com/staging-site-prestashop-thirtybees/">Setting up a staging site for PrestaShop and Thirtybees</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>It’s always better not to directly modify your live site, especially when core changes are involved. In this video, we will see how to create a development site for PrestaShop 1.6 and ThirtyBees<br />
<span id="more-3116"></span></p>
<h2>Watch the screencast</h2>

	<div class="video-embed">
		<iframe width="640" height="360" src="http://www.youtube.com/embed/ATV5K9UpJHs" frameborder="0" allowfullscreen></iframe><p><a href="https://www.youtube.com/user/NemoPostScriptum/videos" rel="nofollow" title="Subscribe Post Scriptum's Youtube Channel">Subscribe Post Scriptum's Youtube Channel</a></p>	</div>
<p>The post <a rel="nofollow" href="http://nemops.com/staging-site-prestashop-thirtybees/">Setting up a staging site for PrestaShop and Thirtybees</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nemops.com/staging-site-prestashop-thirtybees/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>10 mistakes to avoid in PrestaShop Development</title>
		<link>http://nemops.com/prestashop-development-10-mistakes/</link>
		<comments>http://nemops.com/prestashop-development-10-mistakes/#comments</comments>
		<pubDate>Wed, 03 Feb 2016 11:16:31 +0000</pubDate>
		<dc:creator><![CDATA[Nemo]]></dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[prestashop]]></category>

		<guid isPermaLink="false">http://nemops.com/?p=2696</guid>
		<description><![CDATA[<p>PrestaShop development is not easy at all. The software&#8217;s architecture requires a few good practices that are often ignored, making bug fixing an even more troublesome process for developers. In this article, we will see which are the ten most common mistakes you can make when developing for PrestaShop, and how to avoid them. 1- [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://nemops.com/prestashop-development-10-mistakes/">10 mistakes to avoid in PrestaShop Development</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>PrestaShop development is not easy at all. The software&#8217;s architecture requires a few good practices that are often ignored, making bug fixing an even more troublesome process for developers. In this article, we will see which are the ten most common mistakes you can make when developing for PrestaShop, and how to avoid them.<br />
<span id="more-2696"></span></p>
<div class="separator"></div>
<h2>1- You don&#8217;t keep PS_DEV_MODE enabled</h2>
<p>On your dev sites, always keep development mode. If you don&#8217;t know how to, just reach out <em>config/defines.inc.php</em>, locate define(&#8216;_PS_MODE_DEV_&#8217;, false) and set it to true. If you need to test out performance, you might want to turn on _PS_DEBUG_PROFILING_ as well</p>
<h2>2- Not enough testing</h2>
<p>It&#8217;s obvious, but again often overlooked. <strong>Always make sure you test your modules on all the PrestaShop versions you want it to work with.</strong>. Test it not only locally, but online as well. If it involves category pages, make sure it works and it&#8217;s compatible with the Layered Navigation module. Also, try to have as many standard modules installed as you can, when you run your tests; this ensures your addon is compatible with at least the basic functionalities of the software.</p>
<h2>3- Core files modification</h2>
<p>I will never stress this highly enough: <strong>do not ever apply your modifications directly to core files</strong> (unless it&#8217;s impossible to do otherwise). <strong>Always use overrides</strong>, since it&#8217;s easier to point out a modification added with this method, rather than having to scrub through all the original files.</p>
<h2>4- Not enough comments</h2>
<p>Even if you use overrides, make sure you point out which part of the code you amended. I often found myself lost in overridden methods, since they were an almost identical copy of the original, with the tiniest modification to a single sql query, without anything pointing it out.</p>
<h2>5- Breaking the MVC pattern</h2>
<p>If you need to query the database, always use a class. It&#8217;s bad practice to run queries directly from controllers, so make sure you always use them exclusively to bind a class to a view. You will have to create another file, but the result is going to be cleaner and easier to maintain. For example, if you need you get a list of entries from your module&#8217;s table, you might want to have a class included in your controller <strong>with require_once at the very beginning of it</strong>. Alternatively, you can always access the module&#8217;s core file&#8217;s methods from any controller, using <strong>$this->module</strong>.</p>
<h2>6- Including CSS and Javascript in templates</h2>
<p>PrestaShop has two methods for inclusing Javascript and CSS: AddCSS and AddJS (please refer to: <a href="http://nemops.com/prestashop-functions-1/#.VrHbDrI4Hmg" title="Essential Prestashop Functions">Essential Prestashop Functions</a>). It&#8217;s not advised to use &laquo;style&raquo; tags directly within templates. You can use script tags, but only if it&#8217;s strictly necessary.</p>
<h2>7- Too much Javascript</h2>
<p>The modern web is full of fancy animations and pretty things that make a page render nice to the viewer, but also slower. Make sure you do not over-rely on javascript for page rendering. I have come across templates that could not be used at all without Javascript, and this is potentially harmful to any shop. Consider this: a single javascript error can kill the whole website&#8217;s accessibility. For this reason, always make sure your addon/theme runs fine without Javascript (just disable it from any browser console and test)</p>
<h2>8- No indentation indentation/formatting</h2>
<p>Try to format your code a bit. It&#8217;s not necessary to slavishly follow the standards PrestaShop requires to validate a module for the addons store, if you are selling it on your site. However, it&#8217;s a lot easier for us other developers to read your code, if it has a decent formatting.</p>
<h2>9- Extensive usage of custom code for the back office</h2>
<p>PrestaShop has tons of helpers for the back office, it&#8217;s better to rely on those instead of writing huge blocks of code with custom styling. It&#8217;s always better to output things with a template, whenever possible, and in any case use the standard css classes/boxes even when outputting directly from PHP.</p>
<h2>10 &#8211; Eccessive use of custom hooks</h2>
<p>Plenty templates I worked on had an insane amount of custom hooks. If you are a template developer, try to keep them at a bare minimum, unless strictly necessary, and rely on PrestaShop&#8217;s standard ones instead. A theme made up entirely with custom hooks is a real pain to deal with.</p>
<div class="separator"></div>
<h3>Want to know more about PrestaShop Development?</h3>
<p>Have a look at my <a href="http://nemops.com/prestashop-modules-course/" title="PrestaShop Modules Course">PrestaShop Modules Course</a></p>
<p>The post <a rel="nofollow" href="http://nemops.com/prestashop-development-10-mistakes/">10 mistakes to avoid in PrestaShop Development</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nemops.com/prestashop-development-10-mistakes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Essential Prestashop Functions – Day 2</title>
		<link>http://nemops.com/prestashop-functions-2/</link>
		<comments>http://nemops.com/prestashop-functions-2/#comments</comments>
		<pubDate>Tue, 28 Jul 2015 12:19:28 +0000</pubDate>
		<dc:creator><![CDATA[Nemo]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[prestashop]]></category>

		<guid isPermaLink="false">http://nemops.com/?p=2492</guid>
		<description><![CDATA[<p>Prestashop has lots of time-saving functions that we can use when developing modules or extensions. In this second batch we will examine database-related methods. NOTICE: Values with &#8220;=&#8221; in the declaration are optionals. Database-related Functions These methods serve as helpers so that we don&#8217;t have to create a new mysql connection every time we want [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://nemops.com/prestashop-functions-2/">Essential Prestashop Functions – Day 2</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Prestashop has lots of time-saving functions that we can use when developing modules or extensions. In this second batch we will examine database-related methods.</p>
<p><span id="more-2492"></span></p>
<p><strong>NOTICE: Values with &#8220;=&#8221; in the declaration are optionals.</strong></p>
<h2>Database-related Functions</h2>
<pre class="brush: php; title: ; notranslate">

// Retrieving an array of values from the given table
Db::getInstance()-&gt;executeS($sql, $array = true, $use_cache = true);

// Retrieving a single value
Db::getInstance()-&gt;getValue($sql, $use_cache = true);

// Retrieving a whole row
Db::getInstance()-&gt;getRow($sql, $use_cache = true);

// Executing a generic query, returns true if succeeded, false if failed
Db::getInstance()-&gt;execute($sql, $use_cache = true)

// Inserting a row
Db::getInstance()-&gt;insert($table, $data, $null_values = false, $use_cache = true, $type = Db::INSERT, $add_prefix = true)

// Updating values
Db::getInstance()-&gt;update($table, $data, $where = '', $limit = 0, $null_values = false, $use_cache = true, $add_prefix = true)

// Erase the given entry
Db::getInstance()-&gt;delete($table, $where = '', $limit = 0, $use_cache = true, $add_prefix = true)

// Escape data
Db::getInstance()-&gt;escape($string, $html_ok = false, $bq_sql = false)

// Get the primary key of the last item you added
Db::getInstance()-&gt;Insert_ID()

</pre>
<p>These methods serve as helpers so that we don&#8217;t have to create a new mysql connection every time we want to run a query</p>
<h3>Example Usage</h3>
<pre class="brush: php; title: ; notranslate">

// Returns an array with each item correspinding to a single database row
// It should only be used to retrieve values, use execute or the other helpers to insert/update them
$customers = Db::getInstance()-&gt;executeS('SELECT * FROM '._DB_PREFIX_.'customers');

// Returns product data of the ones that belong to the given category only (id = 4)
$products = Db::getInstance()-&gt;executeS('
	SELECT * FROM '._DB_PREFIX_.'product p
	LEFT JOIN '._DB_PREFIX_.'category_product cp ON (cp.id_product = p.id_product)
	WHERE cp.id_category = 4
');


// Retrieving the specific product name for our current language
$product_name = Db::getInstance()-&gt;getValue('SELECT name FROM '._DB_PREFIX_.'product_lang WHERE id_product = 1 AND id_lang = ' . $this-&gt;context-&gt;language-&gt;id);

// Get the whole row for customer id = 1
$customer = Db::getInstance()-&gt;getRow('SELECT * FROM '._DB_PREFIX_.'customer WHERE id_customer = 1');


// Insert some data manually (not recommended, unless you have some really specific SQL to use)
Db::getInstance()-&gt;execute('INSERT INTO '._DB_PREFIX_.'customer (id_customer, email, firstname, lastname) VALUES (9, &quot;nemo@nemops.com&quot;, &quot;Fabio&quot;, &quot;Porta&quot;)');

// Inserting a row
// The $data array must be configured like 
// 		column =&gt; value

$data = array(
	'id_customer' =&gt; 1,
	'email' =&gt; &quot;nemo@nemops.com&quot;,
	'firstname' =&gt; &quot;Fabio&quot;,
	'lastname' =&gt; &quot;Porta&quot;,
);
Db::getInstance()-&gt;insert('customer', $data);

// Updating values, array configured as above
$data = array(
	'id_customer' =&gt; 1,
	'email' =&gt; &quot;nemo@nemops.com&quot;,
	'firstname' =&gt; &quot;Fabio&quot;,
	'lastname' =&gt; &quot;Porta&quot;,
);
Db::getInstance()-&gt;update('customer', $data, 'id_customer = 1');

// Erase customer with id = 1
Db::getInstance()-&gt;delete('customer', 'id_customer = 1');

// Escape data
$sanitized = Db::getInstance()-&gt;escape('&lt;div class=&quot;test&quot;&gt;&lt;div&gt;', true);
// will return &lt;div class=\&quot;test\&quot;&gt;&lt;div&gt;

// Get the primary key of the last item you added

$data = array(
	'email' =&gt; &quot;nemo@nemops.com&quot;,
	'firstname' =&gt; &quot;Fabio&quot;,
	'lastname' =&gt; &quot;Porta&quot;,
);
Db::getInstance()-&gt;insert('customer', $data);
$last_id = Db::getInstance()-&gt;Insert_ID()
// $last_id will be the id_customer of the entry we just added


</pre>
<div class="separator"></div>
<h2>The Query object in Prestashop</h2>
<pre class="brush: php; title: ; notranslate">

DbQuery::select($fields)
DbQuery::from($table, $alias = null)
DbQuery::join($fields)
DbQuery::leftJoin($table, $alias = null, $on = null)
DbQuery::where($restriction)
DbQuery::having($restriction)
DbQUery::orderBy($fields)
DbQUery::groupBy($fields)
DbQuery::limit($limit, $offset = 0)

</pre>
<p>All these functions are used to streamline a query creation. Inspect the <strong>DbQuery</strong> class to have a complete list.</p>
<h3>Example Usage</h3>
<pre class="brush: php; title: ; notranslate">

	// get all products with id &gt; 3, with relative language data

	$query = new DbQuery();
	$query-&gt;select('p.*, pl.*')
		-&gt;from('product', 'p')
		-&gt;leftJoin('product_lang', 'pl', 'p.id_product = pl.id_product')
		-&gt;where('p.id_product &gt; 34')
		-&gt;where('pl.id_lang = ' . $this-&gt;context-&gt;language-&gt;id)
		-&gt;groupBy('p.id_product')
		-&gt;limit(5)

	$result = Db::getInstance()-&gt;getValue($query);

</pre>
<div class="separator"></div>
<h2>Quick escape in a SQL query</h2>
<pre class="brush: php; title: ; notranslate">

pSQL($string, $htmlOK = false)

</pre>
<p>Sanitize data which will be injected into SQL query</p>
<h3>Example Usage</h3>
<pre class="brush: php; title: ; notranslate">

	$search = '2&quot; clamps';
	// get aliases for the given search word, as you can see the above requires douple quotes to be escaped
	$aliases = Db::getInstance()-&gt;executeS('
	SELECT a.alias
	FROM `'._DB_PREFIX_.'alias` a
	WHERE `search` = \''.pSQL($search).'\'');

</pre>
<h2>Wrapping it up</h2>
<p>All of the methods we examined today are a huge time saver when dealing with the Database in Prestashop. I rarely use the query object, I admit, as I prefer relying on the other methods. However, since it&#8217;s getting more and more built into the standard workflow of core modules and methods, it&#8217;s good practice to start using that as well, and integrate it into your own Prestashop Module/Extension.</p>
<div class="separator"></div>
<h3>Additional Resources</h3>
<ul>
<li><a href="http://nemops.com/prestashop-functions-1/#.Vb8upPnzrmg" title="Essential Prestashop Functions – Day 1">Essential Prestashop Functions – Day 1</a></li>
<li><a href="http://nemops.com/prestashop-functions-3/#.Vb9EAPnzrmg" title="Essential Prestashop Functions – Day 3">Essential Prestashop Functions – Day 3</a></li>
<li><a href="http://nemops.com/prestashop-functions-4/#.Vc2cffnzrmg" title="Essential Prestashop Functions – Day 4">Essential Prestashop Functions – Day 4</a></li>
<li><a href="http://nemops.com/prestashop-functions-5/#.Ve6fNxHzrmg" title="Essential Prestashop Functions – Day 5">Essential Prestashop Functions – Day 5</a></li>
</ul>
<p>The post <a rel="nofollow" href="http://nemops.com/prestashop-functions-2/">Essential Prestashop Functions – Day 2</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nemops.com/prestashop-functions-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Essential Prestashop Functions &#8211; Day 1</title>
		<link>http://nemops.com/prestashop-functions-1/</link>
		<comments>http://nemops.com/prestashop-functions-1/#comments</comments>
		<pubDate>Tue, 21 Jul 2015 10:03:01 +0000</pubDate>
		<dc:creator><![CDATA[Nemo]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[prestashop]]></category>

		<guid isPermaLink="false">http://nemops.com/?p=2486</guid>
		<description><![CDATA[<p>Prestashop has lots of time-saving functions that we can use when developing modules or extensions. Let&#8217;s have a look at the first batch of Prestashop Functions, and in which context they might come in use. If you want to dive into Prestashop development, these are likely the first methods you&#8217;ll be needing. NOTICE: Values with [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://nemops.com/prestashop-functions-1/">Essential Prestashop Functions &#8211; Day 1</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Prestashop has lots of time-saving functions that we can use when developing modules or extensions. Let&#8217;s have a look at the first batch of Prestashop Functions, and in which context they might come in use.</p>
<p><span id="more-2486"></span></p>
<p>If you want to dive into Prestashop development, these are likely the first methods you&#8217;ll be needing.</p>
<p><strong>NOTICE: Values with &#8220;=&#8221; in the declaration are optionals.</strong></p>
<h2>Managing Configuration values in Prestashop</h2>
<pre class="brush: php; title: ; notranslate">

// Updating a single/multiple value/s
Configuration::updateValue($key, $values, $html = false, $id_shop_group = null, $id_shop = null);

// Getting a single value
Configuration::get($key, $id_lang = null, $id_shop_group = null, $id_shop = null)

// Getting Multiple Values
Configuration::getMultiple($keys, $id_lang = null, $id_shop_group = null, $id_shop = null)

// Getting the same key in all languages
Configuration::getInt($key, $id_shop_group = null, $id_shop = null)

// Erase the given entry
Configuration::deleteByName($key)

</pre>
<p>These methods are used to interact with the *prefix_*configuration table. It&#8217;s the quickest way to store simple data in Prestashop.</p>
<h3>Example Usage</h3>
<pre class="brush: php; title: ; notranslate">
// Saving a normal string
$my_text = 'This is some text';
Configuration::updateValue('MY_CONFIG_PARAMETER', $my_text);

// Saving an html string
$my_text = '&lt;p&gt;This is some text&lt;/p&gt;';
Configuration::updateValue('MY_CONFIG_PARAMETER', $my_text, true);

// Saving multiple values, KEYS represend ID_LANG
$my_texts = array(
	1 =&gt; 'This is some text',
	2 =&gt; 'This is some text in another language'
	);
Configuration::updateValue('MY_CONFIG_PARAMETER_MULTILANG', $my_text);


// Getting a single value
$my_text = Configuration::get('MY_CONFIG_PARAMETER');

// Getting a single value for the current language only
$my_text = Configuration::get('MY_CONFIG_PARAMETER', $this-&gt;context-&gt;language-&gt;id);

// Getting Multiple Values
$keys_to_get = array('MY_CONFIG_PARAMETER', 'OTHER_CONFIG_PARAMETER');
Configuration::getMultiple($keys_to_get);

// Delete the given key
Configuration::deleteByName('MY_CONFIG_PARAMETER');
</pre>
<div class="separator"></div>
<h2>Getting POST and GET in Prestashop</h2>
<pre class="brush: php; title: ; notranslate">
Tools::getValue($key, $default_value = false)

</pre>
<p>Retrieves the current POST or GET variable with the given key</p>
<h3>Example Usage</h3>
<pre class="brush: php; title: ; notranslate">
// Getting either POST or GET with the key 'myvalue', the second parameter is what to assign then nothing is found.
$value = Tools::getValue('myvalue', 4);
// If no 'myvalue' POST or GET variable is set, $value will be 4.
</pre>
<div class="separator"></div>
<h2>Checking for submitted data in Prestashop</h2>
<pre class="brush: php; title: ; notranslate">
Tools::isSubmit($key);
</pre>
<p>Check whether or not the given key has been submitted to the current page.</p>
<h3>Example Usage</h3>
<pre class="brush: php; title: ; notranslate">
if(Tools::isSubmit('myvalue'))
{
	// do something
}
</pre>
<div class="separator"></div>
<h2>Displaying Errors in Prestashop</h2>
<pre class="brush: php; title: ; notranslate">
Tools::displayError($string = 'Fatal error', $htmlentities = true, Context $context = null)
</pre>
<p>Displays an error with proper formatting.</p>
<h3>Example Usage</h3>
<pre class="brush: php; title: ; notranslate">
if($myvalue != 1)
{
	// will need to be displayed later on
	$error_string = Tools::displayError('It is different from 1');
}
</pre>
<div class="separator"></div>
<h2>Displaying Confirmations in Prestashop Modules</h2>
<pre class="brush: php; title: ; notranslate">
$this-&gt;displayConfirmation($string);
</pre>
<p>Displays a properly formatted confirmation message. $this refers to a module, it is therefore only available in module contexts only. Refer to the proper variable elsewhere.</p>
<pre class="brush: php; title: ; notranslate">

// this is inside a module's method
if($myvalue == 1)
{
	// will need to be displayed later on
	$conf_string = $this-&gt;displayConfirmation('Updated!');
}

</pre>
<div class="separator"></div>
<h2>Adding CSS in Prestashop</h2>
<pre class="brush: php; title: ; notranslate">
ControllerCore::addCSS($css_uri, $css_media_type = 'all', $offset = null, $check_path = true)
</pre>
<p>Appends the given CSS file/s to the page</p>
<h3>Example Usage</h3>
<pre class="brush: php; title: ; notranslate">

// this is inside a module's method
$this-&gt;context-&gt;controller-&gt;addCSS($this-&gt;_path.'css/myfile.css', 'all');

// this is inside a controller's method
$this-&gt;addCSS(_THEME_CSS_DIR_.'product.css');

// adding multiple files
$files = array(
	_THEME_CSS_DIR_.'product.css' =&gt; 'all',
	_THEME_CSS_DIR_.'other_css.css' =&gt; 'all'
);
$this-&gt;addCSS($files);

</pre>
<div class="separator"></div>
<h2>Adding Javascript in Prestashop</h2>
<pre class="brush: php; title: ; notranslate">
ControllerCore::addJS($js_uri, $check_path = true);
</pre>
<p>Appends the given JS file/s to the page</p>
<h3>Example Usage</h3>
<pre class="brush: php; title: ; notranslate">

// this is inside a module's method
$this-&gt;context-&gt;controller-&gt;addCSS($this-&gt;_path.'css/myfile.css', 'all');

// this is inside a controller's method
$this-&gt;addCSS(_THEME_CSS_DIR_.'product.css');

</pre>
<div class="separator"></div>
<h2>Adding jQuery Plugins in Prestashop</h2>
<pre class="brush: php; title: ; notranslate">
ControllerCore::addJqueryPlugin($name, $folder = null, $css = true)
</pre>
<p>Appends the given jQuery Plugin/s to the page</p>
<h3>Example Usage</h3>
<pre class="brush: php; title: ; notranslate">

// this is inside a module's method, multiple files
$this-&gt;context-&gt;controller-&gt;addjqueryPlugin('fancybox');

// this is inside a controller's method
$this-&gt;addjqueryPlugin('fancybox');

// this is inside a controller's method, multiple values
$this-&gt;addjQueryPlugin(array('scrollTo', 'alerts', 'chosen', 'autosize', 'fancybox' ));

// this is inside a controller's method, without loading the plugin's CSS
$this-&gt;addjQueryPlugin('growl', null, false);

</pre>
<div class="separator"></div>
<h2>Adding jQuery UI Components in Prestashop</h2>
<pre class="brush: php; title: ; notranslate">
ControllerCore::addJqueryUI($component, $theme = 'base', $check_dependencies = true);
</pre>
<p>Appends the given jQuery UI component/s to the page</p>
<h3>Example Usage</h3>
<pre class="brush: php; title: ; notranslate">

// this is inside a module's method, single file
$this-&gt;context-&gt;controller-&gt;addjQueryUI(ui.datepicker');

// this is inside a controller's method, multiple files
$this-&gt;addJqueryUI(array('ui.slider', 'ui.datepicker'));

</pre>
<div class="separator"></div>
<h2>Wrapping it up</h2>
<p>We saw quite a number of useful PrestaShop Functions today, which are enough to start coding your own, simple module. Apart from trying to remember them all, I suggest creating code snippets with some text expander, so you can quickly drop them in, without having to remember the exact syntax.</p>
<div class="separator"></div>
<h3>Additional Resources</h3>
<ul>
<li><a href="http://nemops.com/prestashop-functions-2/#.Vb9Bxvnzrmg" title="Essential Prestashop Functions – Day 2">Essential Prestashop Functions – Day 2</a></li>
<li><a href="http://nemops.com/prestashop-functions-3/#.Vb9EAPnzrmg" title="Essential Prestashop Functions – Day 3">Essential Prestashop Functions – Day 3</a></li>
<li><a href="http://nemops.com/prestashop-functions-4/#.Vc2cffnzrmg" title="Essential Prestashop Functions – Day 4">Essential Prestashop Functions – Day 4</a></li>
<li><a href="http://nemops.com/prestashop-functions-5/#.Ve6fNxHzrmg" title="Essential Prestashop Functions – Day 5">Essential Prestashop Functions – Day 5</a></li>
</ul>
<p>The post <a rel="nofollow" href="http://nemops.com/prestashop-functions-1/">Essential Prestashop Functions &#8211; Day 1</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nemops.com/prestashop-functions-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A deeper look at Prestashop Hooks</title>
		<link>http://nemops.com/a-deeper-look-at-prestashop-hooks/</link>
		<comments>http://nemops.com/a-deeper-look-at-prestashop-hooks/#comments</comments>
		<pubDate>Thu, 23 Aug 2012 08:09:49 +0000</pubDate>
		<dc:creator><![CDATA[Nemo]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[hook]]></category>
		<category><![CDATA[module]]></category>

		<guid isPermaLink="false">http://nemops.com/?p=173</guid>
		<description><![CDATA[<p>Today, we are going to cover one of the most overlooked feature of Prestashop: Hooks. I&#8217;ll be teaching you what these mysterious entities really are, and how to bend them to your will, and bring your site to the next level of customization. What are hooks? If you have been using Prestashop for quite some [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://nemops.com/a-deeper-look-at-prestashop-hooks/">A deeper look at Prestashop Hooks</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Today, we are going to cover one of the most overlooked feature of Prestashop: Hooks. I&#8217;ll be teaching you what these mysterious entities really are, and how to bend them to your will, and bring your site to the next level of customization.<br />
<span id="more-173"></span><br />
<a class="download-files button style1" href="http://nemops.com/wp-content/uploads/2012/08/testing.rar" title="Download Project Files">Download Project Files</a></p>
<h2><strong>What are hooks?</strong></h2>
<p>If you have been using Prestashop for quite some time, I bet you found yourself saying “Why in the world can’t I place something there? I really want to do it!” in more than one occasion. Yes, it can be frustrating, because Prestashop only allows you to execute your modules in specific spots, defined by the software itself. These places are commonly known as <strong>hooks</strong>. And this is actually all what hooks are, as the name suggests: they represent definite points of the code which you can use to run your own module’s functions.</p>
<p>If this may be considered as a limitation, since it forces the developer to only use those specific points to run a module’s code without touching the core files, it’s also a powerful and easy solution to greatly improve Prestashop’s basics functionalities. Actually, hooks make your life A LOT easier when extending the package. At least, you don’t have to mess for ages with XML and Zend Framework ugly PHP (have I heard someone screaming Magento?).</p>
<p>Anyway, if you are a developer, and know how to code a prestashop module, you’ll be quite used to the following structure:</p>
<pre class="brush:php">public function hookLeftColumn($params)
{
    /* Code to be executed */
}</pre>
<p>This function allows the module to be hooked to the left column, and running proper code from there, be it something to display to the user, or stas gathering stuff. In any case, you will <strong>always</strong> need to run a <em>hookSomewhere</em> function to run the module’s code in a specific place.</p>
<div class="separator"></div>
<p>&nbsp;</p>
<h2><strong>Visual Hooks VS Action Hooks</strong></h2>
<p>Hooks can be basically grouped into 2 main groups: <strong>Visual Hooks</strong>, and <strong>Action Hooks</strong>.</p>
<p>The first ones are generally used to display content to your website, and provide a visual feedback to the customer. While there are in fact back Office visual hooks, like the one used to display new tabs on the Stats page, most of them are defined by controllers in front office, and assigned to variables to be executed within the template. An example of this is the “Left Column” hook we saw previously, used to show modules on the left column of the base template</p>
<p>&nbsp;</p>
<p>Action hooks are, on the other side, deputed to only run code, without actually display content. They are placed both in class and controller files, and may come in handy when we need to gather data thrown by a class, manipulate it, or use it for  purposes other than Prestashop’s default one. As the name suggests, they occur when a spacific action takes place. An example of action hook is the “cart” hook, executed in the Cart.php Class when a cart is created or updated.</p>
<p>To exemplify, let’s say we have a “best sales” module, and it uses database caching to optimize performance. It retrieves best sellers from a cached version of the product list created once in a while. Well, this once in a while can be set in two ways: the traditional way is to set up a cron job, and refresh the list once every hour, or day, or so; the other way would be using hooks. We could hook the module to “<strong>newOrder</strong>”, which is a hook that executes when a new order is placed, and code a function that refreshes data in that particular moment. This way, there would be now need to fetch all product data every time the page reloads, thus increasing our site’s speed. Of course, this is just an example (taken from one of my modules, Best sales of the day/week/month/period, which you can find <a title="here" href="http://store.nemops.com/modules/14-best-sales-of-the-day-week-month-period.html" target="_blank"><strong><span style="text-decoration: underline;">here</span></strong></a>), and there are other many things you can do with action hooks.</p>
<p>Whichever purpose hooks have, they all share the same basic structure:</p>
<ul>
<li><strong>A database entry in the ps_hook table </strong></li>
</ul>
<p><img class="aligncenter size-large wp-image-184" style="color: #333333; font-style: normal; margin-top: 0.4em;" title="Prestashop Hooks table" src="http://nemops.com/wp-content/uploads/2012/08/php_my_admin-680x144.png" alt="Prestashop Hooks table" width="680" height="144" /></p>
<p>This is the core part of the hooks, since it’s what makes Prestashop know whether the specific hook we are currently calling exists or not.</p>
<p>&nbsp;</p>
<ul>
<li><strong> The <em>hookExec()</em> function which executes it</strong></li>
</ul>
<p>It’s the function that runs the hooks execution, and therefore all hook<em>NameOfTheHook</em>() function associated to it(see next). The function is defined in the Module Class, and can be passed a number of parameters to be stored in the <strong>$params</strong> variable of the executing function.</p>
<pre class="brush:php">Module::hookExec('nameofthehook')</pre>
<pre class="brush:php">Module::hookExec('nameofthehook', $params_array)</pre>
<p>When creating a front office visual hook, instead of directly executing the hooks attached modules, the action is assigned to a variable to be called by smarty within the template.</p>
<pre class="brush:php">self::$smarty-&gt;assign('HOOK_NAMEOFTHEHOOK', Module::hookExec('nameofthehook'));</pre>
<p>In this case, you would also need to add the following code to the template file where you wanted to display the hook’s content</p>
<pre class="brush:php">{$HOOK_NAMEOFTHEHOOK}</pre>
<p><strong><br />
</strong></p>
<ul>
<li><strong>A <em>hookNameOfTheHook()</em> function to be placed in modules in order to hook them</strong></li>
</ul>
<p>Without this, a module would not be hooked to anything and would never execute. This last function is the most important one, since it allows you to perform any action for the desired hook. We already saw the code for the <em>HookLeftColumn()</em> function.</p>
<pre class="brush:php">public function hookLeftColumn($params)
{
    /* Code to be executed */
}</pre>
<p>This function usually returns a template file in front office hooks</p>
<pre class="brush:php">return $this-&gt;display(__FILE__, 'filename.tpl')</pre>
<p>&nbsp;</p>
<div class="separator"></div>
<p>&nbsp;</p>
<h2>Let’s go into action: creating a new Hook in Prestashop</h2>
<p>Now that we know enough about hooks, let’s create a new hook that will allow us to display modules content at the beginning of  each category page.</p>
<p>The first step will be to let Prestashop know the hook exists, so let’s add it to the database. We can go 2 ways: log in to phpmyadmin (or any sql manager) and add the column manually; or create a module which, when installed, will add the hook.</p>
<p><strong>The database way:</strong></p>
<p>Log in to your sql manager. In your prestashop database, look for the table ps_hook (remember, ps_ is just the default prefix, you can have anything instead of that! Take note of your prefix). If you’re using phpMyAdmin, click on the <em>yourprefix</em><strong>Hook</strong> table, then Insert, and add values for the name (which should be all underscore and without spaces), title and description then click add. Alternatively, you can use run this SQL query:</p>
<pre class="brush:php">INSERT INTO `ps_hook` (`name`, `title`, `description`) 
VALUES ('category', 'Category', 'Hooks before the product list in a category');</pre>
<p>&nbsp;</p>
<p><strong>The module way:</strong></p>
<p>If the hook is only used by a module, we can also bind it to the module itself, and add it in the installation process. Let&#8217;s create a function that looks like this:</p>
<pre class="brush:php">protected function addHook() {
    $check = Db::getInstance()-&gt;getRow('
			SELECT name
			FROM '._DB_PREFIX_.'hook
			WHERE name = "category"
			');

    if (!$check) {
	$query = "INSERT INTO "._DB_PREFIX_."hook (`name`, `title`, `description`) VALUES ('category', 'Category', 'Hooks before the product list in a category');";
	if(Db::getInstance()-&gt;Execute($query))
	    return true;
	else
	    return false;
    }
    else return true;
}</pre>
<p>The $check part is useful to know if another hook with the same name exists, since creating douplicates will definitely cause problems!</p>
<p>&nbsp;</p>
<p><strong>Executing the hook</strong></p>
<p>Now that prestashop has the hook data stored, it’s time to run it’s execution</p>
<pre class="brush:php">self::$smarty-&gt;assign('HOOK_CATEGORY', Module::hookExec('category'));</pre>
<p>We can place this code in the FrontController.php file, or any other controller, since we are going to use the hook for display purposes. Using the FrontController saves us the time of thinking where exactly it should run, because assigning the variable in the DisplayHeader function will set it for every front page, before even one of the other hooks is assigned. So, to add the assignment to the Front Controller, let’s create a new FrontController.php file, place it into override/classes, and fill it with the following code.</p>
<pre class="brush:php"></pre>
<pre class="brush:php">&lt;?php 

Class FrontController extends FrontControllerCore
{

	public function displayHeader()
	{
		if (!self::$initialized)
			 $this-&gt;init();

			self::$smarty-&gt;assign('HOOK_CATEGORY', Module::hookExec('category'));

		parent::displayHeader();		
	}

}</pre>
<p>&nbsp;</p>
<p>Note the if (!self::$initialized)  $this-&gt;init(); snippet at the beginning of the function. This ensures the controller is properly initialized, and fixes problems in pages such as payment or order confirmation..</p>
<p>Even though using the front controller saves us the time of having to scrub through all other controllers to find the right position to execute it, be aware that it will be assigning the execution for every front page of the shop. So, for our purposes, it would have been better to add the assignment in the <em>process()</em> function of the <strong>CategoryController</strong>. In the end, of course, the choice is all up to you. For the sake of it, here is what adding the code to a categoryController override would have looked like</p>
<pre class="brush:php">class CategoryController extends CategoryControllerCore
{

    public function process()
    {
        parent::process();
        if (!($id_category = (int)Tools::getValue('id_category')) OR !Validate::isUnsignedId($id_category))
            $this-&gt;errors[] = Tools::displayError('Missing category ID');
	else
		{
                     self::$smarty-&gt;assign('HOOK_CATEGORY', Module::hookExec('category'));
          }
    }
}</pre>
<p>&nbsp;</p>
<p><strong>Now, let’s add the hook to the template</strong></p>
<p>Let’s open up the category.tpl file of our template.Just before this code</p>
<pre class="brush:php">{if $products}</pre>
<p>Which is line 90 of the prestashop_new template, add the following</p>
<pre class="brush:php">{$HOOK_CATEGORY}</pre>
<p>Most of the work it’s done, and we have just successfully added a new hook in Prestashop!</p>
<div class="separator"></div>
<h2>Creating a module to be hooked</h2>
<p>…Yay, okay, but we need to test this new hook, or else we don’t know if it really works or not. I’ve prepared a testing module, which can be found as an attachment at the beginning of the post. Grab it, copy the “testing” folder inside the <em>modules</em> folder and open the testing.php file.</p>
<p>If you know something about modules, you’ll definitely know they have to be <strong>registered</strong> to a hook before they can be executed. If you don’t want to do it manually from the positions tab in back office, add the following code in the <em>install()</em> function:</p>
<pre class="brush:php">public function install()
{
    if (!parent::install() OR
	!$this-&gt;registerHook('Category'))
        return false;
    return true;
}</pre>
<p>Now, at the bottom of the file, create the hookCategory Function:</p>
<pre class="brush:php">public function hookCategory($params)
{
    global $cookie, $smarty;
    return $this-&gt;display(__FILE__, 'testing.tpl');
}</pre>
<p>&nbsp;</p>
<p>And that’s all!</p>
<p>Let’s install the module, go to a category page of the shop and see if the template’s content appears where we placed the hook variable. It’s likely we won’t see anything at the moment, but don’t fear. Since Prestashop uses caching, we must first re-compile the cache to see changes. Let’s go to <strong>preferences</strong>, then <strong>performance</strong> in back office, and turn on the option “<strong>Force Compile</strong>”, then refresh the category page. At this point, we should see a piece of text saying “Howdy guys! testing new hooks” If you don’t, since the Force Compile feature is a bit buggy, go to the Tools/Smarty/Compile folder and delete everything <strong>except the index.php</strong>. This should fix it, and you should be able to see the text.</p>
<h3>Conclusion</h3>
<p>We’re done! Thanks for reading. If you still have questions about how to create new hooks, feel free to contact me and I’ll be glad to help you.</p>
<h3>Additional Resources</h3>
<ul>
<li><a title="A list of Prestashop Hooks" href="http://doc.prestashop.com/display/PS15/Hooks+in+PrestaShop+1.5" target="_blank">A list of Prestashop Hooks</a></li>
<li><a title="EZ Hook Module: add new hooks in a couple of clicks" href="http://store.nemops.com/17-ez-hook.html" target="_blank">EZ Hook module: add new hooks in a couple of clicks</a></li>
</ul>
<p>The post <a rel="nofollow" href="http://nemops.com/a-deeper-look-at-prestashop-hooks/">A deeper look at Prestashop Hooks</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nemops.com/a-deeper-look-at-prestashop-hooks/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
