无法将产品添加到购物车ERROR prestashop AJAX

时间:2016-01-04 23:24:58

标签: json ajax prestashop prestashop-1.5

我的网站工作正常,直到本周初,每当客户尝试添加产品时,都会出现此错误;

  

无法将产品添加到购物车中。

     

textStatus:' parsererror'

     

errorThrown:' SyntaxError:JSON.parse:在JSON数据的第1行第3列的JSON数据之后出现意外的非空白字符'

     

responseText的:

     

45B

     

{   "产品":[.....} 0

但如果我把https://www.domain.com.au代替www.domain.com.au,那么AJAX不会抛出任何错误。

我在这里几乎没有猜测,但我没有解决方案; 我能想到的是我需要删除空格,但我不知道在哪里。而且我放入blockcart.php的代码导致了它;

  

全球$ smarty;

     

$ smarty-> assign(' customerDefaultGroup',Customer :: getDefaultGroupId(Context :: getContext() - > customer-> id));

但我试图评论这些,它仍然让我错误。 那么现在,我禁用了AJAX,但是客户已经开始抱怨每次添加产品时都会被引导到购物车。

我不知道这是否相关,每次访问网站时,我的标题都会随机显示。

帮助

按照我的ajax-cart.js添加功能;

// add a product in the cart via ajax
    add : function(idProduct, idCombination, addedFromProductPage, callerElement, quantity, whishlist){
        if (addedFromProductPage && !checkCustomizations())
        {
            alert(fieldRequired);
            return ;
        }
        emptyCustomizations();
        //disabled the button when adding to not double add if user double click
        if (addedFromProductPage)
        {
            $('#add_to_cart input').attr('disabled', true).removeClass('exclusive').addClass('exclusive_disabled');
            $('.filled').removeClass('filled');
        }
        else
            $(callerElement).attr('disabled', true);

        if ($('#cart_block_list').hasClass('collapsed'))
            this.expand();
        //send the ajax request to the server
        $.ajax({
            type: 'POST',
            headers: { "cache-control": "no-cache" },
            url: baseUri + '?rand=' + new Date().getTime(),
            async: true,
            cache: false,
            dataType : "json",
            data: 'controller=cart&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)
            {
                // add appliance to whishlist module
                if (whishlist && !jsonData.errors)
                    WishlistAddProductCart(whishlist[0], idProduct, idCombination, whishlist[1]);

                // add the picture to the cart
                var $element = $(callerElement).parent().parent().find('a.product_image img,a.product_img_link img');
                if (!$element.length)
                    $element = $('#bigpic');
                var $picture = $element.clone();
                var pictureOffsetOriginal = $element.offset();

                if ($picture.size())
                    $picture.css({'position': 'absolute', 'top': pictureOffsetOriginal.top, 'left': pictureOffsetOriginal.left});

                var pictureOffset = $picture.offset();
                if ($('#cart_block')[0] && $('#cart_block').offset().top && $('#cart_block').offset().left)
                    var cartBlockOffset = $('#cart_block').offset();
                else
                    var cartBlockOffset = $('#shopping_cart').offset();

                // Check if the block cart is activated for the animation
                if (cartBlockOffset != undefined && $picture.size())
                {
                    $picture.appendTo('body');
                    $picture.css({ 'position': 'absolute', 'top': $picture.css('top'), 'left': $picture.css('left'), 'z-index': 4242 })
                    .animate({ 'width': $element.attr('width')*0.66, 'height': $element.attr('height')*0.66, 'opacity': 0.2, 'top': cartBlockOffset.top + 30, 'left': cartBlockOffset.left + 15 }, 1000)
                    .fadeOut(100, function() {
                        ajaxCart.updateCartInformation(jsonData, addedFromProductPage);
                    });
                }
                else
                    ajaxCart.updateCartInformation(jsonData, addedFromProductPage);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown)
            {
                document.write("Impossible to add the product to the cart.\n\ntextStatus: '" + textStatus + "'\nerrorThrown: '" + errorThrown + "'\nresponseText:\n" + XMLHttpRequest.responseText);
                //reactive the button when adding has finished
                if (addedFromProductPage)
                    $('#add_to_cart input').removeAttr('disabled').addClass('exclusive').removeClass('exclusive_disabled');
                else
                    $(callerElement).removeAttr('disabled');
            }
        });
    },

我的ajax-cart.js自定义以检查最小数量

var href = $(this).attr('href').split('=');
            if(typeof href[4] === "undefined")
            { qty = href[3];} //array position changed to 3 after URL rewrite enabled.
            else{ qty = href[4];}

我的blockcart-jsan.tpl代码;

{ldelim}
"products": [
{if $products}
{foreach from=$products item=product name='products'}
{assign var='productId' value=$product.id_product}
{assign var='productAttributeId' value=$product.id_product_attribute}
    {ldelim}
        "id":            {$product.id_product},
        "link":          "{$link->getProductLink($product.id_product, $product.link_rewrite, $product.category, null, null, $product.id_shop, $product.id_product_attribute)|addslashes|replace:'\\\'':'\''}",
        "quantity":      {$product.cart_quantity},
        "priceByLine":   "{if $priceDisplay == $smarty.const.PS_TAX_EXC}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total}{else}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total_wt}{/if}",
        "name":          "{$product.name|html_entity_decode:2:'UTF-8'|escape:'htmlall'|truncate:15:'...':true}",
        "price":         "{if $priceDisplay == $smarty.const.PS_TAX_EXC}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total}{else}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total_wt}{/if}",
        "price_float":   "{$product.total}",
        "idCombination": {if isset($product.attributes_small)}{$productAttributeId}{else}0{/if},
        "idAddressDelivery": {if isset($product.id_address_delivery)}{$product.id_address_delivery}{else}0{/if},
        "is_gift" : {if isset($product.is_gift) && $product.is_gift}1{else}0{/if},
{if isset($product.attributes_small)}
        "hasAttributes": true,
        "attributes":    "{$product.attributes_small|addslashes|replace:'\\\'':'\''}",
{else}
        "hasAttributes": false,
{/if}
        "hasCustomizedDatas": {if isset($customizedDatas.$productId.$productAttributeId)}true{else}false{/if},
        "customizedDatas":[
        {if isset($customizedDatas.$productId.$productAttributeId[$product.id_address_delivery])}
        {foreach from=$customizedDatas.$productId.$productAttributeId[$product.id_address_delivery] key='id_customization' item='customization' name='customizedDatas'}{ldelim}
{* This empty line was made in purpose (product addition debug), please leave it here *}
            "customizationId":  {$id_customization},
            "quantity":         "{$customization.quantity}",
            "datas": [
                {foreach from=$customization.datas key='type' item='datas' name='customization'}
                {ldelim}
                    "type": "{$type}",
                    "datas":
                    [
                    {foreach from=$datas key='index' item='data' name='datas'}
                        {ldelim}
                        "index":            {$index},
                        "value":            "{$data.value|addslashes|replace: '\\\'':'\''}",
                        "truncatedValue":   "{$data.value|truncate:28:'...'|addslashes|replace: '\\\'':'\''}"
                        {rdelim}{if !$smarty.foreach.datas.last},{/if}
                    {/foreach}]
                {rdelim}{if !$smarty.foreach.customization.last},{/if}
                {/foreach}
            ]
        {rdelim}{if !$smarty.foreach.customizedDatas.last},{/if}
        {/foreach}
        {/if}
        ]
    {rdelim}{if !$smarty.foreach.products.last},{/if}
{/foreach}{/if}
],
"discounts": [
{if $discounts}{foreach from=$discounts item=discount name='discounts'}
    {ldelim}
        "id":              "{$discount.id_discount}",
        "name":            "{$discount.name|cat:' : '|cat:$discount.description|truncate:18:'...'|addslashes|replace:'\\\'':'\''}",
        "description":     "{$discount.description|addslashes|replace:'\\\'':'\''}",
        "nameDescription": "{$discount.name|cat:' : '|cat:$discount.description|truncate:18:'...'|addslashes|replace:'\\\'':'\''}",
        "code":            "{$discount.code}",
        "link":            "{$link->getPageLink("$order_process", true, NULL, "deleteDiscount={$discount.id_discount}")}",
        "price":           "{if $priceDisplay == 1}{convertPrice|html_entity_decode:2:'UTF-8' price=$discount.value_tax_exc}{else}{convertPrice|html_entity_decode:2:'UTF-8' price=$discount.value_real}{/if}",
        "price_float":     "{if $priceDisplay == 1}{$discount.value_tax_exc}{else}{$discount.value_real}{/if}"
    {rdelim}
    {if !$smarty.foreach.discounts.last},{/if}
{/foreach}{/if}
],
"shippingCost": "{$shipping_cost|html_entity_decode:2:'UTF-8'}",
"shippingCostFloat": "{$shipping_cost_float|html_entity_decode:2:'UTF-8'}",
{if isset($tax_cost)}
"taxCost": "{$tax_cost|html_entity_decode:2:'UTF-8'}",
{/if}
"wrappingCost": "{$wrapping_cost|html_entity_decode:2:'UTF-8'}",
"nbTotalProducts": "{$nb_total_products}",
"total": "{$total|html_entity_decode:2:'UTF-8'}",
"productTotal": "{$product_total|html_entity_decode:2:'UTF-8'}",
{if isset($errors) && $errors}
"hasError" : true,
"errors" : [
{foreach from=$errors key=k item=error name='errors'}
    "{$error|addslashes|html_entity_decode:2:'UTF-8'}"
    {if !$smarty.foreach.errors.last},{/if}
{/foreach}
]
{else}
"hasError" : false
{/if}
{rdelim}

0 个答案:

没有答案