doulue7522 2019-05-30 06:51
浏览 210
已采纳

在woocommerce管理订单页面上的自定义按钮上运行一个功能

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?

展开全部

  • 写回答

1条回答 默认 最新

  • donglu9872 2019-05-30 08:46
    关注

    There are some missing things in your code and an error in your last function where count(couriers); need to be instead 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);
    
                        $id = $rows[0];
                        $id = int($id);
                        $couriers = $rows[1];
    
                        update_post_meta( $id, '_shipping_couriers', $couriers );
                    }
                    $i++;
                }
            }
        } 
        while ( $i <= $count );
    }
    

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

    Based from: Add a button on top of admin orders list in woocommerce

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 怎么实现数组的循环累加,simulink
  • ¥15 51单片机最小开发板系统,想让寻迹小车在全检测到黑线(寻迹模块代码在第一块板子上)时蜂鸣器响(在第二块板子上)
  • ¥15 pbootcms多选调用成列表
  • ¥15 51单片机oled显示时钟
  • ¥15 小规模TSP问题的动态规划求解
  • ¥25 kubelet.service: Failed with result 'exit-code'.
  • ¥15 bitvise黑框内一键部署v2ray提示账户没有root怎么解决
  • ¥15 车型识别以及相似度匹配中细节特征提取以及图像模糊问题
  • ¥15 怎么用鸿蒙的ArkTs写出来啊
  • ¥30 websocket服务端多线程通信
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部