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

标签 php wordpress woocommerce backend hook-wordpress

基于“Add a button on top of admin orders list in woocommerce ”答案代码,我能够在 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 文件中。

我已经测试过它并且它正在工作。当我点击我用上面的函数创建的按钮时,我“只是”让它运行。

单击按钮时如何运行我的函数?

最佳答案

您的代码中缺少一些内容,最后一个函数中存在错误,其中 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 文件中。测试和工作。
来自:Add a button on top of admin orders list in woocommerce

关于php - 在 woocommerce 管理订单页面中的自定义按钮单击上运行功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56380632/

相关文章:

php - 奇怪的 mysql,显示来自 myAdmin 的 0 个结果集,但在 PHP PDO 中返回行

php - "stretch"的 Wordpress 特定方法,(最小高度)等我的页面内容,以便我的页脚后没有空格?

php - 寻求WordPress的代码高亮推荐

php - 根据 WooCommerce 中的自定义结帐单选按钮和文本字段设置动态费用

javascript - WordPress自定义插件按钮onclick调用php函数

PHP循环替换字符串 "one-by-one"中的字符

php - Codeigniter 3 - session 不工作

php - Wordpress 只在 front-page.php 上显示最近的帖子

php - Woocommerce 在订单查看页面中显示客户详细信息中的额外字段

wordpress - WooCommerce REST API - 使用后/前参数获取订单