Pass PHP variable created in one function to another
You could make the variable global:
function add_custom_price( $cart_object ) { global $newVar; foreach ( $cart_object->cart_contents as $key => $value ) { $newVar = $value['data']->price; }}function function_name() { global $newVar; echo $newVar;}
Or, if $newVar
is already available in the global scope you could do:
function function_name($newVar) { echo $newVar;}// Add the hookadd_action( 'another_area', 'function_name' );// Trigger the hook with the $newVar;do_action('another_area', $newVar);
You should use return $variable in your functions:
function add_custom_price( $cart_object ) { foreach ( $cart_object->cart_contents as $key => $value ) { $newVar = $value['data']->price; } return $newVar;}function function_name($newVar) {//do something with $newVar}
And use like this:
$variable = add_custom_price( $cart_object );$xxx = function_name($variable);
UPDATE:
Looking at what @ChunkyBaconPlz said $newVar should be an array:
function add_custom_price( $cart_object ) { foreach ( $cart_object->cart_contents as $key => $value ) { $newVar[] = $value['data']->price; } return $newVar;}