Jump to content

Custom product field shows as empty on front end even when filled in DB (PS 1.7.5.2)


Recommended Posts

Hello, I've got an issue that I'm trying to solve for several hours now and I'm no closer to figuring it out than when I started. I've created a custom module to add additional product field to product page in BO and subsequently show it on product page at front office.

The module adds a column to product_lang table, overrides Product.php with the new field and installs itselfs onto hooks used in BO. The BO stuff works fine, the field updates in DB properly, it shows up in BO when it's filled etc. The issue I have is at front office, where the field shows as empty, even though it's filled with text in database. The field itself is there in {$product|print_r}, it just shows as empty. 

I've no idea why that happens. I've added dozens of custom fields in PS1.6 and never had an issue at this step. Once it was in the database, it showed fine at FO. But here, I'm at wits end.

Github of the module - https://github.com/direwald/ps_module/tree/master/customproductfields

Module code

	<?php

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

class CustomProductFields extends Module {

    public function __construct() {
        $this->name = 'customproductfields';
        $this->tab = 'administration';
        $this->version = '1.0.1';
        $this->author = 'RV';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = [
            'min' => '1.6',
            'max' => _PS_VERSION_
        ];
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Custom Admin Product Fields');
        $this->description = $this->l('Adds custom field to admin product page');

        $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');

        if (!Configuration::get('customproductfields')) {
            $this->warning = $this->l('No name provided');
        }
    }

    public function install() {
        return parent::install()
            && $this->_installSql()
            && $this->registerHook('displayAdminProductsExtra')
            && $this->registerHook('displayAdminProductsMainStepLeftColumnMiddle')
            && $this->registerHook('actionProductSave');
    }

    public function uninstall() {
        if (!parent::uninstall() 
        || !$this->_unInstallSql()
        || !Configuration::deleteByName('customproductfields')
        ) {
            return false;
        }

        return true;
    }

    protected function _installSql() { 
        $sqlInstallLang = "ALTER TABLE "._DB_PREFIX_."product_lang" 
                ." ADD custom_desc text NULL";
 
        $returnSqlLang = Db::getInstance()->execute($sqlInstallLang);
 
        return $returnSqlLang; 
    }


    protected function _unInstallSql() { 
         $sqlInstallLang = "ALTER TABLE "._DB_PREFIX_."product_lang" 
                 ." DROP custom_desc";
  
         $returnSqlLang = Db::getInstance()->execute($sqlInstallLang);
  
         return $returnSqlLang; 
     }

    public function hookDisplayAdminProductsExtra ($params) 
    {
        $product = new Product ($params['id_product']);
        $this->smarty->assign(array(
            'product_detail' => $product,
            'id_product' => $params['id_product'],
            'current_url' => 'index.php?controller=AdminModules&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
        ));
        return $this->display(__FILE__, 'views/templates/hook/displayAdminProductsMainStepLeftColumnMiddle.tpl');
 
    }

    public function displayAdminProductsMainStepLeftColumnMiddle($params)
    {
        $product = new Product ($params['id_product']);
        $this->smarty->assign(array(
            'product_detail' => $product,
            'id_product' => $params['id_product'],
            'current_url' => 'index.php?controller=AdminModules&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
        ));
        return $this->display(__FILE__, 'views/templates/hook/displayAdminProductsMainStepLeftColumnMiddle.tpl');
    }


    public function hookActionProductSave($params) {
        if ((int)$params['object']->id?(int)$params['object']->id:Tools::getValue('id_product')) {
            $id_product = (int)$params['object']->id?(int)$params['object']->id:Tools::getValue('id_product');
            $custom_desc = Tools::getValue('custom_desc');

            $sql = 'UPDATE '._DB_PREFIX_.'product_lang
                    SET custom_desc = "'.$custom_desc.'",
                    WHERE id_product = '.$id_product;
            $result = Db::getInstance()->execute($sql);
        }  
     }


}

Module Product.php override

<?php


class Product extends ProductCore {

    public $custom_desc;
    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
    {
        self::$definition['fields']['custom_desc'] = array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml');
        parent::__construct($id_product , $full , $id_lang, $id_shop,$context );
    }

}

 

Link to comment
Share on other sites

  • 1 year later...
  • 3 years later...

You should be able to access your field in FO tpl as : {$product.custom_desc} or {$product.custom_desc nofilter}

The only thing I see is that in your functions ex hookDisplayAdminProductsExtra ($params) you are not passing language information - but you probably do not need them

public function hookDisplayAdminProductsExtra ($params) 
    {
        $product = new Product ($params['id_product']);
$languages = Language::getLanguages($active);
        $this->smarty->assign(array(
            'product_detail' => $product,
            'id_product' => $params['id_product'],
            'current_url' => 'index.php?controller=AdminModules&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
'languages' => $languages,
'default_language' => $this->context->employee->id_lang,
        ));
        return $this->display(__FILE__, 'views/templates/hook/displayAdminProductsMainStepLeftColumnMiddle.tpl');
 
    }

    

 

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...