首页 > 解决方案 > 在 Woocommerce 3.3+ 中的 Preview Lightbox 上管理订单列表的附加操作按钮

问题描述

在 Woocommerce Admin 订单列表中,单击“眼睛”图标时在此处输入图像描述,会在 Lightbox 中打开订单预览。在该灯箱(预览)的底部,有一些允许更改订单状态的操作按钮。

我还有 5 个自定义订单状态,我也想添加为操作按钮,但我不知道我需要使用哪个钩子。

有谁知道如何在该区域添加更多按钮?

任何帮助或建议表示赞赏。

标签: phpwordpresswoocommercehook-woocommerceorders

解决方案


完成此操作的正确钩子是woocommerce_admin_order_preview_actions过滤器钩子。

您需要在下面的函数中的多维数组中定义您的自定义订单状态数据,如下所示,以获得每个操作按钮:

  • status slug(不以“wc-”开头)作为键
  • 状态标签名称
  • 允许的状态数组 slugs(显示当前状态操作按钮)

示例代码(此处为 2 个自定义假状态“自定义一”和“自定义二”)

add_filter( 'woocommerce_admin_order_preview_actions', 'additional_admin_order_preview_buttons_actions', 25, 2 );
function additional_admin_order_preview_buttons_actions( $actions, $order ){
    // Below set your custom order statuses (key / label / allowed statuses) that needs a button
    $custom_statuses = array(
        'custom_one' => array( // The key (slug without "wc-")
            'label'     => __("Custom One", "woocommerce"), // Label name
            'allowed'   => array( 'pending', 'on-hold', 'processing', 'custom_two' ), // Button displayed for this statuses (slugs without "wc-")
        ),
        'custom_two' => array( // The key (slug without "wc-")
            'label'     => __("Custom Two", "woocommerce"), // Label name
            'allowed'   => array( 'pending', 'on-hold', 'processing', 'custom_one' ), // Button displayed for this statuses (slugs without "wc-")
        ),
    );

    // Loop through your custom orders Statuses
    foreach ( $custom_statuses as $status_slug => $values ){
        if ( $order->has_status( $values['allowed'] ) ) {
            $actions['status']['actions'][$status_slug] = array(
                'url'    => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status='.$status_slug.'&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ),
                'name'   => $values['label'],
                'title'  => __( 'Change order status to', 'woocommerce' ) . ' ' . strtolower($values['label']),
                'action' => $status_slug,
            );
        }
    }
    return $actions;
}

代码在您的活动子主题(活动主题)的 function.php 文件中。测试和工作。

在此处输入图像描述


推荐阅读