Answers.Body: <p>You're currently attaching this to the EVENT_BEFORE_SAVE which is working, in order for your values to appear in the form when the new product form is opened you'll need to create a custom module that injects the values into product form.</p>
  <p><code>&quot;Hmm, I still think we shouldn't need a plugin/module to achieve this.&quot;</code></p>
  <p>I would recommend filing a feature request with Craft Commerce here: <a href="https://github.com/craftcms/commerce" rel="nofollow noreferrer">https://github.com/craftcms/commerce</a></p>
  <p>Currently, this is not out-of-box functionality and does require custom coding via module/plugin.</p>
  <p>I haven't attempted to do what you're trying, but I think you'll need to look into the other events available. Maybe try using the Event Code generator located at: <a href="https://craftcms.com/docs/commerce/4.x/extend/events.html#event-code-generator" rel="nofollow noreferrer">https://craftcms.com/docs/commerce/4.x/extend/events.html#event-code-generator</a> to explore some options.</p>
  <p>It's not an answer, I know, but hopefully helps!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-05-04T15:32:23.880Z
Answers.Text: Thanks! I don't think there's any event that we can hook into that's earlier … the first time the edit form is displayed, no product exists yet, the form is just initialized with a totally empty product. I don't think this triggers an event at all – correct me if I'm wrong. Since those fields are in the sidebar and not part of the field layout, the field layout hooks won't work. I think the only way to solve this programmatically at the moment would be to insert some JS to the product form that sets the default values in the frontend, which … oof.
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-05-04T15:32:34.220Z
Answers.Text: I've opened a feature request discussion here: https://github.com/craftcms/commerce/discussions/3165
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-05-04T01:36:40.170Z
Answers.LastActivityDate: 2023-05-04T01:36:40.170Z
Answers.UserDisplayName: cherrykoda
Body: <p>I'm trying to set some defaults for newly created products. That is, when a new product is created, I want to set some settings to specific default values, but users should be allowed to change those.</p>
  <pre class="lang-php prettyprint-override"><code>use craft\commerce\elements\Product;
  use craft\events\ModelEvent;
  use yii\base\Event;
  
  Event::on(
      Product::class,
      Product::EVENT_BEFORE_SAVE,
      function (ModelEvent $e) {
          if (!$e-&gt;isNew) {
              return;
          }
          $e-&gt;sender-&gt;freeShipping = true;
          $e-&gt;sender-&gt;defaultVariant-&gt;hasUnlimitedStock = true;
      },
  );
  </code></pre>
  <p>This is working, but the default values are only set when the product is first saved. This means that the user presses <em>+ New Product</em> from the product index list, then sees the full product edit form <em>without</em> my defaults. Only after the users clicks <em>Save</em> is the hook triggered, and my default values are set. So if the user has already changed those options, they are overwritten.</p>
  <p>I think the problem is that Commerce products don't have full draft support. For entries, the default values are set before the user sees the edit form for the first time, because pressing <em>+ New entry</em> creates an unpublished draft in the database immediately, triggering the <code>EVENT_BEFORE_SAVE</code> hook. But products don't do this, they present an empty edit form with nothing saved in the database before the first save, so the hook isn't triggered.</p>
  <p>This also presents another problem: If there are validation errors, the hook triggers multiple times, because the product is always new (<code>$e-&gt;isNew</code>) until it is saved successfully. So the default values will be set multiple times, overwriting the values set by the editor, until the product is saved successfully at least once.</p>
  <p>Is there another hook I could use, or another way to solve this?</p>
  <ul>
  <li>Craft 4.4.7.1</li>
  <li>Craft Commerce 4.2.7</li>
  </ul>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-04-20T16:41:38.733Z
