<p>That field type doesn’t currently exist, but it wouldn’t be hard to <a href="https://docs.craftcms.com/v3/extend/field-types.html#registering-custom-field-types" rel="nofollow noreferrer">create it</a>.</p>

<p>Here’s the relevant code you’d need if you want to take a stab at it:</p>

<pre class="lang-php prettyprint-override"><code>use Craft;
use craft\base\ElementInterface;
use craft\commerce\models\Discount;
use craft\commerce\Plugin as Commerce;

// ...

public function normalizeValue($value, ElementInterface $element = null)
{
    if ($value !== null) {
        return Commerce::getInstance()-&gt;discounts-&gt;getDiscountById($value);
    }

    return $value;
}

public function getInputHtml($value, ElementInterface $element = null): string
{
    $options = [];
    foreach (Commerce::getInstance()-&gt;discounts-&gt;getAllDiscounts() as $discount) {
        $options[] = ['value' =&gt; $discount-&gt;id, 'label' =&gt; $discount-&gt;name];
    }

    /** @var Discount|null $value */
    return Craft::$app-&gt;view-&gt;renderTemplate('_includes/forms/select', [
        'id' =&gt; $this-&gt;handle,
        'name' =&gt; $this-&gt;handle,
        'options' =&gt; $options,
        'value' =&gt; $value-&gt;id ?? null,
    ]);
}
</code></pre>

<p>With that, the field type’s value would always either be a <a href="https://docs.craftcms.com/commerce/api/v2/craft-commerce-models-discount.html" rel="nofollow noreferrer">Discount model</a> or <code>null</code>, so in your templates you could do this:</p>

<pre class="lang-twig prettyprint-override"><code>{% if entry.myDiscountField %}
    Code: &lt;code&gt;{{ entry.myDiscountField.code }}&lt;/code&gt;
    ...
{% endif %}
</code></pre>

<p>Alternatively, you could pull this off with a simple Plain Text field that stores a discount code, and then fetch the discount manually from your template.</p>

<pre class="lang-twig prettyprint-override"><code>{% if entry.myDiscountCodeField %}
    {% set discount = craft.commerce.discounts.getDiscountByCode(entry.myDiscountCodeField) %}
{% else %}
    {% set discount = null %}
{% endif %}

{% if discount %}
    Code: &lt;code&gt;{{ discount.code }}&lt;/code&gt;
    ...
{% endif %}
</code></pre>

CC BY-SA 4.0
2019-07-10T01:27:12.623Z
I am having a problem after create the discount field. When I try to save the entry with discount field, I got the error "Discount Field should contain at most 255 characters." I checked the code in the field, and commented out "normalizeValue" function. Or instead of commenting ```public function normalizeValue($value, ElementInterface $element = null)
{
    if ($value !== null) {
        return Commerce::getInstance()->discounts->getDiscountById($value)->id;
    }

    return $value;
}``` might be right?
Parry Huang
CC BY-SA 4.0
2018-11-13T12:27:31.990Z
2018-11-13T12:27:31.990Z
Brandon Kelly
<p>I'd like to create an entry in Craft that talks about a new discount, and then choose that discount via a field (so a bit like you can choose an entry or a product). That way, should the parameters of the discount change, I'm not having to manually update the information in the entry to match any changes to the discount details. Is there a plugin for this?</p>

CC BY-SA 4.0
2018-11-13T11:29:18.923Z
^ I guess the same could apply to commerce subscriptions as well. By the way I'm using Craft 3 and Commerce 2.
KevAdamson
<plugin-craftcommerce><plugins><fieldtypes><relatedto><promotions>
Commerce discount promotions as a fieldtype?
# Commerce discount promotions as a fieldtype?

(Asked by: username_0 on 2018-11-13)

I'd like to create an entry in Craft that talks about a new discount, and then choose that discount via a field (so a bit like you can choose an entry or a product). That way, should the parameters of the discount change, I'm not having to manually update the information in the entry to match any changes to the discount details. Is there a plugin for this?

**Tags:** plugin-craftcommerce, plugins, fieldtypes, relatedto, promotions

> **Comments:**
> **username_0**: ^ I guess the same could apply to commerce subscriptions as well. By the way I'm using Craft 3 and Commerce 2.  

---

## 1 answers

### Answer by username_1 on 2018-11-13. Score: 3

That field type doesn’t currently exist, but it wouldn’t be hard to create it.
Here’s the relevant code you’d need if you want to take a stab at it:
use Craft;
use craft\base\ElementInterface;
use craft\commerce\models\Discount;
use craft\commerce\Plugin as Commerce;

// ...

public function normalizeValue($value, ElementInterface $element = null)
{
    if ($value !== null) {
        return Commerce::getInstance()->discounts->getDiscountById($value);
    }

    return $value;
}

public function getInputHtml($value, ElementInterface $element = null): string
{
    $options = [];
    foreach (Commerce::getInstance()->discounts->getAllDiscounts() as $discount) {
        $options[] = ['value' => $discount->id, 'label' => $discount->name];
    }

    /** @var Discount|null $value */
    return Craft::$app->view->renderTemplate('_includes/forms/select', [
        'id' => $this->handle,
        'name' => $this->handle,
        'options' => $options,
        'value' => $value->id ?? null,
    ]);
}

With that, the field type’s value would always either be a Discount model or null, so in your templates you could do this:
{% if entry.myDiscountField %}
    Code: {{ entry.myDiscountField.code }}
    ...
{% endif %}

Alternatively, you could pull this off with a simple Plain Text field that stores a discount code, and then fetch the discount manually from your template.
{% if entry.myDiscountCodeField %}
    {% set discount = craft.commerce.discounts.getDiscountByCode(entry.myDiscountCodeField) %}
{% else %}
    {% set discount = null %}
{% endif %}

{% if discount %}
    Code: {{ discount.code }}
    ...
{% endif %}

> **Comments:**
> **username_2**: I am having a problem after create the discount field. When I try to save the entry with discount field, I got the error "Discount Field should contain at most 255 characters." I checked the code in the field, and commented out "normalizeValue" function. Or instead of commenting ```public function normalizeValue($value, ElementInterface $element = null) {     if ($value !== null) {         return Commerce::getInstance()->discounts->getDiscountById($value)->id;     }      return $value; }``` might be right?  

---

