<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Krystian Podemski</title>
    <description>The latest articles on DEV Community by Krystian Podemski (@kpodemski).</description>
    <link>https://dev.to/kpodemski</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F503978%2F82790cce-3c59-4e28-a30d-86d2c7f3f83a.jpg</url>
      <title>DEV Community: Krystian Podemski</title>
      <link>https://dev.to/kpodemski</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kpodemski"/>
    <language>en</language>
    <item>
      <title>PrestaShop: adding new fields to the customer form</title>
      <dc:creator>Krystian Podemski</dc:creator>
      <pubDate>Fri, 14 Jan 2022 22:32:57 +0000</pubDate>
      <link>https://dev.to/kpodemski/prestashop-adding-new-fields-to-the-customer-form-350k</link>
      <guid>https://dev.to/kpodemski/prestashop-adding-new-fields-to-the-customer-form-350k</guid>
      <description>&lt;p&gt;In this article, I will demonstrate how to edit, add, and modify fields available in the customer form, in PrestaShop.&lt;/p&gt;




&lt;p&gt;One of the users of a popular Polish PrestaShop group on Facebook did not manage to modify the form fields available during the registration process. His task was to alter the "optin" field. He wanted to change both the content and style of the field.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Code from this article will work with PrestaShop 1.7.7 and above versions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With help comes the "additionalCustomerFormFields" hook. Since PrestaShop version 1.7.7, it got the &lt;code&gt;&amp;amp;$fields&lt;/code&gt; parameter, which contains a whole array of fields used to create a registration form.&lt;/p&gt;

&lt;p&gt;This article describes how we can use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;As in many other places in the system, we have a parameter that is available as a reference (&lt;a href="https://www.php.net/manual/en/language.references.php" rel="noopener noreferrer"&gt;here's&lt;/a&gt; a more detailed explanation of what references are), thanks to that, we can operate with the data freely, and we will apply our modifications after calling this Hook.&lt;/p&gt;

&lt;p&gt;To get started, we need, of course, to hook to the appropriate place in the code in our module.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;install&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;registerHook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'additionalCustomerFormFields'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In our module, we have access to the entire array of fields in the method that handles the hook. You can &lt;code&gt;dump&lt;/code&gt; them, like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;hookAdditionalCustomerFormFields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$format&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'fields'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nf"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$format&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If we go to the registration page at this point, we will see all the fields:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpiabu6hkruy3p84wo9up.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpiabu6hkruy3p84wo9up.jpeg" alt="form fields dump PrestaShop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The rest is just a formality. Let's move on to the real-world use of &lt;code&gt;hookAdditionalCustomerFormFields&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding fields
&lt;/h2&gt;

&lt;p&gt;Each field is an instance of &lt;code&gt;FormField&lt;/code&gt;. This is a class available globally in the system. We can add a new field like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;hookAdditionalCustomerFormFields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$format&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'fields'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nv"&gt;$format&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'confirmation_email'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FormField&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'confirmation_email'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;setType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setLabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;trans&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Confirm your e-mail address'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="s1"&gt;'Modules.Demooverridecustomerformatter.Front'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setRequired&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'fields'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$format&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This way, we added a new field to the form. This field called "confirmation_email" is a typical example of adding an element to force the buyer to confirm their email address.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deleting fields
&lt;/h2&gt;

&lt;p&gt;If we want to make any of the fields no longer available in the form, we can remove them using PHP's &lt;code&gt;unset&lt;/code&gt;. Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;hookAdditionalCustomerFormFields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$format&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'fields'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;unset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$format&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'id_gender'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'fields'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$format&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Modifying existing fields
&lt;/h2&gt;

&lt;p&gt;Let's see how we can modify fields, which the author of the thread on Facebook cared about. How to change the fields? Let's focus on the &lt;code&gt;optin&lt;/code&gt; field. Let's modify its label and add a style (although this is not something we should do in this application layer).&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;hookAdditionalCustomerFormFields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$format&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'fields'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nv"&gt;$newLabel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'&amp;lt;em class="text-warning"&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$newLabel&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;trans&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s1"&gt;'I want to receive free gift from your partners'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;[],&lt;/span&gt;
        &lt;span class="s1"&gt;'Modules.Demooverridecustomerformatter.Front'&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$newLabel&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'&amp;lt;/em&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$format&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'optin'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setLabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nv"&gt;$newLabel&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'fields'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$format&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As we can see, by having access to an instance of a field, we can use methods available in the &lt;code&gt;FormField&lt;/code&gt; class, one of them is &lt;code&gt;setLabel&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We added &lt;code&gt;em&lt;/code&gt;, color and changed the text, the effect is as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0v2vj989jizxpgxjibbk.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0v2vj989jizxpgxjibbk.jpeg" alt="example of modified customer form in PrestaShop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;As we can see, modifying the registration form fields using a dedicated &lt;code&gt;Hook&lt;/code&gt; is quite accessible and allows developers to have complete control over it. As the fields are an array, we can freely modify them, change their order, delete and add new ones.&lt;/p&gt;

&lt;p&gt;This article did not cover all the issues related to the registration form and its fields, I did not show how to receive the values sent from the additional fields, verify and possibly save them in the database. There will be time for that at another time.&lt;/p&gt;

&lt;p&gt;If you have any questions, let me know :-)&lt;/p&gt;

