通过Prestashop 1.7.5.0中的Javascript(jQuery)将产品添加到购物篮 [英] Add product to basket via Javascript (jQuery) in Prestashop 1.7.5.0
问题描述
我正在为prestashop 1.7创建一个主题,然后尝试从javascript(jQuery)创建一个ajax调用,该调用将具有特定名称的产品添加到购物篮中. (我通读了文档,看过模块,用了谷歌搜索了几个小时,但是没有运气.)
I'm creating a theme for prestashop 1.7 and I try to create an ajax call from javascript (jQuery) that adds the product with a specific name to the shoppingbasket. (I read through documentation, looked at modules, googled for hours, but no luck).
所以基本上:
<button id="buyProduct" data-productname="myProduct">Buy Product</button>
$('#buyProduct).click(function(){
var productname = $(this).data('productname');
// Do Prestashop Magic
});
推荐答案
按名称添加产品是个坏主意.您需要id_product和id_product_attribute(0-如果产品没有变体).
Add products by name is bad idea. You need id_product and id_product_attribute (0 - if product don't have variants).
最简单的方式使其形成类似于产品页面上的形式. http://fo.demo.prestashop.com/pl/men/1-1-hummingbird-printed-t-shirt.html#/1-rozmiar-s/8-kolor-bialy
Simplest way it is made form similar to that on product page. http://fo.demo.prestashop.com/pl/men/1-1-hummingbird-printed-t-shirt.html#/1-rozmiar-s/8-kolor-bialy
搜索<form action="http://fo.demo.prestashop.com/pl/koszyk" method="post" id="add-to-cart-or-refresh">
这是preastahop js代码(在core.js中),可添加到购物车:
This is preastahop js code (in core.js) for add to cart:
$body.on('click', '[data-button-action="add-to-cart"]', function (event) {
event.preventDefault();
if ((0, _jquery2['default'])('#quantity_wanted').val() > (0, _jquery2['default'])('[data-stock]').data('stock') && (0, _jquery2['default'])('[data-allow-oosp]').data('allow-oosp').length === 0) {
(0, _jquery2['default'])('[data-button-action="add-to-cart"]').attr('disabled', 'disabled');
} else {
var _ret = (function () {
var $form = (0, _jquery2['default'])(event.target).closest('form');
var query = $form.serialize() + '&add=1&action=update';
var actionURL = $form.attr('action');
var isQuantityInputValid = function isQuantityInputValid($input) {
var validInput = true;
$input.each(function (index, input) {
var $input = (0, _jquery2['default'])(input);
var minimalValue = parseInt($input.attr('min'), 10);
if (minimalValue && $input.val() < minimalValue) {
onInvalidQuantity($input);
validInput = false;
}
});
return validInput;
};
var onInvalidQuantity = function onInvalidQuantity($input) {
$input.parents('.product-add-to-cart').first().find('.product-minimal-quantity').addClass('error');
$input.parent().find('label').addClass('error');
};
var $quantityInput = $form.find('input[min]');
if (!isQuantityInputValid($quantityInput)) {
onInvalidQuantity($quantityInput);
return {
v: undefined
};
}
_jquery2['default'].post(actionURL, query, null, 'json').then(function (resp) {
_prestashop2['default'].emit('updateCart', {
reason: {
idProduct: resp.id_product,
idProductAttribute: resp.id_product_attribute,
linkAction: 'add-to-cart',
cart: resp.cart
},
resp: resp
});
}).fail(function (resp) {
_prestashop2['default'].emit('handleError', { eventType: 'addProductToCart', resp: resp });
});
})();
if (typeof _ret === 'object') return _ret.v;
}
});
这篇关于通过Prestashop 1.7.5.0中的Javascript(jQuery)将产品添加到购物篮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!