Parth Shah
Parth Shah

Reputation: 167

How to add action button on woocommerce admin order detail page

In Woocommerce, I have added button with meta boxes but on click of that button at admin order detail page showing order updated and next function is not getting execute.

This is my code:

add_action( 'add_meta_boxes', 'add_meta_boxesws' );
function add_meta_boxesws()
{
    add_meta_box( 
        'add_meta_boxes', 
        __( 'Custom' ), 
        'sun'
    );
}

function sun(){
    echo '<input type="hidden" value="abc" name="abc"/>';
    echo '<p><button id="mybutton" type="submit">Return Shipment</button></p>';
}

if ( !empty( $_POST['abc'] ) )  {
    function call_this(){
        echo "hello";    
    }
    add_action('dbx_post_sidebar','call_this');
}

Any Help will be appreciated.

Upvotes: 1

Views: 7003

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254378

Is better to use the GET method and display the result in your meta-box content under the button itself. So you will replace you <button>, by a <a href=""> html tag…

Your revisited code:

// Add a custom metabox only for shop_order post type (order edit pages)
add_action( 'add_meta_boxes', 'add_meta_boxesws' );
function add_meta_boxesws()
{
    add_meta_box( 'custom_order_meta_box', __( 'My Title' ),
        'custom_metabox_content', 'shop_order', 'normal', 'default');
}

function custom_metabox_content(){
    $post_id = isset($_GET['post']) ? $_GET['post'] : false;
    if(! $post_id ) return; // Exit

    $value="abc";
    ?>
        <p><a href="?post=<?php echo $post_id; ?>&action=edit&abc=<?php echo $value; ?>" class="button"><?php _e('Return Shipment'); ?></a></p>
    <?php
    // The displayed value using GET method
    if ( isset( $_GET['abc'] ) && ! empty( $_GET['abc'] ) ) {
        echo '<p>Value: '.$_GET['abc'].'</p>';
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

Once submitted:

enter image description here

Upvotes: 5

Related Questions