运行 在 woocommerce 管理订单页面中单击自定义按钮的功能

Run a function on custom button click in woocommerce admin order page

基于 "" 答案代码,我能够在 woocommerce 管理员订单列表上添加自定义按钮。

这是代码(轻微定制):

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
    }
}

现在我需要 运行 单击此自定义按钮时执行以下功能:

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 );
}

实际上,该函数会根据特定的订单 ID 更新“_shipping_couriers”自定义字段。这两个值存在于一个csv文件中。

我已经对其进行了测试并且它正在运行。我 "just" 当我点击我用上面的函数创建的按钮时 运行 有它。

单击按钮时如何运行我的功能?

您的代码中缺少一些内容,并且您最后一个函数中存在错误,其中 count(couriers); 需要改为 count($couriers);

// 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 ) {
        ?>
        <div class="alignleft actions custom">
            <button type="submit" name="import_courier" style="height:32px;" class="button" value="yes"><?php
                echo __( 'Import Couriers', 'woocommerce' ); ?></button>
        </div>
        <?php
    }
}

// Trigger an action (or run some code) when the button is pressed
add_action( 'restrict_manage_posts', 'display_admin_shop_order_language_filter' );
function display_admin_shop_order_language_filter() {
    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 -------------- ##
    }
}

// Your 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 );
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

基于: