Answers.Body: <p>Here's how you could accomplish this.</p>
  
  <p>1) Create a product variant called 'custom amount' with a zero price.</p>
  
  <p>2) Get the ID of that variant</p>
  
  <p>3) When submitting/adding/updating the item to the cart make sure to post an 'amount' line item option. For docs on that see: <a href="https://docs.craftcms.com/commerce/v2/adding-to-and-updating-the-cart.html#line-item-options-and-notes" rel="nofollow noreferrer">https://docs.craftcms.com/commerce/v2/adding-to-and-updating-the-cart.html#line-item-options-and-notes</a></p>
  
  <p>3) Create a plugin/module that attaches this listener:</p>
  
  <pre><code>Event::on(LineItems::class, LineItems::EVENT_POPULATE_LINE_ITEM, function(LineItemEvent $event) {
      $lineItem = $event-&gt;lineItem;
      $purchasable = $lineItem-&gt;getPurchasable();
      if($purchasable &amp;&amp; $purchasable-&gt;id = ID_OF_VARIANT_HERE)
      {
          $amount = $lineItem-&gt;options['amount'] ?? 0;
          if($amount)
          {
              $lineItem-&gt;price = $amount;
              $lineItem-&gt;salePrice = $amount;
          }
      }
  });
  </code></pre>
  
  <p>This will set the price to the price of the line item option 'amount' that is submitted.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-28T15:35:15.753Z
Answers.Text: Wicked, thanks! One note for anyone else coming across this, you'll need to `use craft\commerce\services\LineItems;` and
   `use craft\commerce\events\LineItemEvent;` in your plugin file as well.
Answers.UserDisplayName: Derek Hogue
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-03T14:29:29.513Z
Answers.Text: Is setting a line item's price in an event no longer supported under Commerce 3? This broke for me with the Commerce 3 update.
Answers.UserDisplayName: Derek Hogue
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-28T02:27:01.020Z
Answers.LastActivityDate: 2020-06-09T16:04:36.263Z
Answers.LastEditDate: 2020-06-09T16:04:36.263Z
Answers.UserDisplayName: Luke Holder
Body: <p>Is there a way in Craft Commerce 2 to have a user-input price as a product variant? In my case, I'm offering several pre-set amounts for a donation product (each is a variant), but I also want to offer a "custom amount" option.</p>
  
  <p>Is there a native way to do this? Or a way I can accomplish this with a plugin action?</p>
  
Tags: <plugin-craftcommerce2>
Title: User-determined price for a product variant?
ThreadText: # User-determined price for a product variant?
  
  (Asked by: username_0 on 2018-08-27)
  
  Is there a way in Craft Commerce 2 to have a user-input price as a product variant? In my case, I'm offering several pre-set amounts for a donation product (each is a variant), but I also want to offer a "custom amount" option.
  
  Is there a native way to do this? Or a way I can accomplish this with a plugin action?
  
  **Tags:** plugin-craftcommerce2
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-08-28. Score: 5 | Accepted answer
  
  Here's how you could accomplish this.
  
  1) Create a product variant called 'custom amount' with a zero price.
  
  2) Get the ID of that variant
  
  3) When submitting/adding/updating the item to the cart make sure to post an 'amount' line item option. For docs on that see: <https://docs.craftcms.com/commerce/v2/adding-to-and-updating-the-cart.html#line-item-options-and-notes>
  
  3) Create a plugin/module that attaches this listener:
  
  ```
  Event::on(LineItems::class, LineItems::EVENT_POPULATE_LINE_ITEM, function(LineItemEvent $event) {
      $lineItem = $event->lineItem;
      $purchasable = $lineItem->getPurchasable();
      if($purchasable && $purchasable->id = ID_OF_VARIANT_HERE)
      {
          $amount = $lineItem->options['amount'] ?? 0;
          if($amount)
          {
              $lineItem->price = $amount;
              $lineItem->salePrice = $amount;
          }
      }
  });
  
  ```
  
  This will set the price to the price of the line item option 'amount' that is submitted.
  
  > **Comments:**
  > **username_0**: Wicked, thanks! One note for anyone else coming across this, you'll need to `use craft\commerce\services\LineItems;` and  `use craft\commerce\events\LineItemEvent;` in your plugin file as well.  
  > **username_0**: Is setting a line item's price in an event no longer supported under Commerce 3? This broke for me with the Commerce 3 update.  
  
  ---
  
  