<p>Something like this should do the trick.</p>
<pre><code>use craft\commerce\events\LineItemEvent;
use craft\commerce\services\LineItems;
use craft\commerce\models\LineItem;
use yii\base\Event;
 
Event::on(
LineItems::class,
LineItems::EVENT_POPULATE_LINE_ITEM,
function(LineItemEvent $event) {

 $lineItem = $event-&gt;lineItem;
 $options = $lineItem-&gt;getOptions(); 

 if (isset($options['customPrice'])) { 
  $lineItem-&gt;salePrice = $options['customPrice'];
  $lineItem-&gt;price = $options['customPrice'];
 }

});
</code></pre>
<p>Other examples can be found <a href="https://craftcms.com/knowledge-base/dynamically-customizing-line-item-prices" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 4.0
2022-04-23T16:22:37.347Z
2022-04-23T16:22:37.347Z
Mike Niland
<p>It's not possible to send the price to the cart in this way.</p>

<p>You will need to send that price as a custom option on the item and then use a custom module or plugin to update the line item's price.</p>

<p>You can just alter the line item price directly if desired but it may be more appropriate to create an adjuster to do it.</p>

<p>So your add to cart form might look like...</p>

<pre><code>&lt;form method="POST"&gt;
    &lt;input type="hidden" name="action" value="commerce/cart/update-cart"&gt;
    {{ redirectInput('shop/cart') }}
    {{ csrfInput() }}
    &lt;input type="hidden" name="options[customPrice]" value="HERE NEW PRICE"&gt;
    &lt;input type="hidden" name="purchasableId" value="{{ variant.id }}"&gt;
    &lt;input type="submit" value="Add to cart"&gt;
&lt;/form&gt;
</code></pre>

<p>Then your custom module would listen for a Commerce event, probably <code>populateLineItem</code> (see events here <a href="https://docs.craftcms.com/commerce/v2/events.html#order-related-events" rel="nofollow noreferrer">https://docs.craftcms.com/commerce/v2/events.html#order-related-events</a>)  and update the item's price.</p>

<pre><code>use craft\commerce\events\LineItemEvent;
use craft\commerce\services\LineItems;
use yii\base\Event;

Event::on(LineItems::class, LineItems::EVENT_POPULATE_LINE_ITEM, function(LineItemEvent $e) {
   // update the line item's price with the customPrice option from this line item
   // save the line item
});
</code></pre>

CC BY-SA 4.0
2019-08-25T16:30:24.083Z
probably: public function handlePopulateLineItem(LineItemEvent $event)
{
    $options = $event->lineItem->getOptions();
    if (isset($options['customPrice'])) {
        $event->lineItem->price = $options['customPrice'];
    }
}
Nikolay Bronskiy
CC BY-SA 4.0
2019-08-25T16:16:01.627Z
2019-08-25T16:25:32.893Z
2019-08-25T16:25:32.893Z
foamcow
<p>I want to add range field that will change price based on its value.
How can I send updated price to the cart? Is there something easy like:</p>

<pre><code>{% set product = craft.products.one() %}
{% set variant = product.defaultVariant %}

&lt;form method="POST"&gt;
    &lt;input type="hidden" name="action" value="commerce/cart/update-cart"&gt;
    {{ redirectInput('shop/cart') }}
    {{ csrfInput() }}
    &lt;input type="hidden" name="price" value="HERE NEW PRICE"&gt;
    &lt;input type="hidden" name="purchasableId" value="{{ variant.id }}"&gt;
    &lt;input type="submit" value="Add to cart"&gt;
&lt;/form&gt;
</code></pre>

<craft3><plugin-craftcommerce2>
How can I change variant price in Craft Commerce 2 form
# How can I change variant price in Craft Commerce 2 form

(Asked by: username_0 on 2019-08-25)

I want to add range field that will change price based on its value.
How can I send updated price to the cart? Is there something easy like:
{% set product = craft.products.one() %}
{% set variant = product.defaultVariant %}


    
    {{ redirectInput('shop/cart') }}
    {{ csrfInput() }}

**Tags:** craft3, plugin-craftcommerce2

---

## 2 answers

### Answer by username_2 on 2019-08-25. Score: 4 | Accepted answer

It's not possible to send the price to the cart in this way.
You will need to send that price as a custom option on the item and then use a custom module or plugin to update the line item's price.
You can just alter the line item price directly if desired but it may be more appropriate to create an adjuster to do it.
So your add to cart form might look like...

    
    {{ redirectInput('shop/cart') }}
    {{ csrfInput() }}
    
    
    


Then your custom module would listen for a Commerce event, probably populateLineItem (see events here https://docs.craftcms.com/commerce/v2/events.html#order-related-events)  and update the item's price.
use craft\commerce\events\LineItemEvent;
use craft\commerce\services\LineItems;
use yii\base\Event;

Event::on(LineItems::class, LineItems::EVENT_POPULATE_LINE_ITEM, function(LineItemEvent $e) {
   // update the line item's price with the customPrice option from this line item
   // save the line item
});

> **Comments:**
> **username_0**: probably: public function handlePopulateLineItem(LineItemEvent $event) {     $options = $event->lineItem->getOptions();     if (isset($options['customPrice'])) {         $event->lineItem->price = $options['customPrice'];     } }  

---

### Answer by username_1 on 2022-04-23. Score: 2

Something like this should do the trick.
use craft\commerce\events\LineItemEvent;
use craft\commerce\services\LineItems;
use craft\commerce\models\LineItem;
use yii\base\Event;
 
Event::on(
LineItems::class,
LineItems::EVENT_POPULATE_LINE_ITEM,
function(LineItemEvent $event) {

 $lineItem = $event->lineItem;
 $options = $lineItem->getOptions(); 

 if (isset($options['customPrice'])) { 
  $lineItem->salePrice = $options['customPrice'];
  $lineItem->price = $options['customPrice'];
 }

});

Other examples can be found here.


---

