Jump to content

A solution for using Authors functionality in a bookstore shop


stathis
 Share

Recommended Posts

In bookstore related stores, each book may have one ore more Authors. There is not such or similar functionality in Prestashop that allows making an one-to-many relationship (one product to many authors). 
In addition, the shop must have unique Author's pages that displays some information and lists all books by that author.
From my understanding, not a single theme or module supports this, so it is a common question in the forum.
 
Step1 - How to have an Authors Page with information about the Author and a List of the books he authored
The solution is to use product categories for this purpose.
First create a master category and name it bookauthors (or something similar). This category will not be displayed anywhere. Then, add a subcategory for each Book Author. This way, you can have an Author's page with a friendly url and the ability to add text, images etc.

Step 2 - How to add this information to Product's page:
Next step is to display the Author(s) inside the product's page.
 
2.1: You need to make an addition to classes/product.php file (yes, i know - that tweeks the core functionality). Prestashop v1.6 has remove function "getProductCategoriesParent" while previous versions still have it. In either way, make sure that the function is the one bellow:
 

    public static function getProductCategoriesParent($id_product = '', $id_parent = '', $id_lang = null)
	{
		if (!$id_lang)
			$id_lang = Context::getContext()->language->id;

		$ret = array();
		$row = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
			SELECT cp.`id_category`, cl.`name`, cl.`link_rewrite` FROM `'._DB_PREFIX_.'category_product` cp
			LEFT JOIN `'._DB_PREFIX_.'category` c ON (c.id_category = cp.id_category)
			LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cp.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').')
			'.Shop::addSqlAssociation('category', 'c').'
			WHERE cp.`id_product` = '.(int)$id_product.' AND c.`id_parent` = '.(int)$id_parent.' AND cl.`id_lang` = '.(int)$id_lang //<-- bug was in this line - originally there's cl.'id_parent'
		);

		foreach ($row as $val)
			$ret[$val['id_category']] = $val;

		return $ret;
	} 

2.2: Next, you add the code bellow to product.tpl page, in any place you want to display book's Authors:

<ul>
{foreach from=Product::getProductCategoriesParent(Tools::getValue('id_product'),[add here the bookauthors category ID]) item=cat}
<li><a href="{$link->getCategoryLink({$cat.id_category})}" title="{$cat.name}">{$cat.name}</a></li>
{/foreach}
</ul>

PLEASE NOTE: You MUST change the [add here the bookauthors category ID] with the category ID of the bookauthors  master category.
 
I hope this helps someone out there!

Share this post


Link to post
Share on other sites

  • 4 years later...

Sir I tried this approach, but was not able to retrieve the category in product page. My category id is 52 and I modified the code like below,

<ul>
{foreach from=Product::getProductCategoriesParent(Tools::getValue('id_product'),[52]) item=cat}
<li><a href="{$link->getCategoryLink({$cat.id_category})}" title="{$cat.name}">{$cat.name}</a></li>
{/foreach}
</ul>

also, I cleared the cache from performance tab after making the change. I am not good at writing SQL queries - so you are my only hope.

 

authors.JPG

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More