Jump to content

Adding a custom variable to the product detail page


Recommended Posts

Hello guys

I am really new to prestashop and i'm just playing around with it and trying to get some tutorials done.

Now i have an idea, for which i could not find any solution yet, although it seems really simple to me.

I would like to calculate something and show the calculated value into a tpl file. What do you think is the best way of doing anything like that? Should i create a new module, or hook this function somewhere or should i modify any controller.php?

I make an example, just for simplicity... it does not make any sense (yet) but it's just to get the point:
Assume I want to display the quantity available of a product and its reserved quantity (reserved = amount bought but not shipped) in the detail page of this product. I know there is a "product-add-to-cart.tpl" inside the classic theme, where i would like to print my values just below the "add to cart"-button. how should i proceed? where should i put my sql-query to fetch these values and then pass them to the desired tpl-file?

Thanks in advance

Regards

Link to comment
Share on other sites

Module (create folder my_module and put this script my_module.php):

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class my_module extends Module
{
  
  public function __construct()
    {
        $this->name = 'my_module';
        $this->tab = 'other';
        $this->version = '1.0.0.0';
        $this->author = '';
        $this->author_email = '';
        $this->author_website = '';
        $this->bootstrap = true;
        parent::__construct();

        $this->displayName = 'Add my text to product detail page';
        $this->description = 'Module add my text to product page';
        $this->confirmUninstall = 'Uninstall?';
        $this->ps_versions_compliancy = array('min' => '1.7.0', 'max' => '1.7.6.9');
        $this->default_lang = (int)Configuration::get('PS_LANG_DEFAULT'); 
        
    }

	public function uninstall()
    { 

        
        if (Shop::isFeatureActive())
        {
            Shop::setContext(Shop::CONTEXT_ALL);
        }
        
        if (!parent::uninstall())
        {
            return false;
        }
        
  
        return true;
    }

	public function install() 
    {
        if (Shop::isFeatureActive())
        {
            Shop::setContext(Shop::CONTEXT_ALL);
        }
        
        if (!parent::install())
        {
            return false;
        }
        
		$this->registerHook('DisplayProductAdditionalInfo');

        return true;
    }

	public function hookDisplayProductAdditionalInfo($params)
	{
		$id_product = Tools::getValue('id_product');
		$my_query = Db::getInstance()->getValue('SELECT my_value FROM'._DB_PREFIX_.'my_table WHERE id_product = '.$id_product);
		if ($my_query){
			return '<span class="my-variable">'.$my_query.'</span>';
		} else {
			return '';
		}
	}
  
}

 

Link to comment
Share on other sites

Or create custom hook and add to product-add-to-cart.tpl

Module:

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class my_module extends Module
{
  
  public function __construct()
    {
        $this->name = 'my_module';
        $this->tab = 'other';
        $this->version = '1.0.0.0';
        $this->author = '';
        $this->author_email = '';
        $this->author_website = '';
        $this->bootstrap = true;
        parent::__construct();

        $this->displayName = 'Add my text to product detail page';
        $this->description = 'Module add my text to product page';
        $this->confirmUninstall = 'Uninstall?';
        $this->ps_versions_compliancy = array('min' => '1.7.0', 'max' => '1.7.6.9');
        $this->default_lang = (int)Configuration::get('PS_LANG_DEFAULT'); 
        
    }

	public function uninstall()
    { 

        $id_hook = Hook::getIdByName('displayMyProductActions');
        $hook = new Hook($id_hook);
        $hook->delete();

        if (Shop::isFeatureActive())
        {
            Shop::setContext(Shop::CONTEXT_ALL);
        }
        
        if (!parent::uninstall())
        {
            return false;
        }
        
  
        return true;
    }

	public function install() 
    {
        if (Shop::isFeatureActive())
        {
            Shop::setContext(Shop::CONTEXT_ALL);
        }
        
        if (!parent::install())
        {
            return false;
        }
        
		if (!$id_hook = Hook::getIdByName('displayMyProductActions')) {
            $hook = new Hook();
            $hook->name = 'displayMyProductActions';
            $hook->title = 'My new hook';
            $hook->description = 'This is a custom hook!';
            $hook->position = 1;
            $hook->add(); // return true on success
        }

		$this->registerHook('displayMyProductActions');

        return true;
    }

	public function hookDisplayMyProductActions($params)
	{
		$id_product = Tools::getValue('id_product');
		$my_query = Db::getInstance()->getValue('SELECT my_value FROM'._DB_PREFIX_.'my_table WHERE id_product = '.$id_product);
		if ($my_query){
			return '<span class="my-variable">'.$my_query.'</span>';
		} else {
			return '';
		}
	}
  
}

 

HOOK in product-add-to-cart.tpl:

{hook h='displayMyProductActions' product=$product}

obrazek.png.7216a9274c01bfcbbcd922080e264f99.png

 

 

Edited by Guest (see edit history)
Link to comment
Share on other sites

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
×
×
  • Create New...