php - Woocommerce 3.3+ 中的后端订单列表自定义操作按钮

标签 php wordpress button woocommerce orders

自 WooCommerce 版本 3.3+ 起,下面在管理订单列表中显示自定义操作按钮的代码不再有效。

// Add your custom order action button
add_action( 'woocommerce_admin_order_actions_end', 'add_custom_order_actions_button', 100, 1 );
function add_custom_order_actions_button( $order ) {

    // Get the tracking number
    $traking_number = get_post_meta( $order->get_id(), '_aftership_tracking_number', true );
    if( empty($traking_number) ) return;

    // Prepare the button data
    $url    = esc_url('https://track.aftership.com/'.$traking_number.'?');
    $name   = esc_attr( __('Tracking', 'woocommerce' ) );
    $action = esc_attr( 'view tracking' ); // keep "view" class for a clean button CSS

    // Set the action button
    printf( '<a class="button tips %s" href="%s" data-tip="%s" target="_blank">%s</a>', $action, $url, $name, $name );
}

// The icon of your action button (CSS)
add_action( 'admin_head', 'add_custom_order_actions_button_css' );
function add_custom_order_actions_button_css() {
    echo '<style>.view.tracking::after { font-family: woocommerce; content: "\e005" !important; }</style>';
}

代码来自这个答案:Add custom URL link to admin order list page in WooCommerce

他们做了什么改变以防止它在新版本中工作?
如何让它在 Woocommerce 版本 3.3+ 中运行?

最佳答案

这是实现此功能的正确方法,因为这是代码 from one of my answers , 这将在单独的浏览器窗口(或选项卡)中加载相应的跟踪页面。

hook woocommerce_admin_order_actions_end still exist and works. What has changed in vesion 3.3+ is the function that displays the buttons wc_render_action_buttons() and so the displayed buttons html structure and classes too.
Why? … Because that order list display has been enhanced in version 3.3+.

代码:

// Add your custom order action button
add_action( 'woocommerce_admin_order_actions_end', 'add_custom_order_actions_button', 100, 1 );
function add_custom_order_actions_button( $order ) {

    // Get the tracking number
    $traking_number = get_post_meta( $order->get_id(), '_aftership_tracking_number', true );
    if( empty($traking_number) ) return;

    // Prepare the button data
    $url    = esc_url('https://track.aftership.com/'.$traking_number.'?');
    $name   = esc_attr( __('Tracking', 'woocommerce' ) );
    $class  = esc_attr( 'tracking' );

    // Custom action button (with a target='_blank' opening a new browser window)
    printf( '<a class="button wc-action-button wc-action-button-%s %s" href="%s" title="%s" target="_blank">%s</a>', $class, $class, $url, $name, $name );
}

// The icon of your action button (CSS)
add_action( 'admin_head', 'add_custom_order_actions_button_css' );
function add_custom_order_actions_button_css() {
    echo '<style>.wc-action-button-tracking::after { font-family: woocommerce !important; content: "\e01a" !important; }</style>';
}

代码进入您活跃的子主题(或主题)的 function.php 文件。

测试并仅适用于 woocommerce 3.3+ 版

enter image description here

Here I don't use woocommerce_admin_order_actions usual action hook, but instead I use an unusual hook, to allow displaying the tracking page in a separate browser window (or tab)

关于php - Woocommerce 3.3+ 中的后端订单列表自定义操作按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48959047/

相关文章:

c# - 当文本太长时显示按钮的工具提示

php - 使用 SVN 对 PHP 站点进行版本控制的最佳方法是什么?

php - 在表 1 中选择作者并在表 2 mysql 中计算已出版的书

css - npm run构建错误,没有这样的文件或目录,打开 'build/bundle.css'

android - Android 虚拟设备 (AVD) 管理器中缺少新按钮

javafx如何在运行时将按钮图像复制到另一个?

php - 将大型 Excel 工作表转换为 Mysql?

PHP 下拉菜单中的项目

html - CSS:类中类

php - 为 wordpress 设计主题的最佳方法,需要一些建议