</description>
      <category>prestashop</category>
      <category>php</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Better PrestaShop Profiler in version 1.7.8. How to use it in the older versions?</title>
      <dc:creator>Krystian Podemski</dc:creator>
      <pubDate>Sat, 08 Jan 2022 18:06:03 +0000</pubDate>
      <link>https://dev.to/kpodemski/better-prestashop-profiler-in-version-178-how-to-use-it-in-the-older-versions-31h1</link>
      <guid>https://dev.to/kpodemski/better-prestashop-profiler-in-version-178-how-to-use-it-in-the-older-versions-31h1</guid>
      <description>&lt;p&gt;In version 1.7.8 of the PrestaShop returns the ability to collect more precise information about the modules that are displayed on the front office of your store.&lt;/p&gt;

&lt;p&gt;Enabling the PrestaShop front office profiler is a great place to start if you need to know more about the performance of your PrestaShop.&lt;/p&gt;

&lt;p&gt;The data is collected both for hooks and modules attached to them, but also for widgets. If you don't have PrestaShop 1.7.8, don't worry. The profiler in the new version is very easy to use also with older versions of PrestaShop. In this post I will show you how to use it in the older versions.&lt;/p&gt;

&lt;p&gt;I've tested it in PrestaShop 1.7.7, and 1.7.6.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the profiler from 1.7.8 in lower versions of PrestaShop
&lt;/h2&gt;

&lt;p&gt;To use profiler in lower versions of PrestaShop, only 4 steps are needed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First download the PrestaShop 1.7.8 package, you can do it from &lt;a href="https://github.com/PrestaShop/PrestaShop/tree/1.7.8.x" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;After unpacking it, copy the &lt;code&gt;tools/profiling&lt;/code&gt; folder and move it to the same path in the older PrestaShop version.&lt;/li&gt;
&lt;li&gt;Next step is to edit the &lt;code&gt;config/config.inc.php&lt;/code&gt; file, just in the place where you currently have this code:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_PS_DEBUG_PROFILING_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;include_once&lt;/span&gt; &lt;span class="n"&gt;_PS_TOOL_DIR_&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'profiling/Controller.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;include_once&lt;/span&gt; &lt;span class="n"&gt;_PS_TOOL_DIR_&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'profiling/ObjectModel.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;include_once&lt;/span&gt; &lt;span class="n"&gt;_PS_TOOL_DIR_&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'profiling/Db.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;include_once&lt;/span&gt; &lt;span class="n"&gt;_PS_TOOL_DIR_&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'profiling/Tools.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;replace it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_PS_DEBUG_PROFILING_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;include_once&lt;/span&gt; &lt;span class="n"&gt;_PS_TOOL_DIR_&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'profiling/Profiler.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;include_once&lt;/span&gt; &lt;span class="n"&gt;_PS_TOOL_DIR_&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'profiling/Controller.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;include_once&lt;/span&gt; &lt;span class="n"&gt;_PS_TOOL_DIR_&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'profiling/ObjectModel.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;include_once&lt;/span&gt; &lt;span class="n"&gt;_PS_TOOL_DIR_&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'profiling/Db.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;include_once&lt;/span&gt; &lt;span class="n"&gt;_PS_TOOL_DIR_&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'profiling/Hook.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;include_once&lt;/span&gt; &lt;span class="n"&gt;_PS_TOOL_DIR_&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'profiling/Module.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;include_once&lt;/span&gt; &lt;span class="n"&gt;_PS_TOOL_DIR_&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'profiling/Tools.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last step you need to do, is to edit the &lt;code&gt;controllers/admin/AdminLegacyLayoutController.php&lt;/code&gt; file and delete:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$this-&amp;gt;outPutHtml;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;from the end of the &lt;code&gt;display&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Now if you set &lt;code&gt;_PS_DEBUG_PROFILING_&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt; in the &lt;code&gt;config/defines.inc.php&lt;/code&gt;, you can use a better version of the profiler in your PrestaShop store.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;All this should give you more information about the health of your front office. You can expect more features available in the front office profiler in the next major version of PrestaShop.&lt;/p&gt;

