Jump to content

Modifying product title for non logged in users


G-Man

Recommended Posts

Hi Guys,

 

I want to use Prestashop for a wholesale order site if a user is logged in. A non logged in user will see the shop in view mode only. This is achieved by modifying config\config.inc.php with the following code:

if (!defined('_PS_ADMIN_DIR_'))
{
    if (Context::getContext()->customer->isLogged() ? Configuration::set('PS_CATALOG_MODE', 0) : Configuration::set('PS_CATALOG_MODE', 1));
}

There maybe a better place for this code, ideally I would create an override but not sure where to put it but it works. If anyone has a better way of doing this I am open to suggestions.

 

My next problem is that I wanted to parse a the product title and short description by parsing the name and short description, so I overrode the product core constructor but it only sort of worked when a single product was selected and not at all for the product lists. I have pasted my test override code below, but I feel I might be going down the wrong path as there doesn't seem to be a centralised get that I could find for the name and short description. Anyone have any ideas? My code that doesn't really work below:

<?php
/*
 * Override the product name
 */
class Product extends ProductCore {

	public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
	{
		parent::__construct($id_product, $id_lang, $id_shop, $context);
		//Only parse the parse string for non admin page and non logged in users
		if (!defined('_PS_ADMIN_DIR_') and Context::getContext()->customer->isLogged() )
		{
			$arraylen= count($this->name);
			$this->name = parseString($this->name);
			$this->description_short = parseString($this->description_short);
		}
	}
	function parseString($stringToParse)
	{
		return $stringToParse.'SomeAddedToEnd';
	}
}
Link to comment
Share on other sites

btw. $this->name = parseString($this->name); this works? instead of it you should use $this->name = $this->parseString($this->name);

 

 

to use the same parse function for product names on categories listing page - you have to modify category controller too.

there is an array with products, you have to do the same for each iteration of array.

Link to comment
Share on other sites

Thanks for the reply, much appreciated. You were correct, that code didn't work as it was a modified version of the test code I was using. I have made some progress, and have decided to implement a module with some new fields added to the product which are editable from the admin panel. The module is working well and I have added a new field to the admin panel. The product override code pasted below works but just for an individual product, I guess now I have override the category controller as you mentioned.

 

I do have a  couple of questions as I am new to php and prestashop development:

 

1. When I place product.php override in the \prestashoproot\modules\mymodule\override\classes folder it is not picked up and not run but when I place it in the \prestashoproot\override\classes folder it works fine. I am using Prestashop 1.5.6.1, is this a known issue or am I doing something wrong?

 

2. I assign a value $this->name but when I am debugging this in eclipse it appears that the name variable is actually an array of length 3 and array elements 2 and 3 appear to be the category name but the definition is 'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 128). So I was expecting different values for elements 2 and 3, like the lang, and validate values? What is going on here?

<?php
/**
 * Override the product name*/
class Product extends ProductCore 
{
	/** @var string retail_product_name  - this is the field consumers will see for the product name*/
	public $retail_product_name;
	
	/* override the constructor to parse the name and short description
	*/
	public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
	{
		// addtional retail_product_name is a field which gets edited in the admin panel, attributes not complete yet this is test
		self::$definition['fields']['retail_product_name'] = array('type' => self::TYPE_STRING, 'lang' => true, 'size' => 128);

		parent::__construct($id_product, $id_lang, $id_shop, $context);
		// Only parse the string for non admin page and non logged in users
		if (!defined('_PS_ADMIN_DIR_') and !Context::getContext()->customer->isLogged() )
		{
			// $arraylen= count($this->name);
			$this->name = $this->retail_product_name;
		}
	}
}
Edited by G-Man (see edit history)
Link to comment
Share on other sites

Hi, Thanks for the help. I did try adding code to the category controller but I found it didn't work for home featured products. Instead I ended up doing it all with a class override of productcore. Code pasted below. This I hope is written well enough to be useful to others.

<?php
/**
 * Override the product name*/
class Product extends ProductCore 
{
	/** @var string retail_product_name  - this is the field consumers will see for the product name*/
	public $retail_product_name;
	
	/* override the constructor to parse the name and short description
	*/
	public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
	{
		// addtional retail_product_name is a field which gets edited in the admin panel, attributes not complete yet this is test
		self::$definition['fields']['retail_product_name'] = array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => false, 'size' => 128);
		parent::__construct($id_product, $id_lang, $id_shop, $context);
		// Only parse the string for non admin page and non logged in users
		if (!defined('_PS_ADMIN_DIR_') && !Context::getContext()->customer->isLogged() && isset($this->retail_product_name) && !empty($this->retail_product_name))
		{
			// $arraylen= count($this->name);
			$this->name = $this->retail_product_name;
		}
		
	}
	public static function getProductProperties($id_lang, $row, Context $context = null)
	{
		if (!defined('_PS_ADMIN_DIR_') && !Context::getContext()->customer->isLogged())
		{
			$sql = 'SELECT * FROM `'._DB_PREFIX_.'product_lang` WHERE `id_product` = '.(int)$row['id_product'].' AND `id_lang` = '.(int)$id_lang;
			//$sql = 'SELECT * FROM \'._DB_PREFIX_.\'shop WHERE id_shop = 42';
			$rowp = Db::getInstance()->getRow($sql);
			if(isset($rowp['retail_product_name']) && !empty($rowp['retail_product_name']))
				$row['name'] = $rowp['retail_product_name'];
		}
		return parent::getProductProperties($id_lang, $row, $context);
	}
	
}	
Link to comment
Share on other sites

I am definitely chipping away at this but my current biggest frustration is not being able to debug the php code of my custom module because the modules folder doesn't show up in eclipse under my project. Anyone have any ideas on that one?

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