添加产品后,如何在不刷新购物车页面的情况下自动查看更新的购物车?

问题描述

我尝试了几种方法,但是每次成功将产品添加到购物车中,但购物车本身都没有更新时,我现在已经三天了才能完成此任务,请记住我对此是完全陌生的,并且我已经对更改的方法和某种代码进行了大量研究,但是我对如何实现这一点完全迷失了。我正在使用Shopify,唯一要实现的是添加新产品时产品购物车会更新,它确实可以处理数量(当您在购物车页面上并简单地更改数量时,然后自动更新而不刷新)

如果您缺少一些代码,请直接说出来,以便我轻松更新。

我尝试过的代码

theme.UpdateRefresh = (function() {

  var refresh = document.getElementsByClassName('.product-form .product-form-product-template');
  var test = ('[data-product-form]');

  $('btn product-form__cart-submit').click(function() {
    location.reload();
});

  function update()
  {
    url: '/cart/add'
    dataType: 'json'
  }
  return { update:update}
})   ();

_addItemToCart(theme.js)

    _addItemToCart: function(data) {
      var params = {
        url: '/cart/add.js',data: $(data).serialize(),dataType: 'json'
      };

      $.post(params)
        .done(
          function(item) {
            this._hideErrorMessage();
            this._setupCartPopup(item); //cartpop met additem 
            this.theme.UpdateRefresh.update();
          }.bind(this)
        )
        .fail(
          function(response) {
            this.$prevIoUslyFocusedElement.focus();
            var errorMessage = response.responseJSON
              ? response.responseJSON.description
              : theme.strings.cartError;
            this._showErrorMessage(errorMessage);
            this._handleButtonLoadingState(false);
          }.bind(this)
        );
    },

Shopify(product-template.liquid)

<div class="product-form__controls-group product-form__controls-group--submit">
              <div class="product-form__item product-form__item--submit
                    {%- if section.settings.enable_payment_button %} product-form__item--payment-button {%- endif -%}
                    {%- if product.has_only_default_variant %} product-form__item--no-variants {%- endif -%}">
                <button type="submit" name="add"
                  {% unless current_variant.available %} aria-disabled="true"{% endunless %}
                  aria-label="{% unless current_variant.available %}{{ 'products.product.sold_out' | t }}{% else %}{{ 'products.product.add_to_cart' | t }}{% endunless %}"
                  class="btn product-form__cart-submit{% if section.settings.enable_payment_button %} btn--secondary-accent{% endif %}"
                  data-add-to-cart>
                  <span data-add-to-cart-text>
                    {% unless current_variant.available %}
                      {{ 'products.product.sold_out' | t }}
                    {% else %}
                      {{ 'products.product.add_to_cart' | t }}
                    {% endunless %}
                  </span>
                </button>
              </div> 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)