&lt;p&gt;Do you want to know more about the PrestaShop 8, and future of the PrestaShop? Read an article: &lt;a href="https://build.prestashop.com/news/prestashop-beyond-1-7/" rel="noopener noreferrer"&gt;PrestaShop Beyond 1.7&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkjg8spmla02mkccbf50t.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkjg8spmla02mkccbf50t.jpeg" alt="PrestaShop front office profiler"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>prestashop</category>
      <category>php</category>
      <category>ecommerce</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Display extra column in the PrestaShop Grid</title>
      <dc:creator>Krystian Podemski</dc:creator>
      <pubDate>Thu, 23 Dec 2021 08:14:07 +0000</pubDate>
      <link>https://dev.to/kpodemski/display-extra-column-in-the-prestashop-grid-157j</link>
      <guid>https://dev.to/kpodemski/display-extra-column-in-the-prestashop-grid-157j</guid>
      <description>&lt;p&gt;&lt;em&gt;This article is about PrestaShop version 1.7.7 and above.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The order list in &lt;a href="https://github.com/PrestaShop/PrestaShop" rel="noopener noreferrer"&gt;PrestaShop&lt;/a&gt; has quite a bit of information about them. One piece of information missing, however, is the name of the selected carrier.&lt;/p&gt;

&lt;p&gt;In this article I will walk you through the module that I published here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/kpodemski/kporderlistcarrier" rel="noopener noreferrer"&gt;https://github.com/kpodemski/kporderlistcarrier&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This module allows you to add carrier name to the order list and filter by it. The whole thing is based on &lt;a href="https://devdocs.prestashop.com/1.7/modules/concepts/hooks/#hooks" rel="noopener noreferrer"&gt;Hook&lt;/a&gt;, which is available in PrestaShop standard but it works only with the Symfony version of this page. That is why the module will work only for PrestaShop 1.7.7+&lt;/p&gt;

&lt;h2&gt;
  
  
  How it works. Grid component
&lt;/h2&gt;

&lt;p&gt;At this moment, any list of elements taken from the database, on pages that have been migrated to Symfony in PrestaShop, uses a new component: &lt;a href="https://devdocs.prestashop.com/1.7/development/components/grid/" rel="noopener noreferrer"&gt;Grid&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This component is similar to &lt;em&gt;HelperList&lt;/em&gt;, which you may know from older versions of PrestaShop. &lt;em&gt;HelperList&lt;/em&gt; is still present in the core, but you might want to use a modern alternative, which is Grid.&lt;/p&gt;

&lt;p&gt;With this component, we will display a list of elements e.g. from the database. We will add options to search through it, sorting, we can also enable various operations on records, deleting, editing, or other. We can also define them ourselves.&lt;/p&gt;

&lt;p&gt;Detailed information about the component is available in PrestaShop developers' documentation, you can find there examples of usage, default data types and much more about this component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example module
&lt;/h2&gt;

&lt;p&gt;The solution I linked above uses the ability to modify the lists created by this component, directly from within the module. As you can see, I have attached to two Hooks:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;

&lt;span class="cd"&gt;/** @var array */&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="no"&gt;MODULE_HOOKS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'actionOrderGridDefinitionModifier'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'actionOrderGridQueryBuilderModifier'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The names of these Hooks are dynamic. You may want to hook to the customer list instead of the order list by replacing &lt;em&gt;Order&lt;/em&gt; with &lt;em&gt;Customer&lt;/em&gt;. You can find yet another example that executes hooks like these in the file: &lt;code&gt;/src/Core/Grid/GridFactory.php&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;

