造型在Shopify中售罄的选项
我正在尝试将CSS添加到产品选项中,以向用户提供视觉指示哪些选项不可用(已售罄)。当产品只有一组选项值时,这起作用,但是我的产品具有两个(“日期”和“时间”)。我在大多数方面都可以使用此操作,但是当我在日期之间切换时,可用的“时间”选项不会更新。这里的任何帮助将不胜感激。
在我的产品形式中,我有以下代码来添加一类以售罄的选项。也许我需要一些JS?
如果有帮助,我正在使用声望主题,并且页面在这里: https://neetfield.co。英国/比萨饼
{%- if option.name == "Time" and product.selected_or_first_available_variant.option1 == product.variants[forloop.index0].option1 -%}
{%- if product.variants[forloop.index0].available -%}
{%- assign opt_sold = false -%}
{%- else -%}
{%- assign opt_sold = true -%}
{%- endif -%}
{%- else -%}
{%- assign opt_sold = false -%}
{%- endif -%}
然后,这用于添加一类以售罄选项{%如果Opt_sold%} Opt-Sold-Sold-out {%endif%}
这是我位置的屏幕截图到达(现在在实时网站上禁用,直到在日期之间切换时,我一直在工作):
I'm trying to add css to product options to provide the user with a visual indication of which options are not available (sold out). This works when a product only has one set of option values, however my product has two ("date", and "time"). I have this working most of the way, however the available "Time" options are not updated when I switch between dates. Any help here would be hugely appreciated.
Inside my product-form.liquid file I have the following code to add a class to sold out options. Perhaps I need some js?
In case it's helpful, I'm using the Prestige theme and the page is here: https://neetfield.co.uk/pizza
{%- if option.name == "Time" and product.selected_or_first_available_variant.option1 == product.variants[forloop.index0].option1 -%}
{%- if product.variants[forloop.index0].available -%}
{%- assign opt_sold = false -%}
{%- else -%}
{%- assign opt_sold = true -%}
{%- endif -%}
{%- else -%}
{%- assign opt_sold = false -%}
{%- endif -%}
And then this is used to add a class to sold out options {% if opt_sold %}opt-sold-out{% endif %}
This is a screenshot of where I got to (now disabled on the live site until I haved it working when switching between dates):
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首页渲染(液体)
的初始页面加载您可以使用类似的内容:
此片段在所有产品变体上迭代,其中
option1
等于option1 当前选择变体的值(例如,“ 6月4日,星期六”)。对于每个变体,我们将检查它是否可用,并将其推到
opary_timeslots
array。确保在所有产品选项上迭代之前,将此片段放置。
最后,使用此数组检查选项值是否可用:
value
基本上是option.values
的值。我敢肯定,您会在产品形式中找到它的某个位置:)用户交互后(JavaScript),
由于Liquid是一种服务器端渲染模板语言,您只能通过更新通过JavaScript更新变体更改的选项输入元素来实现此目的在客户端。 Shopify Prestige主题具有有用的 customevent 对于这种风景:
变体:更改
实现的示例将如下所示:
请记住,您并没有真正禁用输入。它只是在视觉上表明它是禁用的。如果这对您来说不是问题,请忽略此评论。
请注意,这无法处理以下方案:
我猜想选择日期的决定始终首先,这不应该是要担心的重要情况。
First page render (Liquid)
For the initial page load you can use something like this:
This snippet iterates over all product variants where
option1
equals theoption1
value of the currently selected variant (e.g. "Saturday 4th June"). For each variant we'll check if its available and push it to theavailable_timeslots
array.Make sure to place this snippet before iterating over all product options.
Finally, use this array to check if an option value is available:
value
basically is a value ofoption.values
. I'm sure you'll find this somewhere in your product-form :)After user interaction (JavaScript)
Since Liquid is a server-side rendered template language, you can only achieve this by updating the option input elements on variant change via JavaScript on the client-side. The Shopify Prestige theme has a useful CustomEvent for this scenario:
variant:changed
An example of the implementation would look like this:
Keep in mind that you are not really disabling the input. It's just visually indicating that it's disabled. If that's not a problem for you, just ignore this comment.
Note that this does not handle the following scenario:
I'm guessing that the decision which date to chose always comes first so this should not be an important scenario to worry about.
具有单一选项的产品的方法
非常容易,并且不需要任何JavaScript,因为每个选项值映射恰好是一个变体。去这里的方法是获取该变体并检查是否可用:
Approach for products with a single option
This case is pretty easy and does not require any JavaScript since each option value maps to exactly one variant. The way to go here would be to get that variant and check if it's available: