Jump to content

get variable in product-list.tpl


almeidaz

Recommended Posts

Hi,

 

I try to create module to add custom_field with HookAdminProductExtra

 

i can see and get  {$custom_field} in product page (Product.tpl) but not in product-list page (product-list.tpl)

 

there is my code in module.php :

<?php

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

class newFieldsTut extends Module
{
	/* @var boolean error */
	protected $_errors = false;
	
	public function __construct()
	{
		$this->name = 'newfieldstut';
		$this->tab = 'front_office_features';
		$this->version = '1.0';
		$this->author = 'Nemo';
		$this->need_instance = 1;
		$this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');

	 	parent::__construct();

		$this->displayName = $this->l('New Fields Tutorial');
		$this->description = $this->l('Test module from Nemo\'s Post Scriptum Tutorial (nemops.com).');
		$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
   	 	$this->warning = $this->l('No name provided');

	}
	
	public function install()
	{
		if (!parent::install() OR
			!$this->alterTable('add') OR			
			!$this->registerHook('actionAdminControllerSetMedia') OR
			!$this->registerHook('DisplayProductDeliveryTime') OR
			!$this->registerHook('actionProductUpdate') OR
			!$this->registerHook('displayAdminProductsExtra'))
			return false;
		return true;
	}
	
	public function uninstall()
	{
		if (!parent::uninstall() OR !$this->alterTable('remove'))
			return false;
		return true;
	}


	public function alterTable($method)
	{
		switch ($method) {
			case 'add':
				$sql = 'ALTER TABLE ' . _DB_PREFIX_ . 'product_lang ADD `custom_field` TEXT NOT NULL';
				break;
			
			case 'remove':
				$sql = 'ALTER TABLE ' . _DB_PREFIX_ . 'product_lang DROP COLUMN `custom_field`';
				break;
		}
		
		if(!Db::getInstance()->Execute($sql))
			return false;
		return true;
	}

	public function prepareNewTab()
	{

		$this->context->smarty->assign(array(
			'custom_field' => $this->getCustomField((int)Tools::getValue('id_product')),
			'languages' => $this->context->controller->_languages,
			'default_language' => (int)Configuration::get('PS_LANG_DEFAULT')
		));

	}

	public function hookDisplayAdminProductsExtra($params)
	{
		if (Validate::isLoadedObject($product = new Product((int)Tools::getValue('id_product'))))
		{
			$this->prepareNewTab();

			
			return $this->display(__FILE__, 'views/templates/admin/newfieldstut.tpl');
		}
	}	

	public function hookActionAdminControllerSetMedia($params)
	{

		// add necessary javascript to products back office
		if($this->context->controller->controller_name == 'AdminProducts' && Tools::getValue('id_product'))
		{
			$this->context->controller->addJS($this->_path.'/js/newfieldstut.js');
		}

	}

	public function hookActionProductUpdate($params)
	{
		// get all languages
		// for each of them, store the custom field!

		$id_product = (int)Tools::getValue('id_product');
		$languages = Language::getLanguages(true);
		foreach ($languages as $lang) {
			if(!Db::getInstance()->update('product_lang', array('custom_field'=> pSQL(Tools::getValue('custom_field_'.$lang['id_lang']))) ,'id_lang = ' . $lang['id_lang'] .' AND id_product = ' .$id_product ))
				$this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
					}


	}

	public function getVariable()
	{
    $this->context->smarty->assign('custom_field', $this->getCustomField((int)Tools::getValue('id_product')));
	}

  
    
	
    public function hookDisplayProductDeliveryTime($params)
    {

        $this->getVariable();
        return $this->display(__FILE__, 'views/templates/front/custom.tpl');

    }

    public function getCustomField($id_product)
	{
    $result = Db::getInstance()->getValue('SELECT custom_field FROM '._DB_PREFIX_.'product_lang WHERE id_product = ' . (int)$id_product . ' AND id_lang = ' . $this->context->language->id);
    if(!$result)
        return false;

    return $result;
	}

	public function getCustomFieldTest($id_product)
	{
		$result = Db::getInstance()->ExecuteS('SELECT custom_field, id_lang FROM '._DB_PREFIX_.'product_lang WHERE id_product = ' . (int)$id_product);
		if(!$result)
			return array();

		foreach ($result as $field) {
			$fields[$field['id_lang']] = $field['custom_field'];
		}

		return $fields;
	}

	

}

and my custom.tpl

<div>{$custom_field}</div>
<div id="text" class="montext">
this is custom field
</div>

i can see "this is custom field" on each product when i m on product-list page  at DisplayProductDeliveryTime

but <div>{$custom_field}</div> doesn't appear (because doesnt' exist or found i suppose)

 

Do you have any idea how  to get {$custom_field} in product-list view ?

 

Thanks for help.

Edited by almeidaz (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...