-1

What is the correct way to implement the second function below? The first function works fine but the second function does not correctly invoke a custom field in WordPress whose key is _ni_cost_goods.

Parse error: syntax error, unexpected '=', expecting variable (T_VARIABLE) in your code on line 11

<?php
function round_price( $price = null, $multiplier = 1, $nearest = 1, $minus = 0 ) {
    if ( !empty( $price ) ) {
        // strip any extra characters from price
        $price = preg_replace("/[^0-9,.]/", "", $price);
        // perform calculations
        return ( round ( ( $price * $multiplier ) / $nearest ) * $nearest ) - $minus; 
    }
}

function add_gst( _ni_cost_goods = null, $multiplier = 1.1) {
    if ( !empty( _ni_cost_goods ) ) {
        // strip any extra characters from price
        _ni_cost_goods = preg_replace("/[^0-9,.]/", "", _ni_cost_goods);
        // perform calculations
        return _ni_cost_goods * $multiplier; 
    }
}
?>
ptrcao
  • 421
  • 1
  • 5
  • 19

1 Answers1

0

In your second function, you have a few issues, the variables being passed to the function and the return.

function add_gst( $_ni_cost_goods = null, $multiplier = 1.1) {

    if ( !empty( $_ni_cost_goods ) ) {

        // strip any extra characters from price
        $_ni_cost_goods = preg_replace("/[^0-9,.]/", "", $price);

        // perform calculations
        return $_ni_cost_goods * $multiplier; 
    }
}

You'll noticed I added the $ sign to the _ni_cost_goods in several places. That being said, you're also accessing a variable $price which isn't in scope for the function.

Adam
  • 1,149
  • 2
  • 14
  • 21
  • I've corrected the instance of $price you alluded to, it was actually supposed to be an instance of `_ni_cost_goods`. I'll try run it and confirm everything works in a bit. – ptrcao Oct 08 '18 at 14:48
  • Glad you got your problem solved :) – Adam Oct 08 '18 at 14:55