<p>In Commerce 1, <code>craft.commerce.discounts</code> will return an array of Commerce_DiscountModel objects of all available discounts.</p>

<pre><code>public function getAllDiscounts($criteria = [])
{
    $records = Commerce_DiscountRecord::model()-&gt;findAll($criteria);

    return Commerce_DiscountModel::populateModels($records);
}
</code></pre>

<p><code>Commerce_DiscountModel</code> objects have a <code>dateFrom</code> and a <code>dateTo</code> property, which is probably what you're looking for.</p>

<p>Completely untested, but something like this:</p>

<pre><code>{% set discount = null %}
{% for d in craft.commerce.discounts %}
    {% if d.id == 1 %}
        {% set discount = d %}
        {% if discount.dateTo &gt; '2018-01-01 00:00:00' and discount.dateFrom &lt; '2018-12-31 23:59:59' %}
            Matches!
        {% endif %}
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2018-08-31T22:33:54.217Z
2018-08-31T22:33:54.217Z
Brad Bell
<p>In Twig, I want to find out if a discount is valid based on start/end date.  startDate and endDate don't seem to exist and I can't inspect the discount object by using the json_encode filter (returns {}).</p>

<p>How can we do this?</p>

<p>This is the code used to grab the discount object:</p>

<pre><code>{% set discount = null %}
{% for d in craft.commerce.discounts %}
    {% if d.id == 1 %}{% set discount = d %}{% endif %}
{% endfor %}
</code></pre>

<p>name, description and enabled variables are available but no startDate/endDate.</p>

CC BY-SA 4.0
2018-08-30T00:16:46.020Z
Can you share the code you're using to retrieve the discount model?
Brad Bell
CC BY-SA 4.0
2018-08-30T05:21:06.570Z
Sure it's in the description now.
biko
CC BY-SA 4.0
2018-08-31T04:47:54.060Z
@BradBell I forgot to mention you in my last comment but the code is now in the description.  Thanks in advance.
biko
<templating><plugin-craftcommerce><discounts>
Commerce 1 find if Discount object is valid based on start/end dates (Twig)
# Commerce 1 find if Discount object is valid based on start/end dates (Twig)

(Asked by: username_0 on 2018-08-29)

In Twig, I want to find out if a discount is valid based on start/end date.  startDate and endDate don't seem to exist and I can't inspect the discount object by using the json_encode filter (returns {}).
How can we do this?
This is the code used to grab the discount object:
{% set discount = null %}
{% for d in craft.commerce.discounts %}
    {% if d.id == 1 %}{% set discount = d %}{% endif %}
{% endfor %}

name, description and enabled variables are available but no startDate/endDate.

**Tags:** templating, plugin-craftcommerce, discounts

> **Comments:**
> **username_1**: Can you share the code you're using to retrieve the discount model?  
> **username_0**: Sure it's in the description now.  
> **username_0**: @BradBell I forgot to mention you in my last comment but the code is now in the description.  Thanks in advance.  

---

## 1 answers

### Answer by username_1 on 2018-08-31. Score: 1 | Accepted answer

In Commerce 1, craft.commerce.discounts will return an array of Commerce_DiscountModel objects of all available discounts.
public function getAllDiscounts($criteria = [])
{
    $records = Commerce_DiscountRecord::model()->findAll($criteria);

    return Commerce_DiscountModel::populateModels($records);
}

Commerce_DiscountModel objects have a dateFrom and a dateTo property, which is probably what you're looking for.
Completely untested, but something like this:
{% set discount = null %}
{% for d in craft.commerce.discounts %}
    {% if d.id == 1 %}
        {% set discount = d %}
        {% if discount.dateTo > '2018-01-01 00:00:00' and discount.dateFrom < '2018-12-31 23:59:59' %}
            Matches!
        {% endif %}
    {% endif %}
{% endfor %}


---

