简体   繁体   English

Shopify:单击按钮时如何将产品名称添加到输入字段表单?

[英]Shopify: how do I get the Product Name to the Input Field form when the button is clicked?

I have this function works well with me to get the URL of page using Input Field:我有这个 function 可以很好地与我一起使用输入字段来获取页面的 URL:

var url      = window.location.href;
$( document ).ready(function() {
$('#url').val(url);
});

I want to make it works to get Product Name, please can anyone show me how I can do that?我想让它获得产品名称,请谁能告诉我我该怎么做? note that I want to use it with Shopify Store.请注意,我想将它与 Shopify Store 一起使用。

Here's the input field code:这是输入字段代码:

<input type="text" name="entry.123456789" id="url" value="" style="display: none;">

Thanks y'all guys in advance for your help!提前感谢大家的帮助!

Shopify has a global product object available on Product pages. Shopify 在产品页面上有一个全球产品 object。 If you need this form on some product page, then using the code below should work fine.如果您在某些产品页面上需要此表单,那么使用下面的代码应该可以正常工作。

<input type="text" name="product-title" value="{{product.title}}" style="display: none;">

If you need it on some other page, get a single product object (eg using all_products) and then use the title property.如果您在其他页面上需要它,请获取单个产品 object(例如使用 all_products),然后使用 title 属性。

Moreover, from the display none, I have understood that you just need to get this data when form is submitted and does not need input from user.此外,从显示无,我了解到您只需要在提交表单时获取此数据,不需要用户输入。 In that case, hidden field is recommended.在这种情况下,建议使用隐藏字段。

<input type="hidden" name="product-title" value="{{product.title}}">

As per addition in comments, if you are trying to get this on Cart page, the available object is named as cart .根据评论中的补充,如果您尝试在购物车页面上获取此内容,可用的 object 被命名为cart You can loop over the products in cart and then get title.您可以遍历购物车中的产品,然后获取标题。

{% for line_item in cart.items %}
    <input type="hidden" name="product-title-{{line_item.product_id}}" value="{{line_item.product.title}}">

    {{line_item.product.title}} - {{line_item.quantity}}

{% endfor %}

This will generate the hidden input fields.这将生成隐藏的输入字段。 Change the input name accordingly.相应地更改输入名称。

Input Type Hidden 输入类型隐藏

Shopify Product Object Shopify 产品 Object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM