<p>You now need to set the groups, product types, and purchasables on the discount model directly, then just pass the discount model to the saveDiscount method:</p>

<p><code>Plugin::getInstance()-&gt;getDiscounts()-&gt;saveDiscount($discount)</code></p>

<p>An example of setting these things on the discount before saving can be found in the discount controller:</p>

<p><a href="https://github.com/craftcms/commerce/blob/develop/src/controllers/DiscountsController.php#L128-L166" rel="nofollow noreferrer">https://github.com/craftcms/commerce/blob/develop/src/controllers/DiscountsController.php#L128-L166</a></p>

CC BY-SA 4.0
2019-01-09T11:17:57.167Z
Ok , so this would be $discount->setCategoryIds($productTypes); in my case. Thanks a lot!
phaetons
CC BY-SA 4.0
2019-01-09T11:07:49.940Z
2019-01-09T11:07:49.940Z
Luke Holder
<p>We sell vouchers to our shop users. To prevent them for being used on vouchers themself, i could relate them to specific productTypes or even products in Commerce 1.</p>

<pre><code>craft()-&gt;commerce_discounts-&gt;saveDiscount($discount, $groups, $productTypes, $products)
</code></pre>

<p>In Commerce 2 the 3rd and 4th parametes are gone. How can i relate vouchers to product types programmaticely? </p>

<plugin-development><plugin-craftcommerce2>
What happened to commerce_discounts->saveDiscounts or How to relate vouchers to product types in Commerce 2?
# What happened to commerce_discounts->saveDiscounts or How to relate vouchers to product types in Commerce 2?

(Asked by: username_0 on 2019-01-09)

We sell vouchers to our shop users. To prevent them for being used on vouchers themself, i could relate them to specific productTypes or even products in Commerce 1.
craft()->commerce_discounts->saveDiscount($discount, $groups, $productTypes, $products)

In Commerce 2 the 3rd and 4th parametes are gone. How can i relate vouchers to product types programmaticely?

**Tags:** plugin-development, plugin-craftcommerce2

---

## 1 answers

### Answer by username_1 on 2019-01-09. Score: 1 | Accepted answer

You now need to set the groups, product types, and purchasables on the discount model directly, then just pass the discount model to the saveDiscount method:

`Plugin::getInstance()->getDiscounts()->saveDiscount($discount)`

An example of setting these things on the discount before saving can be found in the discount controller:

<https://github.com/craftcms/commerce/blob/develop/src/controllers/DiscountsController.php#L128-L166>

> **Comments:**
> **username_0**: Ok , so this would be $discount->setCategoryIds($productTypes); in my case. Thanks a lot!  

---

