pass variables of add_action" to do_action - Wordpress

Viewed 26

I have this add_action function that creates an array in the following way.

add_action( 'mixedcart', 'checkoutincluyenevio', 1, 2 );
function checkoutincluyenevio() {
$mensaje['label'] = "Incluye Envío";
$mensaje['price'] = null;
$mensaje['style'] = "background-color:#e4f4fd;font-weight:initial;";
$mensaje['class'] = "mixedcar";
return $mensaje;
}

This code is in the functions.php file of my theme.

In a template of my theme I want to retrieve the values by doing this.

$checkoutincluyenevio = do_action( 'mixedcart' );

I obtain as a result the variable $checkoutincluyenevio as NULL

The expected result would be the variable $mensaje with all its content.

What am I doing wrong?

1 Answers

I found the answer for variables better use the filters apply_filters and add_filter

it would stay this way

add_filter( 'mixedcart', 'checkoutincluyenevio', 10, 2 );

function checkoutincluyenevio($mensaje) {
$mensaje['label'] = "Incluye Envío";
$mensaje['price'] = null;
$mensaje['style'] = "background-color:#e4f4fd;font-weight:initial;";
$mensaje['class'] = "mixedcar";
return $mensaje;
}

In template

$checkoutincluyenevio = apply_filters( 'mixedcart', $mensaje );
Related