简体   繁体   中英

do_action passing a variable with T_OBJECT_OPERATOR(->) PHP

I have a plugin that uses a custom checkout form. In this checkout form there is a "create account" checkbox identical to the default Wordpress one. I am trying to write some code to add another checkbox that allows the user to choose a role for this new account. The plugin that creates this checkout form has a do_action that I want to hook into. It even has a variable that I need. Sadly, I'm uncertain how to use this.

The do_action:

do_action( 'woocommerce_after_checkout_validation', $woocommerce_checkout->posted );

As you can see this uses $woocommerce_checkout->posted

My code has the following

add_action('woocommerce_after_checkout_validation', 'validate_professional_field' 10,1);

And In my function I try to use this with the following code:

function validate_professional_field( $woocommerce_checkout->posted ) {}

This does not work And I have no clue what to do here.. How can I use the variables sent with the action?

In the end I want to use $woocommerce_checkout->posted['createprofessional'] to get the value that should be the value of my checkbox.

Other details

This checkbox was made with the following code:

add_action('woocommerce_before_checkout_registration_form' , 'define_telesales_fields', 3);

<?php function define_telesales_fields() { ?>
    <p class="form-row form-row-wide create-professional">
        <div class="create-account">
        <input class="input-checkbox" id="createprofessional" type="checkbox" name="createprofessional" value="1" />
        <label for="createprofessional" class="checkbox"><?php _e( 'Create a professional', 'woocommerce' ); ?></label>
        </div>
    </p>          
<?php }?>

Solved

A comma was missing in the add_action

add_action('woocommerce_after_checkout_validation', 'validate_professional_field', 10,1);

Just use $woocommerce_checkout as the parameter name in the function declaration; if you're passing the whole object into the function, you can then use $woocommerce_checkout->posted within the function itself.

eg:

function validate_professional_field( $woocommerce_checkout ) {
    //$woocommerce_checkout->posted is valid here
}

//and you'd call it like this:
validate_professional_field( $woocommerce_checkout );

Alternatively, if you know you only want to use posted in the function, you could limit the argument to just that, which seems to be what you want to do. In this case, give it a valid variable name in the function declaration, like so:

function validate_professional_field( $posted ) {
    //$posted is valid here, and will be contain the data from $woocommerce_checkout->posted that you're passing into the function.
}

//and you'd call it like this:
validate_professional_field( $woocommerce_checkout->posted );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM