Jump to content

Add custom field on product list


sandra nanou

Recommended Posts

Good morning everyone!

 

I've been struggling for a fews days to add custom fields on product-list.tpl. I added my fields on product.tpl without problem but I can't figure out how to make it work on category pages.

 

Here what I've tried so far :

 

1) Create new column in mytable_product_lang called fab1 and ref1 (text)

 

2) Override classes > Product.php

class Product extends ProductCore
{
  	public $fab1;
	public $ref1;
	 
    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
    {
      	  Product::$definition['fields']['fab1'] = array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isString');
	  Product::$definition['fields']['ref1'] = array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isString');
	  
      parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
    }
 
}

3) Add field on back office (modify information.tpl)

 

4) Modify product.tpl : add lines 

{if isset ($product->fab1)}{$product->fab1|escape:'html':'UTF-8'}{else} - {/if} 
{if isset ($product->ref1)}{$product->ref1|escape:'html':'UTF-8'}{else} - {/if}

Until now everything worked fine.

 

Now I try to add these fields on product-list.tpl.

 

5) Modify override > Category.php, function getProducts

 

I added my 2 new fields in $sql :

        $sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) AS quantity'.(Combination::isFeatureActive() ? ', IFNULL(product_attribute_shop.id_product_attribute, 0) AS id_product_attribute,
					product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity' : '').', pl.`description`, pl.`description_short`, pl.`available_now`,
					pl.`available_later`, pl.`link_rewrite`, pl.`fab1`, pl.`ref1`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, image_shop.`id_image` id_image,
					il.`legend` as legend, m.`name` AS manufacturer_name, cl.`name` AS category_default,
					DATEDIFF(product_shop.`date_add`, DATE_SUB("'.date('Y-m-d').' 00:00:00",
					INTERVAL '.(int)$nb_days_new_product.' DAY)) > 0 AS new, product_shop.price AS orderprice
				FROM `'._DB_PREFIX_.'category_product` cp
				LEFT JOIN `'._DB_PREFIX_.'product` p
					ON p.`id_product` = cp.`id_product`
				'.Shop::addSqlAssociation('product', 'p').
                (Combination::isFeatureActive() ? ' LEFT JOIN `'._DB_PREFIX_.'product_attribute_shop` product_attribute_shop
				ON (p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop='.(int)$context->shop->id.')':'').'
				'.Product::sqlStock('p', 0).'
				LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
					ON (product_shop.`id_category_default` = cl.`id_category`
					AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
				LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
					ON (p.`id_product` = pl.`id_product`
					AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').')
				LEFT JOIN `'._DB_PREFIX_.'image_shop` image_shop
					ON (image_shop.`id_product` = p.`id_product` AND image_shop.cover=1 AND image_shop.id_shop='.(int)$context->shop->id.')
				LEFT JOIN `'._DB_PREFIX_.'image_lang` il
					ON (image_shop.`id_image` = il.`id_image`
					AND il.`id_lang` = '.(int)$id_lang.')
				LEFT JOIN `'._DB_PREFIX_.'manufacturer` m
					ON m.`id_manufacturer` = p.`id_manufacturer`
				WHERE product_shop.`id_shop` = '.(int)$context->shop->id.'
					AND cp.`id_category` = '.(int)$this->id
                    .($active ? ' AND product_shop.`active` = 1' : '')
                    .($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '')
                    .($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : '');


6) Add fields on product-list.tpl :

{if isset($fab1)} {$product.fab1|escape:'html':'UTF-8'} {/if}
{if isset($ref1)} {$product.ref1|escape:'html':'UTF-8'} {/if}

I get nothing. Using {debug}, I can see that none of these fields exist.

 

What am I missing?

 

I'm on prestashop 1.6.1.14.

 

Any help would be so much appreciated!!

 

Thank you,

Sandra

Link to comment
Share on other sites

Hi,

 

Thank you for your answer but I get a fatal error :

Fatal error: Call to protected method ProductCore::_getCustomizationFieldsNLabels() from context '' in /tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 334

 

Where am I supposed to use a foreach ?

 

Thanks again,

Sandra

Link to comment
Share on other sites

Ok, I addded the function and I don't have the error anymore but still no custom field.

 

I've never used the function you mention so I'm not sure of what I'm supposed to do with it.

 

My custom field name is fab1. Could you tell me how to modify your function to display it?

 

Thanks a lot,

Sandra

Link to comment
Share on other sites

  • 9 months later...
On 3/7/2017 at 6:55 PM, sandra nanou said:

Nevermind I found what was wrong. Incompatibility with a module: Advanced search. 

My code was acutally fine!

 

Thanks for your help! 

 

Did you manage to solve the incompatibility? I have the code like you, it works perfectly except when I activate the advanced search, and unfortunately I need the activated module ... can you help me?
Edited by marmad82 (see edit history)
Link to comment
Share on other sites

  • 2 years later...

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