Jump to content

How can I display value in product_list.tpl?


Recommended Posts

Hi guys,

 

I really need help!

 

I use this code to display in product.tpl 

<span class="custom_product_attribute {$belvg_textarea}">{$belvg_textarea}</span>

Why is it not working on product_list.tpl? What is the proper way?

 

 

Below is the module:

class Belvg_SampleModule extends Module {

    public function __construct() {
        $this->name = 'belvg_samplemodule';
        $this->tab = 'front_office_features';
        $this->version = '1.0.0';
        $this->author = 'Belvg';
        $this->need_instance = 0;
        $this->module_key = "";

        parent::__construct();

        $this->displayName = $this->l('Product Custom Fields');
        $this->description = $this->l('Works with displayAdminProductsExtra and actionProductUpdate');
    }

    public function install() {
        $sql = array();
	
        $sql[] = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'belvg_samplemodule` (
                  `id_belvg_samplemodule` int(10) unsigned NOT NULL AUTO_INCREMENT,
                  `id_product` INT( 11 ) UNSIGNED NOT NULL,
                  `textarea` TEXT NOT NULL,
                  PRIMARY KEY (`id_belvg_samplemodule`),
                  UNIQUE  `BELVG_SAMPLE_UNIQ` (  `id_product` )
                ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';
                                
        if (!parent::install() OR 
            !$this->registerHook('displayAdminProductsExtra') OR
            !$this->registerHook('actionProductUpdate') OR
            !$this->registerHook('displayFooterProduct') OR
			!Configuration::updateValue('sample_module_textarea', '') OR
            !$this->runSql($sql)
        ) {
            return FALSE;
        }
        
        return TRUE;
    }
    
    public function uninstall() {
        $sql = array();
	
        $sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'belvg_samplemodule`';
        if (!parent::uninstall() OR
            !$this->runSql($sql) 
        ) {
            return FALSE;
        }

        return TRUE;
    }
    
    public function runSql($sql) {
        foreach ($sql as $s) {
			if (!Db::getInstance()->Execute($s)){
				return FALSE;
			}
        }
        
        return TRUE;
    }
    
    public function hookDisplayAdminProductsExtra($params) {
        $id_product = Tools::getValue('id_product');
        $sampleObj = Belvg_Sample::loadByIdProduct($id_product);
        if(!empty($sampleObj) && isset($sampleObj->id)){
            $this->context->smarty->assign(array(
                'belvg_textarea' => $sampleObj->textarea,
            ));
        }
        
        return $this->display(__FILE__, 'views/admin/sample.tpl');
    }
    
    public function hookActionProductUpdate($params) {
        $id_product = Tools::getValue('id_product');
        $sampleObj = Belvg_Sample::loadByIdProduct($id_product);
        $sampleObj->textarea = Tools::getValue('belvg_sample');
        $sampleObj->id_product = $id_product;
        
        if(!empty($sampleObj) && isset($sampleObj->id)){
            $sampleObj->update();
        } else {
            $sampleObj->add();
        }
    }
    
    public function hookDisplayFooterProduct($params) {
        $id_product = Tools::getValue('id_product');
        $sampleObj = Belvg_Sample::loadByIdProduct($id_product);
        if(!empty($sampleObj) && isset($sampleObj->id)){
            $this->context->smarty->assign(array(
                'belvg_textarea' => $sampleObj->textarea,
            ));
        }
        
        return $this->display(__FILE__, 'views/frontend/sample.tpl');
    }
    
}
Link to comment
Share on other sites

You can't use the variable {$belvg_textarea} directly in product.tpl if you assign it in a hook that displays another tpl.

In your case you can use the variable in views/frontend/sample.tpl .

Define the variable in hookHeader and it will work.

Link to comment
Share on other sites

×
×
  • Create New...