通过Prestashop 1.7.5.0中的Javascript(jQuery)将产品添加到购物篮

时间:2018-12-30 16:27:10

标签: jquery ajax themes prestashop prestashop-1.7

我正在为prestashop 1.7创建一个主题,然后尝试从javascript(jQuery)创建一个ajax调用,该调用将具有特定名称的产品添加到购物篮中。 (我通读了文档,看过模块,用了谷歌搜索了几个小时,但是没有运气。)

所以基本上:

<button id="buyProduct" data-productname="myProduct">Buy Product</button>

$('#buyProduct).click(function(){
  var productname = $(this).data('productname');
  // Do Prestashop Magic
});

1 个答案:

答案 0 :(得分:0)

按名称添加产品是个坏主意。您需要id_product和id_product_attribute(0-如果产品没有变体)。

最简单的方式使其形成类似于产品页面上的形式。 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中),可添加到购物车:

      $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;
    }
  });