我在prestashop 1.6下开发了一个模块来管理产品的包装(每箱瓶装和每箱X瓶的包装盒),本地的prestashop不提供此解决方案,因此我进行了开发,但是我对请求中的ajax有所限制购物篮摘要。
我希望当我输入2的数量时,如果它的包装数是3 /纸箱,那么它将输入的数量更新为6,所以3 x 2 = 6。
我设法更新了prestashop的默认数量输入,但是ajax请求没有开始更新价格。
这是我已经做过的:
HTML / Smarty(shopping-cart-product-line.tpl):
<td class="cart_quantity text-center" data-title="{l s='Quantity'}">
{if (isset($cannotModify) && $cannotModify == 1)}
<span>
{if $quantityDisplayed == 0 AND isset($customizedDatas.$productId.$productAttributeId)}
{$product.customizationQuantityTotal}
{else}
{$product.cart_quantity-$quantityDisplayed}
{/if}
</span>
{else}
{if isset($customizedDatas.$productId.$productAttributeId) AND $quantityDisplayed == 0}
<span id="cart_quantity_custom_{$product.id_product}_{$product.id_product_attribute}_{$product.id_address_delivery|intval}">{$product.customizationQuantityTotal}</span>
{/if}
<script>
var colisage = '{$product.colisage}';
</script>
{if !isset($customizedDatas.$productId.$productAttributeId) OR $quantityDisplayed > 0}
<input type="hidden"
value="{if $quantityDisplayed == 0 AND isset($customizedDatas.$productId.$productAttributeId)}{$customizedDatas.$productId.$productAttributeId|@count}{else}{$product.cart_quantity-$quantityDisplayed}{/if}"
name="quantity_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}_hidden"/>
<input size="2" type="text" autocomplete="off" class="cart_quantity_input form-control grey"
value="{if $quantityDisplayed == 0 AND isset($customizedDatas.$productId.$productAttributeId)}{$customizedDatas.$productId.$productAttributeId|@count}{else}{$product.cart_quantity-$quantityDisplayed}{/if}"
name="quantity_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}"/>
<input class="cart_quantity_input_custom form-control" type="text" value="" name="quantity_custom_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}"/>
<div class="cart_quantity_button clearfix">
{if $product.minimal_quantity < ($product.cart_quantity-$quantityDisplayed) OR $product.minimal_quantity <= 1}
<a rel="nofollow" class="cart_quantity_down btn btn-default button-minus"
id="cart_quantity_down_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}"
href="{$link->getPageLink('cart', true, NULL, "add=1&id_product={$product.id_product|intval}&ipa={$product.id_product_attribute|intval}&id_address_delivery={$product.id_address_delivery|intval}&op=down&token={$token_cart}")|escape:'html':'UTF-8'}"
title="{l s='Subtract'}">
<span><i class="icon-minus"></i></span>
</a>
{else}
<a class="cart_quantity_down btn btn-default button-minus disabled" href="#"
id="cart_quantity_down_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}"
title="{l s='You must purchase a minimum of %d of this product.' sprintf=$product.minimal_quantity}">
<span><i class="icon-minus"></i></span>
</a>
{/if}
<a rel="nofollow" class="cart_quantity_up btn btn-default button-plus"
id="cart_quantity_up_{$product.id_product}_{$product.id_product_attribute}_{if $quantityDisplayed > 0}nocustom{else}0{/if}_{$product.id_address_delivery|intval}"
href="{$link->getPageLink('cart', true, NULL, "add=1&id_product={$product.id_product|intval}&ipa={$product.id_product_attribute|intval}&id_address_delivery={$product.id_address_delivery|intval}&token={$token_cart}")|escape:'html':'UTF-8'}"
title="{l s='Add'}"><span><i class="icon-plus"></i></span></a>
</div>
{/if}
{/if}
</td>
JS(cart-summary.js):
$(document).ready(function(){
$('.cart_quantity_up').off('click').on('click', function(e){
e.preventDefault();
upQuantity($(this).attr('id').replace('cart_quantity_up_', ''));
$('#' + $(this).attr('id').replace('_up_', '_down_')).removeClass('disabled');
// Colisage calcul
$('.cart_quantity_input_custom').val($('.cart_quantity_input').val() / colisage +1);
});
$('.cart_quantity_down').off('click').on('click', function(e){
e.preventDefault();
downQuantity($(this).attr('id').replace('cart_quantity_down_', ''));
// Colisage calcul
$('.cart_quantity_input_custom').val($('.cart_quantity_input').val() / colisage -1);
});
$('.cart_quantity_delete' ).off('click').on('click', function(e){
e.preventDefault();
deleteProductFromSummary($(this).attr('id'));
});
$('.cart_address_delivery').on('change', function(e){
changeAddressDelivery($(this));
});
$(document).on('click', '.voucher_name', function(e){
$('#discount_name').val($(this).data('code'));
});
$('.cart_quantity_input').typeWatch({
highlight: true, wait: 600, captureLength: 0, callback: function(val){
updateQty(val, true, this.el);
}
});
// Colisage calcul
$('.cart_quantity_input_custom').val($('.cart_quantity_input').val() / colisage);
cleanSelectAddressDelivery();
refreshDeliveryOptions();
$('.delivery_option_radio').on('change', function(){
refreshDeliveryOptions();
});
$('#allow_seperated_package').on('click', function(){
$.ajax({
type: 'POST',
headers: { "cache-control": "no-cache" },
url: baseUri + '?rand=' + new Date().getTime(),
async: true,
cache: false,
dataType: 'json',
data: 'controller=cart&ajax=true'
+ '&summary=true'
+ '&allowSeperatedPackage=true'
+ '&value='
+ ($(this).prop('checked') ? '1' : '0')
+ '&token='+static_token
+ '&allow_refresh=1',
success: function(jsonData)
{
if (jsonData.hasError)
{
var errors = '';
for(var error in jsonData.errors)
//IE6 bug fix
if(error !== 'indexOf')
errors += $('<div />').html(jsonData.errors[error]).text() + "\n";
if (!!$.prototype.fancybox)
$.fancybox.open([
{
type: 'inline',
autoScale: true,
minHeight: 30,
content: '<p class="fancybox-error">' + errors + '</p>'
}],
{
padding: 0
});
else
alert(errors);
$('input[name=quantity_'+ id +']').val($('input[name=quantity_'+ id +'_hidden]').val());
}
else
{
if (jsonData.refresh)
window.location.href = window.location.href;
updateCartSummary(jsonData.summary);
if (window.ajaxCart != undefined)
ajaxCart.updateCart(jsonData);
updateHookShoppingCart(jsonData.HOOK_SHOPPING_CART);
updateHookShoppingCartExtra(jsonData.HOOK_SHOPPING_CART_EXTRA);
if (typeof(getCarrierListAndUpdate) !== 'undefined')
getCarrierListAndUpdate();
if (typeof(updatePaymentMethodsDisplay) !== 'undefined')
updatePaymentMethodsDisplay();
}
}
});
});
$('#gift').checkboxChange(function(){
$('#gift_div').show('slow');
}, function(){
$('#gift_div').hide('slow');
});
$('#enable-multishipping').checkboxChange(
function(){
$('.standard-checkout').hide(0);
$('.multishipping-checkout').show(0);
},
function(){
$('.standard-checkout').show(0);
$('.multishipping-checkout').hide(0);
}
);
});
function upQuantity(id, qty)
{
// Colisage
if (typeof(colisage) !== 'undefined' || colisage) {
qty = colisage;
}
if (typeof(qty) == 'undefined' || !qty)
qty = 1;
var customizationId = 0;
var productId = 0;
var productAttributeId = 0;
var id_address_delivery = 0;
var ids = 0;
ids = id.split('_');
productId = parseInt(ids[0]);
if (typeof(ids[1]) !== 'undefined')
productAttributeId = parseInt(ids[1]);
if (typeof(ids[2]) !== 'undefined' && ids[2] !== 'nocustom')
customizationId = parseInt(ids[2]);
if (typeof(ids[3]) !== 'undefined')
id_address_delivery = parseInt(ids[3]);
$.ajax({
type: 'POST',
headers: { "cache-control": "no-cache" },
url: baseUri + '?rand=' + new Date().getTime(),
async: true,
cache: false,
dataType: 'json',
data: 'controller=cart'
+ '&ajax=true'
+ '&add=true'
+ '&getproductprice=true'
+ '&summary=true'
+ '&id_product=' + productId
+ '&ipa=' + productAttributeId
+ '&id_address_delivery=' + id_address_delivery
+ ((customizationId !== 0) ? '&id_customization=' + customizationId : '')
+ '&qty=' + qty
+ '&token=' + static_token
+ '&allow_refresh=1',
success: function(jsonData)
{
if (jsonData.hasError)
{
var errors = '';
for(var error in jsonData.errors)
//IE6 bug fix
if(error !== 'indexOf')
errors += $('<div />').html(jsonData.errors[error]).text() + "\n";
if (!!$.prototype.fancybox)
$.fancybox.open([
{
type: 'inline',
autoScale: true,
minHeight: 30,
content: '<p class="fancybox-error">' + errors + '</p>'
}],
{
padding: 0
});
else
alert(errors);
$('input[name=quantity_'+ id +']').val($('input[name=quantity_'+ id +'_hidden]').val());
}
else
{
if (jsonData.refresh)
window.location.href = window.location.href;
updateCartSummary(jsonData.summary);
if (window.ajaxCart != undefined)
ajaxCart.updateCart(jsonData);
if (customizationId !== 0)
updateCustomizedDatas(jsonData.customizedDatas);
updateHookShoppingCart(jsonData.HOOK_SHOPPING_CART);
updateHookShoppingCartExtra(jsonData.HOOK_SHOPPING_CART_EXTRA);
if (typeof(getCarrierListAndUpdate) !== 'undefined')
getCarrierListAndUpdate();
if (typeof(updatePaymentMethodsDisplay) !== 'undefined')
updatePaymentMethodsDisplay();
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (textStatus !== 'abort')
{
error = "TECHNICAL ERROR: unable to save update quantity \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus;
if (!!$.prototype.fancybox)
$.fancybox.open([
{
type: 'inline',
autoScale: true,
minHeight: 30,
content: '<p class="fancybox-error">' + error + '</p>'
}],
{
padding: 0
});
else
alert(error);
}
}
});
}
function downQuantity(id, qty)
{
// Colisage
if (typeof(colisage) !== 'undefined' || colisage) {
qty = colisage;
}
var val = $('input[name=quantity_' + id + ']').val();
var newVal = val;
if(typeof(qty) == 'undefined' || !qty)
{
qty = 1;
newVal = val - 1;
}
else if (qty < 0)
qty = -qty;
var customizationId = 0;
var productId = 0;
var productAttributeId = 0;
var id_address_delivery = 0;
var ids = 0;
var qtyColisage = $('.cart_quantity_input').val() / qty;
$('.cart_quantity_input_custom').val(qtyColisage);
ids = id.split('_');
productId = parseInt(ids[0]);
if (typeof(ids[1]) !== 'undefined')
productAttributeId = parseInt(ids[1]);
if (typeof(ids[2]) !== 'undefined' && ids[2] !== 'nocustom')
customizationId = parseInt(ids[2]);
if (typeof(ids[3]) !== 'undefined')
id_address_delivery = parseInt(ids[3]);
if (newVal > 0 || $('#product_' + id + '_gift').length)
{
$.ajax({
type: 'POST',
headers: { "cache-control": "no-cache" },
url: baseUri + '?rand=' + new Date().getTime(),
async: true,
cache: false,
dataType: 'json',
data: 'controller=cart'
+ '&ajax=true'
+ '&add=true'
+ '&getproductprice=true'
+ '&summary=true'
+ '&id_product='+productId
+ '&ipa='+productAttributeId
+ '&id_address_delivery='+id_address_delivery
+ '&op=down'
+ ((customizationId !== 0) ? '&id_customization='+customizationId : '')
+ '&qty='+qty
+ '&token='+static_token
+ '&allow_refresh=1',
success: function(jsonData)
{
if (jsonData.hasError)
{
var errors = '';
for(var error in jsonData.errors)
//IE6 bug fix
if(error !== 'indexOf')
errors += $('<div />').html(jsonData.errors[error]).text() + "\n";
if (!!$.prototype.fancybox)
$.fancybox.open([
{
type: 'inline',
autoScale: true,
minHeight: 30,
content: '<p class="fancybox-error">' + errors + '</p>'
}],
{
padding: 0
});
else
alert(errors);
$('input[name=quantity_' + id + ']').val($('input[name=quantity_' + id + '_hidden]').val());
}
else
{
if (jsonData.refresh)
window.location.href = window.location.href;
updateCartSummary(jsonData.summary);
if (window.ajaxCart !== undefined)
ajaxCart.updateCart(jsonData);
if (customizationId !== 0)
updateCustomizedDatas(jsonData.customizedDatas);
updateHookShoppingCart(jsonData.HOOK_SHOPPING_CART);
updateHookShoppingCartExtra(jsonData.HOOK_SHOPPING_CART_EXTRA);
if (newVal == 0)
$('#product_' + id).hide();
if (typeof(getCarrierListAndUpdate) !== 'undefined')
getCarrierListAndUpdate();
if (typeof(updatePaymentMethodsDisplay) !== 'undefined')
updatePaymentMethodsDisplay();
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (textStatus !== 'abort')
alert("TECHNICAL ERROR: unable to save update quantity \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
}
});
}
else
{
deleteProductFromSummary(id);
}
}
当我单击添加或删除按钮时,它可以正常工作。另一方面,如果我把那只硬纸板的量还给那只手,它就不会叫任何ajax。
谢谢您的帮助。