Jump to content

[SOLVED] Ajax cart title length / truncate / substring


Recommended Posts

Prestashop 1.5

 

On the default theme, there is that cart block on all pages, with the hover effect (top right).

 

I've edited the JS so that now, when you click add to cart, if the title length is more than 11 characters, it will cut it to 9 and add '...'

 

this.name.length > 11 ? this.name.substring(0, 9)

 

Instead of the original 12 cut to 10

 

this.name.length > 12 ? this.name.substring(0, 10)

 

That's great, that works for when you've just added it, but when you reload the page, or load a different page, it's obviously loading the title from somewhere else and is not shortened to the same settings... does anyone know where I have to change to shorten the name when page is loaded?

 

I've looked and everywhere that I thought it might be, hasn't fixed it.

 

Thanks in advance,

 

JD

Edited by jd.creative (see edit history)
Link to comment
Share on other sites

blockcart.tpl located in modules/blockcart or themes/modules/blockcart

 

you've got there:

<a class="cart_block_product_name" href="{$link->getProductLink($product, $product.link_rewrite, $product.category, null, null, $product.id_shop, $product.id_product_attribute)}" title="{$product.name|escape:html:'UTF-8'}">
   {$product.name|truncate:13:'...'|escape:html:'UTF-8'}</a>

 

{$product.name|truncate:13:'...'|escape:html:'UTF-8'}

 

truncate it! :-)

Link to comment
Share on other sites

blockcart.tpl located in modules/blockcart or themes/modules/blockcart

 

you've got there:

<a class="cart_block_product_name" href="{$link->getProductLink($product, $product.link_rewrite, $product.category, null, null, $product.id_shop, $product.id_product_attribute)}" title="{$product.name|escape:html:'UTF-8'}">
{$product.name|truncate:13:'...'|escape:html:'UTF-8'}</a>

 

{$product.name|truncate:13:'...'|escape:html:'UTF-8'}

 

truncate it! :-)

 

Thanks for your comments, I've already tried that, it doesn't seem to work, do I need to clear a cache file or something?

Link to comment
Share on other sites

where you apply changes? in file located in modules/blockcart or in theme/modules/blockcart ?

If you change something in tpl files it is necessary to recompile the theme - so turn force compilation on

Link to comment
Share on other sites

  • 5 months later...

Tanks vekia 

i had that problem and i had truncate the wrong file "blockcart-json.tpl" in the wrong place "/modules/blockcart"

insted of "themes/modules/blockcart.tpl"  :huh:

 

hello majority,

but now, after changing proper file, everything works as you expected?

Link to comment
Share on other sites

Ok I've discovered a fix for the missing characters in the Ajax cart.

It seems this happens only when the prestashop 1.5.4 is updated by the "1-click Upgrade" (not 100% sure)

I've 2 cleans installations of prestashop 1.5.6, one was installed manually (clean install)

the other one was upgraded from 1.5.4 to 1.5.6 by the 1-click Upgrade module,

the one that was installed manually (clean install) have no problems (missing characters) in the cart.

 

So the the logic thing that cross my mind was to study the old files from prestashop 1.5.4 (clean install)

and compare it with the upgraded files from prestshop 1.5.6 (1-click Upgrade)

 

So, after analyzing the file "ajax-cart.js" I found out that the new prestashop 1.5.6 (1-click Upgrade) have this line:

var min = this.name.indexOf(';', 10);

 that the old file "ajax-cart.js" from the prestashop 1.5.4 (clean install) doesn't have

also the line:

var name = (this.name.length > 12 ? this.name.substring(0, ((min - 10) <= 7) ? min : 10) + '...' : this.name); 

is different from the prestashop 1.5.4 (clean install)

 

 

The fix:

  1. Go to modules/blockcart and open the file "ajax-cart.js"  around the line 465 comment out by adding // at beguining of the code line or just delete the line.
    var min = this.name.indexOf(';', 10);
  2. In the same file "ajax-cart.js" around the line 465 find this code:
    var name = (this.name.length > 12 ? this.name.substring(0, ((min - 10) <= 7) ? min : 10) + '...' : this.name);
    

    and play with the values or just replace by this code (with the min values removed):

    var name = (this.name.length > 18 ? this.name.substring(0, 16) + '...' : this.name);
    

 

Hope this helps someone with the same problem as me.

this goes a little off the original topic, should i make a new topic about it?

 

Regards.

Edited by majority (see edit history)
  • Like 2
Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...
  • 3 months later...

Thanks for the solution! it was exactly what I needed. One note here though:

var name = (this.name.length > 12 ? this.name.substring(0, ((min - 10) <= 7) ? min : 10) + '...' : this.name);

"this.name.substring(" alters "this.name", which is used right in the following line, to set the title:

 

content += '<a href="' + this.link + '" title="' + this.name

 

the result is that hovering over the truncated text shows the same truncated text. (easy fix: do "var title=this.name" before this line, and do  "content += '<a href="' + this.link + '" title="' + title ..."  after.)

Edited by JuanCu (see edit history)
Link to comment
Share on other sites

  • 1 year later...

Finally got it to work in PS 1.6 (full txt when add to cart) by changing in yourtheme/js/modules/blockcart/ajax-cart.js

 

 

the following with the desired length:

name = (name.length > 25 ? name.substring(0, 24) + '...' : name);

I precise this is if you want the product name to be displayed fully, or up to xx characters when added to cart without having to refresh

Edited by MilkSheikh (see edit history)
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...