后端订单列出了Woocommerce 3.3+中的自定义操作按钮

时间:2018-02-24 03:24:17

标签: 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 +中使其工作?

2 个答案:

答案 0 :(得分:0)

它们必定已经发生了很大变化,因为你使用过的钩子并不存在。这是您的代码的修改版本。我改变了你将内联CSS排入最佳实践的方式。

// Add your custom order action button
add_filter( 'woocommerce_admin_order_actions', 'add_custom_order_actions_button', 10, 2 );

function add_custom_order_actions_button( $actions, $order ) {

    // Get the tracking number
    $tracking_number = get_post_meta( $order->get_id(), '_aftership_tracking_number', true );

    if( empty( $tracking_number ) ) 
        return $actions;

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

    $actions['view-tracking'] = array( 'url' => $url, 'name' => $name, 'action' => $action );

    return $actions;
}

//Adding CSS inline style to an existing CSS stylesheet
function add_inline_css() {
    //All the user input CSS settings as set in the plugin settings
    $custom_css = '.view.tracking::after { font-family: woocommerce; content: "\e005" !important; }';

    //Add the above custom CSS via wp_add_inline_style
    wp_add_inline_style( 'woocommerce_admin_styles', $custom_css );
}
add_action( 'wp_enqueue_scripts', 'add_inline_css' );

答案 1 :(得分:0)

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

  

woocommerce_admin_order_actions_end still exist and works。 vesion 3.3+中的变化是显示按钮wc_render_action_buttons()的功能,因此显示的按钮也是html结构和类。
  为什么? ...因为版本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

  

此处我不使用woocommerce_admin_order_actions常用操作挂钩,而是使用异常挂钩,允许在单独的浏览器窗口中显示跟踪页(或标签)