1
votes

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.

1
Where dbx_post_sidebar action hook is coming from? This is not a wordpress or a Woocommerce hook. So If you are using a plugin or some other costumizations, you should edit your question adding the necessary details related to that. Without it, nobody can help you.LoicTheAztec
Dbx sidebar coming from theme as when i add button it display button at all postParth Shah
Please @LoicTheAztec can u send me proper refrence or a code to submit i am trying from many days .... please help meParth Shah

1 Answers

4
votes

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