【问题标题】:Add a custom action button in WooCommerce admin order list在 WooCommerce 管理订单列表中添加自定义操作按钮
【发布时间】:2018-01-12 23:36:25
【问题描述】:

我已关注 this instructions 为我的 WooCommerce 订单添加自定义订单状态。

我找不到创建自定义操作按钮的方法,该按钮可从管理订单列表页面将订单状态更改为我的自定义状态,如下图所示:

我希望为具有“处理中”状态的订单显示此自定义操作按钮。

我在 WooCommerce 文档中找不到任何答案。

是否有应用这些按钮的钩子?
如何将其添加到function.php

谢谢

【问题讨论】:

  • 这里通过按下按钮我们保存一个新的状态,但是是否可以保存或更新一个元数据?与“update_post_meta”?按一个输入就行了?我尝试了一个链接,但一个按钮也会去:stackoverflow.com/questions/59939550/…

标签: php wordpress woocommerce status orders


【解决方案1】:

要恢复,您已经创建了一个自定义订单状态“wc-parcial”(使用您的问题中提供的说明代码),您需要将相关操作按钮添加到订单管理列表。

对于 WooCommerce 3.3+ 版,请查看更新 in this answer below

您需要使用在 woocommerce_admin_order_actions 过滤器挂钩中挂钩的自定义函数

// Add your custom order status action button (for orders with "processing" status)
add_filter( 'woocommerce_admin_order_actions', 'add_custom_order_status_actions_button', 100, 2 );
function add_custom_order_status_actions_button( $actions, $order ) {
    // Display the button for all orders that have a 'processing' status
    if ( $order->has_status( array( 'processing' ) ) ) {

        // Get Order ID (compatibility all WC versions)
        $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
        // Set the action button
        $actions['parcial'] = array(
            'url'       => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=parcial&order_id=' . $order_id ), 'woocommerce-mark-order-status' ),
            'name'      => __( 'Envio parcial', 'woocommerce' ),
            'action'    => "view parcial", // keep "view" class for a clean button CSS
        );
    }
    return $actions;
}

// Set Here the WooCommerce icon for your action button
add_action( 'admin_head', 'add_custom_order_status_actions_button_css' );
function add_custom_order_status_actions_button_css() {
    echo '<style>.view.parcial::after { font-family: woocommerce; content: "\e005" !important; }</style>';
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码已经过测试并且可以工作。你会得到:

【讨论】:

    【解决方案2】:

    Woocommerce 3.3+ 的更新版本

    要恢复,您已经创建了一个自定义订单状态“wc-parcial”(使用您的问题中提供的说明代码),您需要将相关的操作按钮添加到订单管理列表。

    新代码:

    // Add your custom order status action button (for orders with "processing" status)
    add_filter( 'woocommerce_admin_order_actions', 'add_custom_order_status_actions_button', 100, 2 );
    function add_custom_order_status_actions_button( $actions, $order ) {
        // Display the button for all orders that have a 'processing' status
        if ( $order->has_status( array( 'processing' ) ) ) {
    
            // The key slug defined for your action button
            $action_slug = 'parcial';
    
            // Set the action button
            $actions[$action_slug] = array(
                'url'       => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=parcial&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ),
                'name'      => __( 'Envio parcial', 'woocommerce' ),
                'action'    => $action_slug,
            );
        }
        return $actions;
    }
    
    // Set Here the WooCommerce icon for your action button
    add_action( 'admin_head', 'add_custom_order_status_actions_button_css' );
    function add_custom_order_status_actions_button_css() {
        $action_slug = "parcial"; // The key slug defined for your action button
    
        echo '<style>.wc-action-button-'.$action_slug.'::after { font-family: woocommerce !important; content: "\e029" !important; }</style>';
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。

    经过测试和工作

    【讨论】:

    • @TehseenAhmed 抱歉,它适用于最新版本的 woocommerce,我刚刚重新测试了它……现在这只适用于“处理中”订单状态 (正如您在代码中注意到的那样)...我什至更新了答案中的屏幕截图以向您展示...
    • 抱歉,我使用的是 3.3.3 版本,但这对我不起作用。这些下面的两个钩子工作正常add_filter( 'manage_edit-shop_order_columns', array($this,'custom_shop_order_column'), 20 ); add_action( 'manage_shop_order_posts_custom_column', array($this,'custom_orders_list_column_content'), 20, 2 ); 我使用另一种方法来使用上面的钩子完成这项任务
    • @TehseenAhmed 我使用的是 3.3.5 版......而这个答案代码完美运行......所以可能(如果你喜欢/想要)你可以在 StackOverFlow 上提问将您的实际相关代码添加到问题中,如果您通知我,我会回答。
    • 如果您认为它不起作用,您必须点击右上角的“调整视图”并选中“操作”才能在表格视图中看到它
    猜你喜欢
    • 1970-01-01
    • 2021-04-15
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    相关资源
    最近更新 更多