phpwordpresswoocommercebackendhook-wordpress

Run a function on custom button click in WooCommerce admin order list (HPOS)


Based on "Add a button on top of admin orders list in woocommerce" answer code, I was able to add a custom button on woocommerce admin orders list.

Here is that code (lightly customized):

add_action( 'manage_posts_extra_tablenav', 'admin_order_list_top_bar_button', 20, 1 );
function admin_order_list_top_bar_button( $which ) {
    global $typenow;

    if ( 'shop_order' === $typenow && 'top' === $which ) {
        ?>
        <div class="alignleft actions custom">
            <button type="submit" name="custom_" style="height:32px;" class="button" value=""><?php
                echo __( 'Import Couriers', 'woocommerce' ); ?></button>
        </div>
        <?php
    }
}

Now I need to run a the following function when this custom button is clicked:

function update_shipping_couriers_meta_field() {
    $dir = __DIR__;
    $couriers = file( $dir . '/import-couriers.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
    $count = count(couriers);
    $i = 1;

    do {
        if ( !empty( $couriers ) ) {
            foreach ( $couriers as $a ) {
                if ( !empty( $a ) ) {
                    $rows = explode(';', $a);

                    $id = $rows[0];
                    $id = int($id);
                    $couriers = $rows[1];

                    update_post_meta( $id, '_shipping_couriers', $couriers );
                }
                $i++;
            }
        }
    } 
    while ( $i <= $count );
}

In practice, the function updates a "_shipping_couriers" custom field based on a specific order ID. The two values ​​are present in a csv file.

I've already tested it and it's working. I "just" have it run when I click on the button I created with the function above.

How can I run my function when the button is clicked?


Solution

  • Update for High Performance Order Storage (HPOS).

    There are some missing things in your code and an error in your last function where count(couriers); need to be instead count($couriers);.

    Try the following:

    // Display an action button in admin order list header:
    add_action( 'manage_posts_extra_tablenav', 'admin_order_list_top_bar_button', 20, 1 );
    function admin_order_list_top_bar_button( $which ) {
        global $pagenow, $typenow;
    
        if ( 'shop_order' === $typenow && 'edit.php' === $pagenow && 'top' === $which ) {
            printf('<div class="alignleft actions import-courier">
            <button type="submit" name="import_courier" style="height:32px;" class="button" value="%s">%s</button>
            </div>', 'yes', esc_html__('Import Couriers', 'woocommerce') );
        }
    }
    
    // Trigger an action (or run some code) when the button is pressed:
    add_action( 'restrict_manage_posts', 'admin_order_list_update_shop_orders_action' );
    function admin_order_list_update_shop_orders_action() {
        global $pagenow, $typenow;
    
        if ( 'shop_order' === $typenow && 'edit.php' === $pagenow &&
        isset($_GET['import_courier']) && $_GET['import_courier'] === 'yes' ) {
            
            ## -------- The code to be trigered -------- ##
            
            update_shipping_couriers_meta_field();
            
            ## -------------- End of code -------------- ##
        }
    }
    
    // Utility function that will be triggered on button press
    function update_shipping_couriers_meta_field() {
        $dir = __DIR__;
        $couriers = file( $dir . '/import-couriers.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
        $count = count($couriers);
        $i = 1;
        
        do {
            if ( ! empty( $couriers ) ) {
                foreach ( $couriers as $a ) {
                    if ( ! empty( $a ) ) {
                        $rows = explode(';', $a);
        
                        update_post_meta( intval($rows[0]), '_shipping_couriers', $rows[1] );
                    }
                    $i++;
                }
            }
        } 
        while ( $i <= $count );
    }
    

    To make is work when High Performance Order Storage (HPOS) is enabled, replace the first 2 functions with the following ones:

    // Display an action button in admin order list header:
    add_action( 'woocommerce_order_list_table_extra_tablenav', 'admin_hpos_wc_order_list_top_bar_button', 20, 2 ); 
    function admin_hpos_wc_order_list_top_bar_button( $order_type, $which ) {
        if( 'shop_order' === $order_type && 'top' === $which ) {
            printf('<div class="alignright actions import-courier">
            <button type="submit" name="import_courier" style="height:32px;" class="button" value="%s">%s</button>
            </div>', 'yes', esc_html__('Import Couriers', 'woocommerce') );
        }
    }
    
    // Trigger an action (or run some code) when the button is pressed:
    add_action( 'woocommerce_order_list_table_restrict_manage_orders', 'admin_hpos_wc_order_list_update_action', 20, 2 );
    function admin_hpos_wc_order_list_update_action( $order_type, $which ) {
    
        if ( 'shop_order' === $order_type &&  'top' === $which &&
        isset($_GET['import_courier']) && $_GET['import_courier'] === 'yes' ) {
            
            ## -------- The code to be trigered -------- ##
            
            update_shipping_couriers_meta_field();
            
            ## -------------- End of code -------------- ##
        }
    }
    

    See: Filtering orders list in WooCommerce with HPOS

    Code goes in functions.php file of your child theme (or in a plugin file).

    enter image description here

    Based on: Add a button on top of WooCommerce admin orders list (HPOS)