【问题标题】:Backend orders list custom action buttons in Woocommerce 3.3+Woocommerce 3.3+ 中的后端订单列表自定义操作按钮
【发布时间】:2018-08-04 04:05:48
【问题描述】:

由于 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+ 版中工作?

【问题讨论】:

  • 有什么问题?该按钮不显示,或者当您单击它时没有任何反应?
  • 我看了看你还没有定义变量$actions。它是什么意思?
  • 按钮不再显示。他们把按钮从右边移到了中间,但由于某种原因,它坏了。
  • @AndrewSchultz 不是重复的,因为自 WC 3.3 版以来,管理员订单列表已通过一些增强功能进行了更改……操作按钮现在有所不同,并且涉及 Woocommerce 中的全新功能源代码。所以这个问题问题与这些变化直接相关......

标签: php wordpress button woocommerce orders


【解决方案1】:

这是实现此功能的正确方法,因为这是代码 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+ 版

这里我没有使用woocommerce_admin_order_actions 通常的动作钩子,而是使用了一个不寻常的钩子,允许在单独的浏览器窗口中显示跟踪页面(或标签)

【讨论】:

  • 谢谢你的工作。我也意识到他们在最新版本中隐藏了这些动作。我必须选择屏幕选项并启用它。傻我,我是一只鹅。
  • @Disquy 没问题……重要的是它可以再次使用比以前更好的图标
  • 非常感谢 - 如果我不想只生成指向另一个站点的 href,而是执行一些 PHP 代码(可能是 API 调用或更新订单上的某些元数据) ) 当您单击按钮时,该代码属于哪里?
  • @NullHypothesis 谢谢。如果你觉得这很棒,如果你喜欢/想要,你可以点赞。要回答您的问题,是的,例如,可以使用 CURL 执行 API 调用,然后在您获得正确响应时更新您的订单中的某些内容……现在无法在评论中回答,因为您应该需要其他内容。最好的办法是在 StackOverFlow 上提出一个新的相关问题并在此处通知我。最好在您的问题中包含一些自定义代码。
【解决方案2】:

它们一定发生了很大变化,因为您使用的那个钩子不存在。这是您的代码的修改版本。我更改了您将内联 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' );

【讨论】:

  • 这适用于您的 3.3.3 安装吗?不幸的是,这对我不起作用。
  • 嗯,我使用的是 3.3.1 版本,我没有意识到我没有安装最新版本。我去升级看看有什么区别。
  • 我想在按下按钮时触发这个 wp_remote_post API 调用,我怎样才能将这个动作绑定到动作中?
猜你喜欢
  • 2018-01-12
  • 2018-08-26
  • 2021-09-01
  • 2018-04-27
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
  • 2020-01-07
相关资源
最近更新 更多