<p>Removed the var_dump() from setting the variable as follows, and it works perfectly:</p>

<pre><code>craft()-&gt;on('commerce_lineItems.onPopulateLineItem', function($event)
{
    $lineItem = $event-&gt;params['lineItem'];
    $globalPrices = craft()-&gt;globals-&gt;getSetByHandle('defaultPrices');
    $itemPrice = $globalPrices-&gt;fontStylePrice;
    $lineItem-&gt;price = $itemPrice;
</code></pre>

CC BY-SA 3.0
2016-08-22T13:24:38.703Z
Well yeah sorry I thought when you answered brad you meant that was just a typo above. Vardump returns a string... Must be a wierd se
Jeremy Daalder
CC BY-SA 3.0
2016-08-22T13:25:52.377Z
..se post to recommend assigning var dump to a var?  Well there are uses for that but... In any case I was right...you were assigning a string to it...type error!
Jeremy Daalder
CC BY-SA 3.0
2016-08-22T13:26:13.363Z
Yeah, I missed the point of his question, I was thinking of the variable name :P
Adam Ladrach
CC BY-SA 3.0
2016-08-22T13:22:16.280Z
2016-08-22T13:22:16.280Z
Adam Ladrach
<p>Just a wild guess really, but it feels like this might be a type issue - is the price you're getting back from the global a float?  Or is it a string maybe?</p>

<p><code>lineItem-&gt;price = floatval($itemPrice);</code></p>

<p>...might be what you need?</p>

CC BY-SA 3.0
2016-08-22T12:47:54.133Z
Still getting a $0 value :/ I'm not sure what's going on. I did set the field type as a number.
Adam Ladrach
CC BY-SA 3.0
2016-08-22T13:12:35.253Z
But what type of value does var_dump say it is?  Probably not it, but worth a check..
Jeremy Daalder
CC BY-SA 3.0
2016-08-22T13:20:57.423Z
Err in an event handler you probably can't. This is what phpstorm / xdebug are great for! But basically it had to be either you're not getting the value from the global ok, or there's some problem assigning to the price because of what you do get back.
Jeremy Daalder
CC BY-SA 3.0
2016-08-22T13:22:38.920Z
I removed the var_dump from where I set the variable and it works! Probably a silly thing to do, but I just did what the SE post said :P
Adam Ladrach
CC BY-SA 3.0
2016-08-22T00:55:55.317Z
2016-08-22T00:55:55.317Z
Jeremy Daalder
<p>I'm trying to set the price of the item being added to the cart by a global variable, which is a number field. Here's what I have:</p>

<pre><code>craft()-&gt;on('commerce_lineItems.onPopulateLineItem', function($event)
{
    $lineItem = $event-&gt;params['lineItem'];
    $globalPrices = craft()-&gt;globals-&gt;getSetByHandle('defaultPrices');
    $itemPrice = var_dump($globalPrices-&gt;fontStylePrice);
    $lineItem-&gt;price = $itemPrice;
</code></pre>

<p>This just results in a $0 price being set, and not the value from the Global Variable. But again, <code>$lineItem-&gt;price = 25</code> works fine. Any suggestions?</p>

CC BY-SA 3.0
2016-08-22T00:42:36.663Z
Are you testing with `var_dump($globalPrices->fontStylePrice);` in place or is that just a typo in your code example?
Brad Bell
CC BY-SA 3.0
2016-08-22T12:43:19.803Z
Yeah, I swapped some of the tags out for placeholders, just didn't get them all apparently.
Adam Ladrach
<plugin-craftcommerce>
Using Global Variable as Price for Variant
# Using Global Variable as Price for Variant

(Asked by: username_0 on 2016-08-21)

I'm trying to set the price of the item being added to the cart by a global variable, which is a number field. Here's what I have:
craft()->on('commerce_lineItems.onPopulateLineItem', function($event)
{
    $lineItem = $event->params['lineItem'];
    $globalPrices = craft()->globals->getSetByHandle('defaultPrices');
    $itemPrice = var_dump($globalPrices->fontStylePrice);
    $lineItem->price = $itemPrice;

This just results in a $0 price being set, and not the value from the Global Variable. But again, $lineItem->price = 25 works fine. Any suggestions?

**Tags:** plugin-craftcommerce
> **Comments:**
> **username_1**: Are you testing with `var_dump($globalPrices->fontStylePrice);` in place or is that just a typo in your code example?  
> **username_0**: Yeah, I swapped some of the tags out for placeholders, just didn't get them all apparently.  

---

## 2 answers

### Answer by username_0 on 2016-08-22. Score: 1

Removed the var_dump() from setting the variable as follows, and it works perfectly:
craft()->on('commerce_lineItems.onPopulateLineItem', function($event)
{
    $lineItem = $event->params['lineItem'];
    $globalPrices = craft()->globals->getSetByHandle('defaultPrices');
    $itemPrice = $globalPrices->fontStylePrice;
    $lineItem->price = $itemPrice;

> **Comments:**
> **username_2**: Well yeah sorry I thought when you answered brad you meant that was just a typo above. Vardump returns a string... Must be a wierd se  
> **username_2**: ..se post to recommend assigning var dump to a var?  Well there are uses for that but... In any case I was right...you were assigning a string to it...type error!  
> **username_0**: Yeah, I missed the point of his question, I was thinking of the variable name :P  

---

### Answer by username_2 on 2016-08-22. Score: 0

Just a wild guess really, but it feels like this might be a type issue - is the price you're getting back from the global a float? Or is it a string maybe?

`lineItem->price = floatval($itemPrice);`

...might be what you need?

> **Comments:**
> **username_0**: Still getting a $0 value :/ I'm not sure what's going on. I did set the field type as a number.  
> **username_2**: But what type of value does var_dump say it is?  Probably not it, but worth a check..  
> **username_2**: Err in an event handler you probably can't. This is what phpstorm / xdebug are great for! But basically it had to be either you're not getting the value from the global ok, or there's some problem assigning to the price because of what you do get back.  
> **username_0**: I removed the var_dump from where I set the variable and it works! Probably a silly thing to do, but I just did what the SE post said :P  

---

