Shopify:更改变体的可用性

时间:2017-05-29 10:21:53

标签: jquery shopify shop

在我的案例中,Shopify产品有5种变体:Monday, Tuesday, Wednesday, Thursday, Friday。 我想在48小时内限制变种。例如:

星期一 - Wednesday, Thursday, Friday

星期三 - Monday, Tuesday, Friday

星期五 - Monday, Tuesday, Wednesday, Thursday

我想知道我是否可以使用标准功能。通过Shopify的管理面板或内部结构。

2 个答案:

答案 0 :(得分:1)

您可以过滤掉您不希望使用Liquid显示的变体,以防止购物者将这些产品添加到购物车中。

在您的商品模板中,找到第{% for variant in product.variants %}

在此行之前,添加{% assign day_of_week = 'now' | date: '%A' %}以将当前星期几存储在Liquid变量中

在此行之后,添加代码以过滤掉您不想看到的变体(如下所示)。对于您关心的一周中的每一天,您将需要一个'when'语句,然后检查变体的某些属性(我使用变体标题)。如果它是您不想显示的变体,请使用'continue'语句跳过该变体并继续下一个变种。

示例:

{% assign day_of_week = 'now' | date: "%A" %}

{% for variant in product.variants %}
<!-- Addition below to filter out products that customer shouldn't see -->
  {% case day_of_week %}
    {% when 'Monday' %}
      {% if variant.title contains 'Monday' or variant.title contains 'Tuesday' %}{% continue %}{% endif %}
    {% when 'Tuesday' %}
      {% if variant.title contains 'Tuesday' or variant.title contains 'Wednesday' %}{% continue %}{% endif %}

<!-- Repeat for each day of the week -->
   {% endcase %}
  <!-- Regular code from your variant loop -->

答案 1 :(得分:0)

除了我的其他答案,您还可以使用Javascript过滤掉变体。我通常更喜欢通过Liquid预过滤变体 - 依赖于Javascript可能会导致页面加载时出现闪烁,因为变量在代码运行之前会短暂出现。

假设jQuery已经在页面上,我们可以将其写为:

<script>
  jQuery(document).ready(function(){
    var variants_list = {{ product.variants | json }};
    var date = new Date(Date.now()).toDateString();

    for(var i=0; i < variants_list.length; i++){
      var variant = variants_list[i];
      if(variant.title == 'Monday' && (date.indexOf('Mon') > -1 || date.indexOf('Tue') > -1){
        jQuery('[value="' + variant.id + '"]').remove();
      //Note: If you theme uses swatches or anything like that, you may need to remove additional elements from the document as well.
      }
      if(variant.title == 'Tuesday' && (date.indexOf('Tue') > -1 || date.indexOf('Wed') > -1){
        jQuery('[value="' + variant.id + '"]').remove();
      }
    }
    //Lather-rinse-repeat for each day of the week that you care about

}
</script>