<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>

CC BY-SA 3.0
2016-05-27T13:57:33.670Z
Off-topic but I dig that term, "sketch code".  (I've used "roughs" before but in  some cases, sketch code is  a better description.)
RitterKnight
CC BY-SA 3.0
2016-06-02T21:44:13.530Z
Right you are! Will update
Jeremy Daalder
CC BY-SA 3.0
2016-06-07T23:41:47.397Z
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.
Jeremy Daalder
CC BY-SA 3.0
2016-06-15T18:38:25.860Z
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?
taylor
CC BY-SA 3.0
2016-06-16T00:57:19.943Z
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?)
Jeremy Daalder
CC BY-SA 3.0
2016-05-26T22:55:09.400Z
2016-12-22T22:44:17.327Z
2016-12-22T22:44:17.327Z
Jeremy Daalder
<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>

CC BY-SA 3.0
2016-05-30T20:37:55.327Z
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
Bob Donderwinkel
<plugin-craftcommerce><ecommerce>
Craft Commerce: Dynamic Product Price
# 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 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 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?)  

---

