Shopify出道主题中的多文件上传器

阿达利德·博里·帕尔玛(Adalid Bori Palma)

我正在自定义页面以上传多个文件(照片)。这是我到目前为止拥有的代码,根据该链接,它应该可以正常工作:链接中的https://shopify.dev/tutorials/customize-theme-get-customization-information-for-products转到“允许文件上传”如果您要检查。

{% form 'product', product, class:form_classes, data-product-form: '', enctype:"multipart/form-data" %}
            {% unless product.has_only_default_variant %}
              <div class="product-form__controls-group">
                {% for option in product.options_with_values %}
                  <div class="selector-wrapper js product-form__item">

                    <select class="single-option-selector single-option-selector-{{ section.id }} product-form__input"
                      id="SingleOptionSelector-{{ forloop.index0 }}"
                      data-index="option{{ forloop.index }}"
                    >
                      {% for value in option.values %}
                        <option value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
                      {% endfor %}
                    </select>
                  </div>
                {% endfor %}
              </div>
            {% endunless %}

            <select name="id" id="ProductSelect-{{ section.id }}" class="product-form__variants no-js">
              {% for variant in product.variants %}
                <option value="{{ variant.id }}"
                  {%- if variant == current_variant %} selected="selected" {%- endif -%}
                >
                  {{ variant.title }}  {%- if variant.available == false %} - {{ 'products.product.sold_out' | t }}{% endif %}
                </option>
              {% endfor %}
            </select>

            {% if section.settings.show_quantity_selector %}
            {% comment %}
              <div class="product-form__controls-group">
                <div class="product-form__item">
                  <input type="number" id="Quantity-{{ section.id }}"
                    name="quantity" value="1" min="1" pattern="[0-9]*"
                    class="product-form__input product-form__input--quantity" data-quantity-input
                  >
                </div>
              </div>

                <div class="qtydiv">
                {% comment %}<label for="Quantity" class="quantity-selector">Quantity</label>{% endcomment %}
                <div class="qtybox">
                  <span class="btnqty qtyminus icon icon-minus">-</span>
                  <input type="text" id="quantity" name="quantity" value="1" min="1" class="quantity-selector quantity-input" readonly="">
                  <span class="btnqty qtyplus icon icon-plus">+</span>
                </div>
              </div>
        {% endcomment %}
            {% endif %}

            <div class="product-form__error-message-wrapper product-form__error-message-wrapper--hidden{% if section.settings.enable_payment_button %} product-form__error-message-wrapper--has-payment-button{% endif %}"
              data-error-message-wrapper
              role="alert"
            >
              <span class="visually-hidden">{{ 'general.accessibility.error' | t }} </span>
              {% include 'icon-error' %}
              <span class="product-form__error-message" data-error-message>{{ 'products.product.quantity_minimum_message' | t }}</span>
            </div>

            <div class="product-form__controls-group product-form__controls-group--submit">
              <div class="qtydiv">
                {% comment %}<label for="Quantity" class="quantity-selector">Quantity</label>{% endcomment %}
                <div class="qtybox">
                  <span class="btnqty qtyminus icon icon-minus">-</span>
                  <input type="text" id="quantity" name="quantity" value="1" min="1" class="quantity-selector quantity-input" readonly="">
                  <span class="btnqty qtyplus icon icon-plus">+</span>
                </div>
              </div>
              <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>
                  <span class="hide" data-loader>
                    {% include 'icon-spinner' %}
                  </span>
                </button>
                {% if section.settings.enable_payment_button %}
                  {{ form | payment_button }}
                {% endif %}
              </div>
                    <input required class="required product-form__input" id="photo" type="file" name="properties[Photo]" multiple>
              </div>
            </div>
          {% endform %}

这是我的表格,根据链接必须具有属性

enctype = "multipart / form-data".

在表单的底部,输入类型=“ file”。

<input required class="required product-form__input" id="photo" type="file" name="properties[Photo]" multiple>

这是购物车template.liquid中的代码。

