0

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?

  • Are you testing with var_dump($globalPrices->fontStylePrice); in place or is that just a typo in your code example? – Brad Bell Aug 22 '16 at 0:42
  • Yeah, I swapped some of the tags out for placeholders, just didn't get them all apparently. – Adam Ladrach Aug 22 '16 at 12:43
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;
  • 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 Aug 22 '16 at 13:24
  • ..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 Aug 22 '16 at 13:25
  • Yeah, I missed the point of his question, I was thinking of the variable name :P – Adam Ladrach Aug 22 '16 at 13:26
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?

  • Still getting a $0 value :/ I'm not sure what's going on. I did set the field type as a number. – Adam Ladrach Aug 22 '16 at 12:47
  • But what type of value does var_dump say it is? Probably not it, but worth a check.. – Jeremy Daalder Aug 22 '16 at 13:12
  • 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 Aug 22 '16 at 13:20
  • 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 Aug 22 '16 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.