Answers.Body: <p>You can add weight and material as <a href="https://craftcms.com/docs/commerce/4.x/products-variants.html#product-types" rel="nofollow noreferrer">custom fields to the product type</a>. For the weight, you can also use the <a href="https://craftcms.com/docs/commerce/4.x/products-variants.html#show-the-dimensions-and-weight-fields" rel="nofollow noreferrer">built-in field</a>.</p>
  <p>To dynamically calculate the price, you could inject a bit of JavaScript that updates the price when either the material or weight fields are changed. However, this will not apply when products are programmatically created or updated.</p>
  <p>As an alternative, or in addition to this, you can use event hooks. For example, you can hook into the <a href="https://craftcms.com/docs/commerce/4.x/extend/events.html#event-code-generator" rel="nofollow noreferrer"><code>Product::EVENT_BEFORE_SAVE</code> event</a> to update the price based on your custom calculation right before a product is saved. This will work regardless of how a product is created or edited.</p>
  <p>In either case, you may want to add instructions to your price field to explain that the price is updated dynamically, so authors won't try to change it manually. You can also inject JavaScript to set the price input to <code>disabled</code> to prevent this entirely.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-05T16:02:50.260Z
Answers.LastActivityDate: 2024-01-05T16:02:50.260Z
Answers.UserDisplayName: MoritzLost
Body: <p>We're building a webshop with Craft and Craft Commerce.
  The products will have dynamic pricing based on the weight and material.</p>
  <p>Is it possible to extend Craft Commerce products to add these fields and update the price based on the inputted values?
  Basically what we need is a small price calculator in the product form.</p>
  
Tags: <plugin-craftcommerce>
Title: How to extend Craft Commerce?
ThreadText: # How to extend Craft Commerce?
  
  (Asked by: username_0 on 2024-01-05)
  
  We're building a webshop with Craft and Craft Commerce.
  The products will have dynamic pricing based on the weight and material.
  
  Is it possible to extend Craft Commerce products to add these fields and update the price based on the inputted values?
  Basically what we need is a small price calculator in the product form.
  
  **Tags:** plugin-craftcommerce
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-01-05. Score: 2 | Accepted answer
  
  You can add weight and material as [custom fields to the product type](https://craftcms.com/docs/commerce/4.x/products-variants.html#product-types). For the weight, you can also use the [built-in field](https://craftcms.com/docs/commerce/4.x/products-variants.html#show-the-dimensions-and-weight-fields).
  
  To dynamically calculate the price, you could inject a bit of JavaScript that updates the price when either the material or weight fields are changed. However, this will not apply when products are programmatically created or updated.
  
  As an alternative, or in addition to this, you can use event hooks. For example, you can hook into the [`Product::EVENT_BEFORE_SAVE` event](https://craftcms.com/docs/commerce/4.x/extend/events.html#event-code-generator) to update the price based on your custom calculation right before a product is saved. This will work regardless of how a product is created or edited.
  
  In either case, you may want to add instructions to your price field to explain that the price is updated dynamically, so authors won't try to change it manually. You can also inject JavaScript to set the price input to `disabled` to prevent this entirely.
  
  
  ---
  
  