Add product to basket via Javascript (jQuery) in Prestashop 1.7.5.0

懵懂的女人 提交于 2020-01-24 22:45:08

问题


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).

So basically:

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

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

回答1:


Add products by name is bad idea. You need id_product and id_product_attribute (0 - if product don't have variants).

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

Search source code for <form action="http://fo.demo.prestashop.com/pl/koszyk" method="post" id="add-to-cart-or-refresh">

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


来源:https://stackoverflow.com/questions/53979360/add-product-to-basket-via-javascript-jquery-in-prestashop-1-7-5-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!