direwald Posted July 12, 2019 Share Posted July 12, 2019 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 More sharing options...
Ciberelfo Posted March 5, 2021 Share Posted March 5, 2021 (edited) Hi! I recently tried to do something similar and had the same problem, I finally fixed it using the params to manually search my database field (return info funtion) , I have uploaded the code on github for help https://github.com/artulance/comentarioadicional Edited August 19, 2023 by Ciberelfo (see edit history) Link to comment Share on other sites More sharing options...
Butch0071 Posted April 3 Share Posted April 3 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now