【问题标题】:Shopify: How to check if there is > 1 collection in a cartShopify:如何检查购物车中是否有 > 1 个集合
【发布时间】:2021-07-07 15:35:05
【问题描述】:

我正试图阻止包含多个集合的订单被签出。如果有多个集合,我只想禁用结帐按钮。我知道语法不好,但我想做这样的事情。

{% for item in cart.collections %}
 {% if item.collection.length > 1 %}
    <button disabled>Cant checkout</button>
  {% else %}
    <button disabled>Can checkout</button>
  {% endif %}

这是按钮的当前代码

<div class="cart__checkout-wrapper">
        <button disabled id="button" type="submit" name="checkout" data-terms-required="{{ settings.cart_terms_conditions_enable }}" class="btn cart__checkout">
          {{ 'cart.general.checkout' | t }}
        </button>

        {% if additional_checkout_buttons and settings.cart_additional_buttons %}
          <div class="additional-checkout-buttons additional-checkout-buttons--vertical">{{ content_for_additional_checkout_buttons }}</div>
        {% endif %}
      </div>
{% endfor %}

【问题讨论】:

    标签: shopify shopify-app


    【解决方案1】:

    方法一

    这个简单的检查可能满足您的需要:

    {% liquid 
      assign all_collections_in_cart = cart.items | map: 'product' | map: 'collections' | map: 'title' | uniq
      assign collection_count = all_collections_in_cart.size
    
      if collection_count > 1
        assign can_checkout = false
      else
        assign can_checkout = true
      endif
    %}
    

    它的作用

    首先,我们使用一系列map 过滤器来“向下钻取”对象结构。购物车中的项目本身不属于任何特定的集合,但每个项目都与 variantproduct 相关联。从product,我们可以得到它所属的所有collections,这将是一个集合对象数组。我们不能对对象本身做太多事情,所以我们进一步深入一层以获得可以比较的东西,例如titlehandleid。此时,我们现在有了一个文本条目数组(或数字,如果你换成 ID),所以我们可以应用 uniq 过滤器来删除任何重复项。

    现在我们有了一个没有重复的数组,我们可以检查数组的size 看它是否大于一并适当地设置一个变量。

    请注意,如果单个产品属于多个集合,这将始终返回 false!


    方法二:

    如果您需要测试所有产品是否共享一个集合,因为产品一次可以属于多个产品,我们将无法采用尽可能多的捷径。相反,我们的代码可能看起来像这样:

    {% liquid
      assign can_checkout = true
    
      if cart.items.size > 1
        assign shared_collections = cart.items.first.product.collections | map: 'title'
        for item in cart.items
          if forloop.first
            continue
          endif
          assign item_collections = item.product.collections | map: title
    
          assign placeholder_array = ''
          for coll in shared_collections
            if item_collections contains coll
              if placeholder_array.size > 0
                assign placeholder_array = placeholder_array | append: ','
              endif
              assign placeholder_array = placeholder_array | append: coll
            endif
          endfor
          assign shared_collections = placeholder_array | split: ','
          if shared_collections.size == 0
            assign can_checkout = false
            break
          endif
        endfor
      endif
    %}
    

    它的作用

    如果购物车中有 2 个或更多商品,则会触发第二个版本。如果有,我们从购物车中的第一个项目中获取所有集合标题,然后遍历购物车中的所有剩余项目。在每一步,我们都会查看共享集合列表,看看有多少与购物车中的当前项目共享,以创建一个新数组。 (Liquid 不允许我们直接创建一个数组,所以我们必须构建一个分隔字符串,然后使用split 过滤器)

    如果我们有一个包含 0 个共享条目的数组,我们将 can_checkout 变量设置为 false 并使用 break 命令停止搜索。


    附录

    对于上述两种方法,如果您的某些集合不计入您的集合规则,事情可能会变得有点棘手。例如,如果您有“精选”或“夏季特卖”系列,您将需要确保您没有在系列比较中计算那些(例如,通过有一个异常数组并从数组中删除这些项目项目所属的集合)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 2022-10-19
      • 2011-04-09
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多