起始页上的捐赠计数器

时间:2019-06-20 15:54:26

标签: shopify shopify-app shopify-template

我需要一个捐赠计数器(总计),该计数器显示在起始页和特定页面上。我的问题是,我包括一个摘录来获得总捐款,而这些捐款来自一种特定的产品。我设法获得了捐款总额,可以将其显示在产品页面上。但我想在首页和特定页面上显示它。我写了一个 product-sold-count.liquid ,如下:

{% assign productStartCount = product.metafields.stock.initial | times:1 %}
{% if productStartCount > 0 %}
{% assign productInventory = product.variants.first.inventory_quantity %}
{% assign totalSum = productStartCount | minus:productInventory | times: product.price | times: product.metafields.donation.percent | divided_by: 10000 %}
<p>{{ totalSum }} € were donated until now!</p>
{% endif %}

现在我想将此totalSum转到另一个页面,或者更可能转到起始页面和其他页面?我尝试过:{% include 'product-sold-count' %}使用诸如 page.liquid theme.liquid 之类的特定液体,但这不起作用。我该如何实现?

1 个答案:

答案 0 :(得分:0)

问题出在产品页面上,您有变量product。但是在其他页面上,此变量不再有效。

为了获得所有产品的总数,您可以为每个产品重复以上代码并获得所有产品的总和。请注意,Shopify提供了all_products变量,但每页最多只能有20种产品。您可以使用collection.products(如果它在集合中)(或将所有捐赠产品分配给捐赠集合,然后获取该集合的所有产品)

{% assign allProductCount = 0 %}

{% for product in  all_products %}
  {% assign productStartCount = product.metafields.stock.initial | times:1 %}
  {% if productStartCount > 0 %}
    {% assign productInventory = product.variants.first.inventory_quantity %}
    {% assign totalSum = productStartCount | minus:productInventory | times: 
  product.price | times: product.metafields.donation.percent | divided_by: 10000 %}

    {% assign allProductCount = allProductCount | plus: totalSum %}
  {% endif %}

{% endfor %}

<p>{{ allProductCount }} € were donated until now!</p>