&lt;span class="cd"&gt;/**
 * {@inheritdoc}
 */&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getGrid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;SearchCriteriaInterface&lt;/span&gt; &lt;span class="nv"&gt;$searchCriteria&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$definition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;definitionFactory&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getDefinition&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nv"&gt;$data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;dataFactory&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$searchCriteria&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Here it is, hook that will modify the Grid data&lt;/span&gt;
    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;hookDispatcher&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;dispatchWithParameters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'action'&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="nc"&gt;Container&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;camelize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$definition&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getId&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'GridDataModifier'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'data'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]);&lt;/span&gt;

    &lt;span class="nv"&gt;$filterForm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;filterFormFactory&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$definition&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$filterForm&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$searchCriteria&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getFilters&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Grid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nv"&gt;$definition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nv"&gt;$searchCriteria&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nv"&gt;$filterForm&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The Hook name is created as follows:&lt;br&gt;
&lt;code&gt;action&amp;lt;Identifier of a given component&amp;gt;GridDataModifier&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The identifier is for example:&lt;br&gt;
&lt;code&gt;const GRID_ID = 'customer';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This &lt;code&gt;const&lt;/code&gt; is defined here: &lt;code&gt;/src/Core/Grid/Definition/Factory/CustomerGridDefinitionFactory.php&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once we are hooked to the appropriate places in the core, we can execute our code. As it usually happens, in the implementation of a given Hook we get in the &lt;code&gt;$params&lt;/code&gt; array, the appropriate "tools" to be able to modify the selected component.&lt;/p&gt;

&lt;p&gt;In the case of &lt;code&gt;hookActionOrderGridDefinitionModifier&lt;/code&gt;, we have access to the Grid component definition, which is hidden behind the &lt;code&gt;GridDefinitionInterface&lt;/code&gt; implementation.&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;$definition&lt;/code&gt; variable in the code, we can manipulate the columns in the listing. In the case of the sample module, we add the &lt;code&gt;carrier_name&lt;/code&gt; column, which we display after the payment column. How do we know that the payment name column is payment? We can use, for example, the developer tools in the Chrome browser:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.podemski.info%2Fwp-content%2Fuploads%2F2021%2F11%2Fdevtools_example.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.podemski.info%2Fwp-content%2Fuploads%2F2021%2F11%2Fdevtools_example.png" alt="Example dev tools PrestaShop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, we assign a filter to the column so that we can look for orders that should be shipped with the carrier we are looking for:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$filters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$definition&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getFilters&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$filters&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;CARRIER_FIELD_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TextType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setTypeOptions&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
        &lt;span class="s1"&gt;'required'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setAssociatedColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;CARRIER_FIELD_NAME&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code that allows us to retrieve the carrier name is a bit more complicated, but the principle is the same. We get the &lt;code&gt;$params&lt;/code&gt; argument to modify the information retrieval for a given list and we do what we need with it.&lt;/p&gt;

&lt;p&gt;The Grid component does not use the database legacy code, but the Doctrine one, &lt;code&gt;$params['search_query_builder']&lt;/code&gt; is a Doctrine instance of &lt;code&gt;QueryBuilder&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;addSelect&lt;/code&gt; method we can... yes, add the fields we want to retrieve. This works very similar to the DbQuery class that you may now from PrestaShop core.&lt;/p&gt;

&lt;p&gt;It may puzzle you that at this point in the code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$queryBuilder&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;addSelect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'IF(carrier.name = "0", "'&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Configuration&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'PS_SHOP_NAME'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="s1"&gt;'", carrier.name) carrier_name'&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We check that the record is not "0". We do this because PrestaShop will set "0" when we select a carrier, which is added by default in the installer as "In-Store Pickup". This is a relic of the past, which should be straightened out in version 8.0 🙂&lt;/p&gt;

&lt;p&gt;In the following code snippets, we already rely on search criteria. With this code, we control how we sort the listing and possibly filter it.&lt;/p&gt;

&lt;p&gt;The whole process may seem very similar for developers who have already worked with Hooks like &lt;code&gt;actionSomethingListingFieldsModifier&lt;/code&gt;. And, of course, it is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced Grid usage
&lt;/h2&gt;

&lt;p&gt;Of course, the sample module that I give you is the simplest example of how to modify the Grid component. Grid is a very flexible mechanism that will allow you to add your own implementation of various mass actions that can be performed on records. There are also many possibilities of formatting the output data, etc.&lt;/p&gt;

&lt;p&gt;I encourage you to read the &lt;a href="https://devdocs.prestashop.com/" rel="noopener noreferrer"&gt;documentation for developers&lt;/a&gt; and check the repository with more sample PrestaShop modules &lt;a href="https://github.com/PrestaShop/example-modules/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;btw. this is my first article here, your feedback will be a gift 🙏🏻&lt;/p&gt;

</description>
      <category>prestashop</category>
      <category>php</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
