Answers.Body: <p>[** Updated Dec 2016]</p>
  
  <p>I think at this stage the best approach will be plugin based.  </p>
  
  <p>Create a business logic plugin if you haven't already.</p>
  
  <p>Two good ways of doing that are : <a href="https://github.com/lindseydiloreto/craft-businesslogic" rel="nofollow noreferrer">Business Logic Plugin</a> or via <a href="https://pluginfactory.io" rel="nofollow noreferrer">https://pluginfactory.io</a></p>
  
  <p>If you listen to:
  <a href="https://craftcommerce.com/docs/events-reference#commerce_lineitems.onpopulatelineitem" rel="nofollow noreferrer">https://craftcommerce.com/docs/events-reference#commerce_lineitems.onpopulatelineitem</a></p>
  
  <p>...then you can do (basically any) logic on the price as it is created as a lineItem in the cart (i.e. added to the cart).</p>
  
  <p>Here's some sketch code to get you started:</p>
  
  <pre><code>craft()-&gt;on('commerce_lineItems.onPopulateLineItem', function($event){
  
    $purchasable= $event-&gt;params['purchasable'];
    $lineItem = $event-&gt;params['lineItem'];
  
    if(isset($lineItem-&gt;options['giftWrapped']) &amp;&amp; $lineItem-&gt;options['giftWrapped'] == 'yes')){
        $lineItem-&gt;price = $lineItem-&gt;price + 1;
    }
  
  });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-27T13:57:33.670Z
Answers.Text: Off-topic but I dig that term, "sketch code".  (I've used "roughs" before but in  some cases, sketch code is  a better description.)
Answers.UserDisplayName: RitterKnight
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-02T21:44:13.530Z
Answers.Text: Right you are! Will update
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-07T23:41:47.397Z
Answers.Text: A further update - Commerce (v 1.1.1211+) has just got a new event - https://craftcommerce.com/docs/events-reference#commerce_lineitems.onpopulatelineitem - which now appears to to the the recommended point to address the pricing of the purchasable.
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-15T18:38:25.860Z
Answers.Text: What about the 'commerce_products.onBeforeSaveProduct' event? It seems like it would be better to do this price change whenever creating or updating a product, that way I can display the proper price on the front end. I'm having difficulty figuring out how to get the price using this event, and furthermore how it would save the update price. Is there an example on manipulating data like this via init() somewhere?
Answers.UserDisplayName: taylor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-16T00:57:19.943Z
Answers.Text: Hi Taylor - you could do that - I figured you meant it was really a dynamic price in that it would change depending on e.g. a variable wholesale rate or whatever.  If it's actually just a calculated price that then remains static once cacluated, you could indeed do it onBeforeSaveProduct I'd say (...but why not just work it out in a spreadsheet and just enter the calculated price then?)
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-26T22:55:09.400Z
Answers.LastActivityDate: 2016-12-22T22:44:17.327Z
Answers.LastEditDate: 2016-12-22T22:44:17.327Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>Rather than a static price for each product, I need the price to be dynamic based on two other fields of that product. Here's an example of what I need to accomplish:</p>
  
  <pre><code>Product
  
  Field 1: Wholesale Cost (int)
  Field 2: Increase Factor (int)
  
  Price: (Wholesale Cost * Increase Factor) + Wholesale Cost
  </code></pre>
  
  <p>Digging around, I am finding absolutely nothing in regards to making the price of products dynamic. Is this possible at all with Craft Commerce, and if so how would I go about this?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-30T20:37:55.327Z
Comments.Text: I'm working on a same kind of dynamic priced product, thanks for your tip. I'm wondering, how would you save the modified price? Trying 'craft()->commerce_orders->saveOrder($order);' doesn't do anything yet, but i'll try some other ways. Thanks, Bob
Comments.UserDisplayName: Bob Donderwinkel
Tags: <plugin-craftcommerce><ecommerce>
Title: Craft Commerce: Dynamic Product Price
ThreadText: # Craft Commerce: Dynamic Product Price
  
  (Asked by: username_0 on 2016-05-26)
  
  Rather than a static price for each product, I need the price to be dynamic based on two other fields of that product. Here's an example of what I need to accomplish:
  
  ```
  Product
  
  Field 1: Wholesale Cost (int)
  Field 2: Increase Factor (int)
  
  Price: (Wholesale Cost * Increase Factor) + Wholesale Cost
  
  ```
  
  Digging around, I am finding absolutely nothing in regards to making the price of products dynamic. Is this possible at all with Craft Commerce, and if so how would I go about this?
  
  **Tags:** plugin-craftcommerce, ecommerce
  
  > **Comments:**
  > **username_1**: I'm working on a same kind of dynamic priced product, thanks for your tip. I'm wondering, how would you save the modified price? Trying 'craft()->commerce_orders->saveOrder($order);' doesn't do anything yet, but i'll try some other ways. Thanks, Bob  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2016-05-26. Score: 8 | Accepted answer
  
  [\*\* Updated Dec 2016]
  
  I think at this stage the best approach will be plugin based.
  
  Create a business logic plugin if you haven't already.
  
  Two good ways of doing that are : [Business Logic Plugin](https://github.com/lindseydiloreto/craft-businesslogic) or via <https://pluginfactory.io>
  
  If you listen to:
  <https://craftcommerce.com/docs/events-reference#commerce_lineitems.onpopulatelineitem>
  
  ...then you can do (basically any) logic on the price as it is created as a lineItem in the cart (i.e. added to the cart).
  
  Here's some sketch code to get you started:
  
  ```
  craft()->on('commerce_lineItems.onPopulateLineItem', function($event){
  
    $purchasable= $event->params['purchasable'];
    $lineItem = $event->params['lineItem'];
  
    if(isset($lineItem->options['giftWrapped']) && $lineItem->options['giftWrapped'] == 'yes')){
        $lineItem->price = $lineItem->price + 1;
    }
  
  });
  
  ```
  
  > **Comments:**
  > **username_3**: Off-topic but I dig that term, "sketch code".  (I've used "roughs" before but in  some cases, sketch code is  a better description.)  
  > **username_2**: Right you are! Will update  
  > **username_2**: A further update - Commerce (v 1.1.1211+) has just got a new event - https://craftcommerce.com/docs/events-reference#commerce_lineitems.onpopulatelineitem - which now appears to to the the recommended point to address the pricing of the purchasable.  
  > **username_0**: What about the 'commerce_products.onBeforeSaveProduct' event? It seems like it would be better to do this price change whenever creating or updating a product, that way I can display the proper price on the front end. I'm having difficulty figuring out how to get the price using this event, and furthermore how it would save the update price. Is there an example on manipulating data like this via init() somewhere?  
  > **username_2**: Hi username_0 - you could do that - I figured you meant it was really a dynamic price in that it would change depending on e.g. a variable wholesale rate or whatever.  If it's actually just a calculated price that then remains static once cacluated, you could indeed do it onBeforeSaveProduct I'd say (...but why not just work it out in a spreadsheet and just enter the calculated price then?)  
  
  ---
  
  