prestashop fix empty cart cookie overwritted

Hi guys

I found a way to fix this issue.

After many hours of trace code I found that cookie is being overwrited when ajax cart is enabled.

This is the cause that the order was lost and cart appears empty and this is the explaination :

– Supose the first time you visit the shop, no cookies are on your browser yet.

– When you add a product using ajax cart ( using a ajax_add_to_cart_button class button ) you are using this piece of code :
[shell]$(‘.ajax_add_to_cart_button’).unbind(‘click’).click(function(){
var idProduct =  $(this).attr(‘rel’).replace(‘ajax_id_product_’, »);
if ($(this).attr(‘disabled’) != ‘disabled’)
ajaxCart.add(idProduct, null, false, this);
return false;
});[/shell]
So you call ajaxCart.add() when you add a product.

Inside ajaxCart.add() there are two significant actions that modify your cookie:

first one is expand the cartblock. Basically open the cart box and send an ajax petition to include ‘expand’ parameter into your cookie
[shell]if ($(‘#cart_block #cart_block_list’).hasClass(‘collapsed’))
this.expand();[/shell]
and inside expand() the expand action the visual box slidedown :
[shell]$(‘#’+parentId+’ #cart_block #cart_block_summary’).slideUp(200, function(){
$(this).addClass(‘collapsed’).removeClass(‘expanded’);
$(‘#’+parentId+’ #cart_block #cart_block_list’).slideDown({
duration: 600,
complete: function(){$(this).addClass(‘expanded’).removeClass(‘collapsed’);}
});
});[/shell]
and store cookie value  :
[shell]$.ajax({
type: ‘GET’,
url: baseDir + ‘modules/blockcart/blockcart-set-collapse.php’,
async: true,
data: ‘ajax_blockcart_display=expand’ + ‘&rand=’ + new Date().getTime()
});[/shell]
Second one, adds the product in your cart.
[shell]$.ajax({
type: ‘POST’,
url: baseDir + ‘cart.php’,
async: true,
cache: false,
dataType : «json»,
data: ‘add=1&ajax=true&qty=’ + ((quantity && quantity != null) ? quantity : ‘1’) + ‘&id_product=’ + idProduct + ‘&token=’ + static_token + ( (parseInt(idCombination) && idCombination != null) ? ‘&ipa=’ + parseInt(idCombination): »),
success: function(jsonData,textStatus,jqXHR)
{
….
….
….

});[/shell]
Well, tracing the code using phpfire I could see that requests are sent in this order :

1.- blockcart-set-collapse.php

2.- cart.php

In cases that works fine I got the return of the request in same order, but when cart gets empty, I reicived cart.php firt and then blockcart-set-collapse.php

This means that blockcart-set-collapse.php is requested without id_cart cookie value, so return a cookie without id_cart.

So id_cart is generated in cart.php call, and blockcart-set-collapse.php returns the cookie after cart.php, so the id_cart is not preserved because is being overwritted.

There are few ways to fix this :

1.- At ajaxCart.add() disable this.expand() , avoid to made ajax call

2.- at ajaxCart.expand() put ajax request before slideDown() call

3.- The second point and made ajaxcall syncronious setting async : false

[shell]$.ajax({
type: ‘GET’,
url: baseDir + ‘modules/blockcart/blockcart-set-collapse.php’,
async: false,
data: ‘ajax_blockcart_display=expand’ + ‘&rand=’ + new Date().getTime()
});[/shell]
Another way to fix this, is to have ever a id_cart at cookie.

I’m not sure but I think at previous prestashop ( 1.3.x) id_cart was stored at cookie in the first request.

At Prestashop 1.4.x cart is generated at FrontControllerCore::init() but not saved even asigned to the cookie.

Reviewing the code quickly, I see that the id_cart is asigned when a product is added to the cart.

So another solution could be to store a cart in cookies ever.

I hope this could help you to fix your shops !

I continue investigating this issue

PounStudio Team !

5 respuestas a «prestashop fix empty cart cookie overwritted»

  1. Can we have the modified file ?
    And how to «have ever a id_cart at cookie» ?

    «at ajaxCart.expand() put ajax request before slideDown() call» ?
    Just before this line «$(‘#header #cart_block #cart_block_summary’).slideUp(200, function(){» ??
    Don’t work for me. 1.5.2.

  2. Hi Pio.

    Maybe need to study a bit the code, but out ajaxCart.expand() function is

    expand : function(){
    $(['left_column', 'right_column']).each(function(id, parentId)
    {
    if ($('#'+parentId+' #cart_block #cart_block_list').hasClass('collapsed'))
    {

    // avoid cookie overwrite with cart.php query
    // save the expand statut in the user cookie
    $.ajax({
    type: 'GET',
    url: baseDir + 'modules/blockcart/blockcart-set-collapse.php',
    async: false,
    data: 'ajax_blockcart_display=expand' + '&rand=' + new Date().getTime()
    });

    $('#'+parentId+' #cart_block #cart_block_summary').slideUp(200, function(){
    $(this).addClass('collapsed').removeClass('expanded');
    $('#'+parentId+' #cart_block #cart_block_list').slideDown({
    duration: 600,
    complete: function(){$(this).addClass('expanded').removeClass('collapsed');}
    });
    });
    // toogle the button expand/collapse button
    $('#'+parentId+' #cart_block h4 span#block_cart_expand').fadeOut('slow', function(){
    $('#'+parentId+' #cart_block h4 span#block_cart_collapse').fadeIn('fast');
    });

    }
    });
    },

  3. Saludos,
    Soy nuevo en prestashop, y le estoy creando una tienda a un familiar; pero al concluirla, veo que siempre mi shoping cart esta vacia, ahunque quiera comprar productos.
    Podria Ud, ayudarme con esto.

    gracias de antemano,

    Jorge

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

 

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.