<p>The following code can be used in the init method of a <a href="https://docs.craftcms.com/v3/extend/module-guide.html" rel="nofollow noreferrer">custom module</a>.</p>

<pre><code>Event::on(
    View::class,
    View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
    function($event) {
        // Check query string for coupon code
        $couponCode = Craft::$app-&gt;getRequest()-&gt;getQueryParam('coupon', null);

        if ($couponCode) {
            // Check there is a discount that has this coupon code
            $discount = Commerce::getInstance()-&gt;getDiscounts()-&gt;getDiscountByCode($couponCode);
            if ($discount) {
                $cart = Commerce::getInstance()-&gt;getCarts()-&gt;getCart();

                $cart-&gt;couponCode = $couponCode;
                Craft::$app-&gt;getElements()-&gt;saveElement($cart, false);
            }
        }

    }
);
</code></pre>

<p>Don't forget the following use statements at the top of the file.</p>

<pre><code>use Craft;
use craft\commerce\Plugin as Commerce;
use craft\web\View;
use yii\base\Event;
</code></pre>

CC BY-SA 4.0
2019-12-10T02:17:32.723Z
Thank you so much, @nfourtythree! I'll try this out this week and let you know if I have any issues.
kamicrafted
CC BY-SA 4.0
2019-12-03T01:50:08.493Z
2019-12-03T01:50:08.493Z
nfourtythree
<p>I was wondering if it was possible to allow a customer to apply a coupon code using a URL or URL params in Commerce 2. I haven’t been able to find any questions on stack exchange or on the web, but the scenario I’m trying to support is:</p>

<ul>
<li><p>Customer receives an email with a coupon code (includes a coupon code image that links to the storefront)</p></li>
<li><p>Customer clicks a link which takes them to the store and automatically applies the coupon with a toast confirmation (e.g. store.com?coupon=somecouponcode)</p></li>
<li><p>Customer adds purchasables to cart and when they go to the cart page, the coupon is already applied to cart lineitems without requiring them to manually input it themselves</p></li>
</ul>

<plugin-craftcommerce><discounts>
Applying coupon codes via URL
# Applying coupon codes via URL

(Asked by: username_0 on 2019-11-27)

I was wondering if it was possible to allow a customer to apply a coupon code using a URL or URL params in Commerce 2. I haven’t been able to find any questions on stack exchange or on the web, but the scenario I’m trying to support is:

* Customer receives an email with a coupon code (includes a coupon code image that links to the storefront)
* Customer clicks a link which takes them to the store and automatically applies the coupon with a toast confirmation (e.g. store.com?coupon=somecouponcode)
* Customer adds purchasables to cart and when they go to the cart page, the coupon is already applied to cart lineitems without requiring them to manually input it themselves

**Tags:** plugin-craftcommerce, discounts


---

## 1 answers

### Answer by username_1 on 2019-12-03. Score: 2

The following code can be used in the init method of a custom module.
Event::on(
    View::class,
    View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
    function($event) {
        // Check query string for coupon code
        $couponCode = Craft::$app->getRequest()->getQueryParam('coupon', null);

        if ($couponCode) {
            // Check there is a discount that has this coupon code
            $discount = Commerce::getInstance()->getDiscounts()->getDiscountByCode($couponCode);
            if ($discount) {
                $cart = Commerce::getInstance()->getCarts()->getCart();

                $cart->couponCode = $couponCode;
                Craft::$app->getElements()->saveElement($cart, false);
            }
        }

    }
);

Don't forget the following use statements at the top of the file.
use Craft;
use craft\commerce\Plugin as Commerce;
use craft\web\View;
use yii\base\Event;

> **Comments:**
> **username_0**: Thank you so much, @username_1! I'll try this out this week and let you know if I have any issues.  

---