Comments.Text: Let me know if you find a good solution for this, I need this functionality too! May be a good feature request - these two parameters should really be configurable imo.
Comments.UserDisplayName: James Smith
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-04-20T16:45:38.807Z
Comments.Text: @JamesSmith Until someone can suggest a better way, I'll go with the imperfect solution mentioned above. A feature request will probably not be necessary – I've read somewhere on GitHub that full draft support for products is planned for Commerce 5. At this point this should resolve itself, since the hook will trigger immediately once new products are created as unpublished drafts immediately (same as entries).
Comments.UserDisplayName: MoritzLost
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-04-20T17:24:45.120Z
Comments.Text: Hmm, I still think we shouldn't need a plugin/module to achieve this. There's currently no way to opt-out of Commerce's inventory management other than to mark each product as "unlimited stock" every time you create one. It could be a simple tickbox in the same spot where you can turn off dimensions & weight fields.
Comments.UserDisplayName: James Smith
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-04-21T07:21:33.240Z
Comments.Text: @JamesSmith Yeah, having more configuration options for the built-in fields would be great!
Comments.UserDisplayName: MoritzLost
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-05-04T15:30:58.370Z
Comments.Text: @JamesSmith I've added a a feature request discussion here: https://github.com/craftcms/commerce/discussions/3165
Comments.UserDisplayName: MoritzLost
Tags: <plugin-craftcommerce><events><hook><products><plugin-craftcommerce4>
Title: Set default values for new Commerce products
ThreadText: # Set default values for new Commerce products
  
  (Asked by: username_0 on 2023-04-20)
  
  I'm trying to set some defaults for newly created products. That is, when a new product is created, I want to set some settings to specific default values, but users should be allowed to change those.
  
  ```php
  use craft\commerce\elements\Product;
  use craft\events\ModelEvent;
  use yii\base\Event;
  
  Event::on(
      Product::class,
      Product::EVENT_BEFORE_SAVE,
      function (ModelEvent $e) {
          if (!$e->isNew) {
              return;
          }
          $e->sender->freeShipping = true;
          $e->sender->defaultVariant->hasUnlimitedStock = true;
      },
  );
  
  ```
  
  This is working, but the default values are only set when the product is first saved. This means that the user presses *+ New Product* from the product index list, then sees the full product edit form *without* my defaults. Only after the users clicks *Save* is the hook triggered, and my default values are set. So if the user has already changed those options, they are overwritten.
  
  I think the problem is that Commerce products don't have full draft support. For entries, the default values are set before the user sees the edit form for the first time, because pressing *+ New entry* creates an unpublished draft in the database immediately, triggering the `EVENT_BEFORE_SAVE` hook. But products don't do this, they present an empty edit form with nothing saved in the database before the first save, so the hook isn't triggered.
  
  This also presents another problem: If there are validation errors, the hook triggers multiple times, because the product is always new (`$e->isNew`) until it is saved successfully. So the default values will be set multiple times, overwriting the values set by the editor, until the product is saved successfully at least once.
  
  Is there another hook I could use, or another way to solve this?
  
  * Craft 4.4.7.1
  * Craft Commerce 4.2.7
  
  **Tags:** plugin-craftcommerce, events, hook, products, plugin-craftcommerce4
  
  > **Comments:**
  > **username_1**: Let me know if you find a good solution for this, I need this functionality too! May be a good feature request - these two parameters should really be configurable imo.  
  > **username_0**: @username_1 Until someone can suggest a better way, I'll go with the imperfect solution mentioned above. A feature request will probably not be necessary – I've read somewhere on GitHub that full draft support for products is planned for Commerce 5. At this point this should resolve itself, since the hook will trigger immediately once new products are created as unpublished drafts immediately (same as entries).  
  > **username_1**: Hmm, I still think we shouldn't need a plugin/module to achieve this. There's currently no way to opt-out of Commerce's inventory management other than to mark each product as "unlimited stock" every time you create one. It could be a simple tickbox in the same spot where you can turn off dimensions & weight fields.  
  > **username_0**: @username_1 Yeah, having more configuration options for the built-in fields would be great!  
  > **username_0**: @username_1 I've added a a feature request discussion here: https://github.com/craftcms/commerce/discussions/3165  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2023-05-04. Score: 2
  
  You're currently attaching this to the EVENT\_BEFORE\_SAVE which is working, in order for your values to appear in the form when the new product form is opened you'll need to create a custom module that injects the values into product form.
  
  `"Hmm, I still think we shouldn't need a plugin/module to achieve this."`
  
  I would recommend filing a feature request with Craft Commerce here: <https://github.com/craftcms/commerce>
  
  Currently, this is not out-of-box functionality and does require custom coding via module/plugin.
  
  I haven't attempted to do what you're trying, but I think you'll need to look into the other events available. Maybe try using the Event Code generator located at: <https://craftcms.com/docs/commerce/4.x/extend/events.html#event-code-generator> to explore some options.
  
  It's not an answer, I know, but hopefully helps!
  
  > **Comments:**
  > **username_0**: Thanks! I don't think there's any event that we can hook into that's earlier … the first time the edit form is displayed, no product exists yet, the form is just initialized with a totally empty product. I don't think this triggers an event at all – correct me if I'm wrong. Since those fields are in the sidebar and not part of the field layout, the field layout hooks won't work. I think the only way to solve this programmatically at the moment would be to insert some JS to the product form that sets the default values in the frontend, which … oof.  
  > **username_0**: I've opened a feature request discussion here: https://github.com/craftcms/commerce/discussions/3165  
  
  ---
  
  