如何在购物车中添加产品 Shopify 通过仅采用产品名称字段的表单?



我在Shopify中制作了一个表单页面,其中我采取了以下字段:-

<form  action="/cart" method="post">
{% comment %}
Successful message
{% endcomment %}
{% if form.posted_successfully? %}
<p class="note form-success">
{{ 'contact.form.post_success' | t }}
</p>
{% endif %}
<div class="selection-wrapper">
{{ form.errors | default_errors }}
</div>
<div class="selection-wrapper">
<div class="grid">

<label for="Name" class="hidden-label">Name*</label>
<div class="grid__item medium-up--one-half">
<input type="text" style="width:100%;" name="first_name" id="FirstName" {% if form.first_name %}value="{{ form.first_name }}"{% endif %} required>
<label for="FirstName">{{ 'customer.register.first_name' | t }}</label>
</div>
<div class="grid__item medium-up--one-half">  
<input type="text"  style="width:100%;" name="last_name" id="LastName" {% if form.last_name %}value="{{ form.last_name }}"{% endif %}>
<label for="LastName">{{ 'customer.register.last_name' | t }}</label>
</div>
</div>
<br/>

<div class="grid">
<label for="Email" class="hidden-label">Email Address *</label>
<input type="email" style="width:100%;"  id="Email"  name="email" autocapitalize="off" value="{% if form.email %}{{ form.email }}{% elsif customer %}{{ customer.email }}{% endif %}">
</div>
<br/>
<div class="grid">
<label for="ContactFormPhone" class="hidden-label">Key Code Card Number*</label>
<input type="text" id="key_cord_card_no"  style="width:100%;" name="keyCordCardNo"  value="{{ form.keyCordCardNo }}" required>
</div>
<br/>
<div class="grid">
<label for="Product Name" class="hidden-label">Product Name*</label>
<input type="text" id="productName"  style="width:100%;" name="product_name"  value="{{ form.product_name }}" required>
the  </div>
<div class="grid">
<p class="submit">
<input type="submit" class="button solid" value="submit">
</p>
</div>
</div>
</form>

我必须按名称将产品添加到购物车,该怎么做? 如何在购物车中添加产品 Shopify 通过仅采用产品名称字段的表单?

简短的回答是 -你不能只用他的名字添加产品。

产品必须具有多属性 ID 才能将其提交到购物车。

长答案是:

按名称添加产品的唯一方法是向产品页面发出 AJAX 请求并从那里获取变体 ID 并提交。

总是在 Shopify 中购买变体,即使产品没有任何变体,它也总是有一个购买的变体。

因此,如果您计划允许客户在文本字段中输入产品名称,则需要向搜索页面发出 AJAX 请求(因为我假设客户将无法输入产品的确切名称以便您直接发出 AJAX 请求(并获得第一个结果(最好在结果项中添加变体 ID 作为例如,数据属性或其他方式,这样您就不需要对产品页面本身进行第二次 AJAX 调用并将其提交到购物车。

由于提供的信息太少,我无法为您提供任何代码或如何继续的进一步说明。

最新更新