Jump to content

Display custom data in template with sql query


pranab13
 Share

Recommended Posts

Hi everyone,

 

I have a new data in my database linked to the customer table and I would like to display it front on the identity.tpl.

We need two tables for that :

PS_customer -> I need the 'id_customer' either with a join a the sql query or we can get it from the page displayed

PS_othertable -> a custom table with 'id_customer', 'lecode' and others that we don't need here.

 

I want to display "lecode" which is a client code on the identity.tpl.

My prefix isn't PS_.

I have Prestashop 1.6.1.10

I guess the query would be something like

'SELECT `lecode` FROM `'.DB_PREFIX_.'othertable` WHERE `id_customer` =`.the id $variable of the current customer displaying the identty page.`';

On the identity.tpl I know that Context::getContext()->customer-id will get me the id of the current customer but isn't there another way to get it as it doesn't seem to work with my query.

And I guess that the query should be put somewhere in a class or the identity controllers as a static function but I don't know how to do all that. I tried several things but it didn't work. I don't know if it's my syntax that isn't right or the variables, I don't know how to call the function front on the template with the id_customer parameter....

 

Did someone already do that ?

Can someone help me ?

Edited by pranab13 (see edit history)

Share this post


Link to post
Share on other sites

  • 11 months later...

I found something since then to add custom data to the identity template on Prestashop.

It works front and displays the client code as I wished but on the back office there is the "wasn't installed correctly" error, but it works...

 

I created a module (which you can name as you want, I named it "zecustomercode"). Its file's hierarchy is the following :

-zecustomercode (folder)

    --index.php (file)

    --zecustomercode.php (file)

    -- classes (folder)

        ---clientcode.php (file)

        ---index.php (file)

    --views (folder)

        ---index.php (file)

        ---templates (folder)

            ----index.php (file)

            ----hook (folder)

                -----zecustomercode.tpl (file)

                -----index.php (file)

 

Code for zecustomercode.php :

<?php
/**
 * 2007-2017 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to [email protected] so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 *  @author 
 *  @copyright  2007-2018
 *  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 **/

class ZeCustomerCode extends Module
{
    public function __construct()
    {
        $this->name = 'zecustomercode';
        $this->tab = 'front_office_features';
        $this->version = '1.3.0';
        $this->author = 'Anaïs';
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Client code');
        $this->description = $this->l('Displays client code.');
        $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');

        if (!Configuration::get('lecode')) {
            $this->warning = $this->l('ZeCustomerCode wasn\'t installed correctly');
        }
    }

    public function install()
    {
        if (!parent::install() || !$this->registerHook('displayClientCode')) {
                return false;
        }
        return true;
    }
    
    public function uninstall()
    {
        return parent::uninstall();
    }
    
    public static function getClientCode($id_customer = null)
    {
        if (!Validate::isUnsignedId($id_customer)) 
        {
            die(Tools::displayError('Erreur 04 : veuillez contacter le webmaster suite à cette erreur.'));
        }
        
        $cache_id = 'ZeCustomerCode::getClientCode_'.(int)$id_customer;
		if (!Cache::isStored($cache_id)) 
        {
            $query = new DbQuery();
            $query->from('zecustomercode', 'zc');
            $query->innerJoin('customer', 'c', 'c.id_customer = zc.id_customer');
            $query->where('zc.id_customer = ' . (int) $id_customer);
            $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query);
            Cache::store($cache_id, $result);
        }
        return Cache::retrieve($cache_id);
    }

    public function hookDisplayClientCode()
    {
        $codes = ZeCustomerCode::getClientCode($this->context->customer->id);
        if (isset($codes)) {
            $this->context->smarty->assign('lecode', $codes['lecode']
            );
        } else {
            $this->context->smarty->assign('zcustomer', 'Code client non trouvé');
        }
        return $this->display(__FILE__, 'zcustomer.tpl');
    }
}

 

 

Code for clientcode.php

<?php
/**
 * 2007-2018 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to [email protected] so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 *  @author 
 *  @copyright  2007-2018
 *  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 **/

class ZeCustomerCode extends ObjectModel
{

    /** @var string Client code */
    public $lecode;
    
    public static $definition = array(
        'table' => 'zecustomercode',
        'primary' => 'id_customer',
        'fields' => array(
            'code_ERP' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'size' => 255)
        ),
    );
}

 

Code for the template :

<p id="client-code">
    {l s='Your client code is' mod='zecustomercode'} <strong class="blue">{$lecode|escape:'htmlall':'UTF-8'}</strong>
</p>

 

Edited by pranab13 (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More