Mostafa Mostafa - 25 days ago 11
PHP Question

WooCommerce Orders metabox: Run php code on custom submit action

In Woocommerce, I have been able to add a custom submit button on order edit page in a custom metabox.

Here is my code (added in

function.php
Wordpress theme):

add_action( 'add_meta_boxes', 'MY_order_meta_boxes' );
function MY_order_meta_boxes() {

add_meta_box(
'woocommerce-order-verifyemail',
__( 'Trusted List' ),
'order_meta_box_content',
'shop_order',
'side',
'default'
);
}

function order_meta_box_content( $order_id ) {
global $woocommerce, $table_prefix, $wpdb;
$order = new WC_Order( $order_id );
$customeremail = $order->get_billing_email();
?>

<form method="post" action="CURRENT_FILE_URL">
<input type="submit" name="submit" value="submit"/>
</form>

<?php
if(isset($submit)) {$order->add_order_note(sprintf("test2"));}
?>

<?php
return $order_id;
}


But I don't know why, the code doesn't run when button is clicked (submitted).

How can I run some custom code, when submit button is clicked on this custom metabox?

Answer Source

To make this work as you expect you need some more things. I have also removed unnecessary code and some errors. Also <imput> "submit" ID is too generic and can make unexpected errors.

You will be able to do any action (or saves) in a custom function hooked in save_post action hook:

// Add a custom metabox
add_action( 'add_meta_boxes', 'trusted_list_order_meta_boxes' );
function trusted_list_order_meta_boxes() {

    add_meta_box(
        'woocommerce-order-verifyemail',
        __( 'Trusted List' ),
        'trusted_list_order_meta_box_content',
        'shop_order',
        'side',
        'default'
    );
}

// Custom metabox content
function trusted_list_order_meta_box_content( $post ){
    $customeremail = get_post_meta( $post->ID, '_billing_email', true);
    $button_text = __( 'Add Note action', 'woocommerce' );

    echo '<form method="post" action="CURRENT_FILE_URL">
        <input type="submit" name="submit_trusted_list" value="' . $button_text . '"/>
        <input type="hidden" name="trusted_list_nonce" value="' . wp_create_nonce() . '">
    </form>';
}

// Saving or doing an action when submitting
add_action( 'save_post', 'trusted_list_save_meta_box_data' );
function trusted_list_save_meta_box_data( $post_id ){

    // Only for shop order
    if ( 'shop_order' != $_POST[ 'post_type' ] )
        return $post_id;

    // Check if our nonce is set (and our cutom field)
    if ( ! isset( $_POST[ 'trusted_list_nonce' ] ) && isset( $_POST['submit_trusted_list'] ) )
        return $post_id;

    $nonce = $_POST[ 'trusted_list_nonce' ];

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $nonce ) )
        return $post_id;

    // Checking that is not an autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;

    // Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
    if ( ! current_user_can( 'edit_shop_order', $post_id ) && ! current_user_can( 'edit_shop_orders', $post_id ) )
        return $post_id;

    // Action to make or (saving data)
    if( isset( $_POST['submit_trusted_list'] ) ) {
        $order = wc_get_order( $post_id );
        // $customeremail = $order->get_billing_email();
        $order->add_order_note(sprintf("test2"));
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Code is tested on Woocommerce 3+ and works. You will get:

enter image description here

In your custom Metabox content function you will not be able to get any data using $_POST submitted data… So for example $_POST['submit'] will be always empty.