<div class="list-view-item__title">
                      <a href="{{ item.url }}" class="cart__product-title" data-cart-item-title>
                        {{ item.product.title }}<br>
                        {% assign property_size = item.properties | size %}
                          {% if property_size > 0 %}
                            {% for p in item.properties %}
                                {{ item.properties.count }}
                              {% assign first_character_in_key = p.first | truncate: 1, '' %}
                              {% unless p.last == blank or first_character_in_key == '_' %}
                                {{ p.first }}:
                                {% if p.last contains '/uploads/' %}
                                  <a class="lightbox" href="{{ p.last }}">{{ property_size }}</a>
                                {% else %}
                                  {{ p.last }}
                                {% endif %}
                                <br>
                              {% endunless %}
                            {% endfor %}
                          {% endif %}
                      </a>
                    </div>

问题是即使我上传两个或更多图像,property_size变量也将获得值一,而实际上我应该返回该项目具有的属性量。谁能帮我吗?我做错了什么?

matheusb.comp

自上载本教程以来,“首演”主题可能已更改,并且由于不受支持,因此可能不会更新。

如果添加type="text"输入而不是type="file"属性,则按预期添加属性。发生这种情况是因为_initAddToCart函数(assets/theme.js第6447行)获取了表单HTML元素并将其传递给另一个函数。

var $data = $(this.selectors.productForm, this.$container);
this._addItemToCart($data);

然后在_addItemToCart函数(assets/theme.js第6513行)中,将表单序列化为字符串:

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

该函数将忽略文件输入,如jQuery文档中所述:

Data from file select elements is not serialized.

要发送文件,可以使用FormData浏览器API。但是要与jQuery AJAX调用一起使用必须设置一些属性

var params = {
  url: '/cart/add.js',
  // data: $(data).serialize(),
  dataType: 'json',
  // Disable the jQuery data processing, and send the FormData object
  contentType: false,
  processData: false,
  data: new FormData($(data).get()[0])
};
$.post(params).done(...

但是,由于HTML文件<input>具有name="properties[Photo]",因此只有最后上传的文件将被添加到购物车项目的Photo属性中。我尝试使用name="properties[Photo][]",但是该cart/add.js调用仅返回一条错误500,并显示消息“内部服务器错误”。

因此,要上传多个文件,您可以向该_addItemToCart函数添加一些自定义代码,以从中获取每个文件<input>使用每个文件的不同键将其添加到FormData中

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

reactjs 形式的多文件上传器

来自分类Dev

Shopify Mobilia主题中的神秘边距/填充

来自分类Dev

如何更改 Shopify 'Brooklyn' 主题中付款图标的大小?

来自分类Dev

Xpages主题中的文件路径设置

来自分类Dev

Xpages主题中的文件路径设置

来自分类Dev

Wordpress 子主题中的多个 css 文件

来自分类Dev

无法上传主题中的图片。nopCommerce MVC

来自分类Dev

无法覆盖WordPress子主题中的主题文件

来自分类Dev

Laravel上传多文件在控制器内部获取NULL

来自分类Dev

多文件上传完成

来自分类Dev

多文件上传识别

来自分类Dev

多文件上传错误

来自分类Dev

多文件上传错误

来自分类Dev

多文件上传识别

来自分类Dev

如何通过自定义Shopify应用在主题中插入自定义HTML?

来自分类Dev

使用GastbyJS + Storefront API在shopify主题中包含应用程序液体

来自分类Dev

Shopify - 我可以从浏览器控制台访问 JSON 对象,但不能从主题文件访问

来自分类Dev

从WordPress主题中的Kirki定制器字段获取值

来自分类Dev

子主题中有多个.css文件

来自分类Dev

无法在gitbook主题中添加自定义模板文件

来自分类Dev

在布局文件中引用内置主题中的颜色

来自分类Dev

WordPress文件未在主题设置中上传

来自分类Dev

前端多文件上传WordPress

来自分类Dev

多文件上传未保存

来自分类Dev

多篇文章-上传文件

来自分类Dev

FTP多文件上传PHP

来自分类Dev

多文件上传php for循环

来自分类Dev

上传文件的验证器

来自分类Dev

React - 文件上传器