Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-10T02:17:32.723Z
Answers.Text: Thank you so much, @nfourtythree! I'll try this out this week and let you know if I have any issues.
Answers.UserDisplayName: kamicrafted
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-03T01:50:08.493Z
Answers.LastActivityDate: 2019-12-03T01:50:08.493Z
Answers.UserDisplayName: nfourtythree
Body: <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>
  
Tags: <plugin-craftcommerce><discounts>
Title: Applying coupon codes via URL
ThreadText: # 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](https://docs.craftcms.com/v3/extend/module-guide.html).
  
  ```
  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.  
  
  ---
  
  