Wednesday, June 2, 2010

Prestashop as Bookstore Part 1 - Authors

I have done several modifications to make PrestaShop usable as a bookstore and I will try to sum it up in a few blog posts.

First off I'll try to explain how I handled authors.

Some others have used eg Manufacturers for this job, renaming Manufacturers to Authors kind of does the job, but there are a couple of issues with this solution. Most important for me was that the authors of the books that I sell often work together with others, and several authors have written many books.
Basically, what I needed was a one to many relationship, ie if I chose a author I'd like to see all books that s/he has written, as a sole author as well as those together with others.
Manufacturers and Distributors don't work like this so I had to look for another solution.

I decided to use Tags. This is not optimal, but for my purposes it works quite good.

First thing to do is translate Tags to Authors (and the equivalent for other languages that you use), using Tools, Translations in the Back Office.

Then I wanted the Tags Block to look like eg the Manufacturers Block.
By looking at the code in modules/blocktags and modules/blockmanufacturer I came up with this for modules/blocktags/blocktags.tpl

<!-- Block tags module -->
<div id="tags_block_left" class="block blockmanufacturer">
<h4>{l s='Tags' mod='blocktags'}</h4>
<div class="block_content">
{if $tags}
<form action="{$smarty.server.SCRIPT_NAME}" method="get">
<p>
<select id="tags_list" onchange="autoUrl('tags_list', '');">
<option value="0">{l s='All tags' mod='blocktags'}</option>
{foreach from=$tags item=tag}
<option value="{$base_dir}search.php?tag={$tag.name|urlencode}">{$tag.name}</option>
{/foreach}
</select>
</p>
</form>
{else}
{l s='No tags specified yet' mod='blocktags'}
{/if}
</div>
</div>
<!-- /Block tags module -->

Then I wanted the author(s) to show when I view the product, in themes/prestashop (or whatever themes folder name you have)/product.tpl
After

<div id="pb-left-column">

add

<div id="short_description_block">

(This is to create a separate block where authors and other information will go, it will show right above the short description block.)

{if $product->tags}
<div id="short_description_content" class="rte align_justify">{l s='Tags'}:
{if $lang_iso == se} {assign var='lng' value=4}
{else} {assign var='lng' value=1}
{/if}
{assign var='tag1' value=$product->tags[$lng]}
{foreach from=$tag1 item=author}
<a href="{$base_dir}search.php?tag={$author}" title="{l s='List books by'} {$author}">{$author}</a>
{/foreach}
</div>
{/if}
</div>

(The last line is of course to end the block, more will be added in this block later.)

All texts inside {l s='...'} can and should be traslated in the BO.

I didn't find what variable contains the numeric code for the language, and it seems to be a great secret because nobody at the PS Forum seems to know either, so I did the ugly workaround using the if statement above.

Now I just added the authors as tags on each book.
Note that it has to be done for each language you use.

The modifications described above were made about six months ago, so I may have forgotten something in the description. Please feel free to ask if something is unclear!

14 comments:

  1. Hi.... excellent Post and nice blog.

    I'm trying to understand if Prestashop is feasible for managing a Bookstore, and found a very helpful information here. Thanks for this.

    And as I've been working with PS in the past with other stores, and regarding your phrase "I didn't find what variable contains the numeric code for the language" I don't know if you are talking about the ID stored in '$cookie->id_lang' or if you are seeking anything else. Just let me know.

    In the meanwhile I will keep looking more information to build a bookstore with PS and will ask you if I have any more information!

    ReplyDelete
  2. Confusing, I couldn't find the comment at first, apparently Blogspot decided it was spam...
    Anyways, thanks and glad you like the blog.
    I've been a bit busy lately, but I hope to be more active again soon.
    And thanks about the tip about $cookie->id_lang, it was exactly what I wanted and now the code looks much better :-)

    ReplyDelete
  3. Sorry, where exactly should the "$cookie->id_lang" expression be inserted in your code to make it look much better?
    Could anyone please show how the final version of the code should be?
    Thank you all in advance.

    ReplyDelete
  4. My "original" code, see above:
    {if $lang_iso == se} {assign var='lng' value=4}
    {else} {assign var='lng' value=1}
    {/if}
    {assign var='tag1' value=$product->tags[$lng]}
    My code after lafondadebacos remark:
    {assign var='tag1' value=$product->tags[$cookie->id_lang]}

    ReplyDelete
  5. Hi can you please expalin in details or email me how to change the Tags from cloud to look like manufacturer menu. I am using 1.4 ver of PS
    email at atharjatoi@gmail.com

    ReplyDelete
  6. I'm sorry, but if the code above doesn't work for 1.4, I cannot help, I've had major problems with my webshop lately and have no time.
    Once I decide to upgrade, I'll look into it.

    ReplyDelete
  7. I updated the code in the blocktags.tpl however no change happened.. it still shows up as tag cloud.. i am using 1.4.1 u can visit my website at meccabooks.com

    ReplyDelete
  8. The original code is cached, go to Preferences - Performance and change the following:
    Force compile: Yes
    Cache: No
    Then update FO and it should have changed, then go back to Performance and change back.

    ReplyDelete
  9. Hi Mohsart,

    thanx for the whole lot: idea + code + explanation. I've been asked to build a bookstore app by a friend and 1st thought of Prestashop. Then it came your blog and advice. Now I've applied it all to my fresh Prestashop installation and it's up and running but have a 'tiny' problem though:

    This one is a local bookstore but with thousands of references in catalog, so the author names input gets a bit too slow. On the other hand I feel incapable of inserting authors in the usual pattern 'surname, name'. So I've decided to move to Drupal, which I know a bit and, despite not being able to resemble such a online shop, lets the admin have more control over every tiny bit.

    I hope this is useful info for anyone visiting these posts. And thanx again for sharing, Moshart.

    Have a nice day!

    ReplyDelete
  10. Nice post Mohsart,

    it was very useful to me when I created a book store for one of my clients but the only think that I can't solve is to have some SEO for the URL. The URL is redirected to the search page and I thought it will be nice to have SEO for that links and also some modifications for the search page.

    In the .htaccess I created this rule
    RewriteRule ^author/(.*)$ search.php?tag=$1
    but I think that is not working properly because of the following line
    RewriteRule ^search$ /search.php [QSA,L]

    How did you solve this, in case you did it ?

    ReplyDelete
  11. I did not :-)
    I really haven't given SEO much thought, especially not as for the listings of books by a certain author.
    My business is a bit special in that there is in practice only one company per country that sells the books I carry.
    Someone from the PrestaShop team recently told me that using the features for authors would give the same behaviour, so you may want to try that out.

    ReplyDelete
  12. HI, can I use this mod for prestashop 1.5.2?

    Thanks!

    ReplyDelete
  13. Probably not straight off, since I made it for 1.3
    It was quite easy to modify to work with 1.4, so to get it working with 1.5 shouldn't be too hard.
    I myself am sticking with 1.4 for this store since 1.5 really isn't all that stable yet, I hear that 1.5.3 is pure disaster.

    ReplyDelete
  14. Hi...I love that suggestions. Can I use for 1.7.6??? Thanks

    ReplyDelete