Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-10T01:27:12.623Z
Answers.Text: 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?
Answers.UserDisplayName: Parry Huang
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-13T12:27:31.990Z
Answers.LastActivityDate: 2018-11-13T12:27:31.990Z
Answers.UserDisplayName: Brandon Kelly
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-13T11:29:18.923Z
Comments.Text: ^ I guess the same could apply to commerce subscriptions as well. By the way I'm using Craft 3 and Commerce 2.
Comments.UserDisplayName: KevAdamson
Tags: <plugin-craftcommerce><plugins><fieldtypes><relatedto><promotions>
Title: Commerce discount promotions as a fieldtype?
ThreadText: # 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](https://docs.craftcms.com/v3/extend/field-types.html#registering-custom-field-types).
  
  Here’s the relevant code you’d need if you want to take a stab at it:
  
  ```php
  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](https://docs.craftcms.com/commerce/api/v2/craft-commerce-models-discount.html) or `null`, so in your templates you could do this:
  
  ```twig
  {% if entry.myDiscountField %}
      Code: <code>{{ entry.myDiscountField.code }}</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.
  
  ```twig
  {% if entry.myDiscountCodeField %}
      {% set discount = craft.commerce.discounts.getDiscountByCode(entry.myDiscountCodeField) %}
  {% else %}
      {% set discount = null %}
  {% endif %}
  
  {% if discount %}
      Code: <code>{{ discount.code }}</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?  
  
  ---
  
  