我一直在努力寻找一个钩子,让我可以在 woocommerce 管理“订单页面”的顶部添加一个按钮,但到目前为止没有成功。我找到了将操作按钮添加到操作列以及每个订单页面内的钩子......但不是我现在需要的地方。
如果没有钩子,则采用替代方法。
更具体地说,我附上了我所指的地方的图像
有什么建议吗?
更新高性能订单存储 (HPOS)。
因为这与 Wordpress 有关,而不是特定于 Woocommerce,因为订单只是自定义帖子类型。因此以下代码将在现有字段和按钮之后的顶部区域显示一个自定义按钮:
add_action( 'manage_posts_extra_tablenav', 'admin_order_list_top_bar_button', 20, 1 );
function admin_order_list_top_bar_button( $which ) {
global $typenow;
if ( 'shop_order' === $typenow && 'top' === $which ) {
?>
<div class="alignleft actions custom">
<button type="submit" name="custom_" style="height:32px;" class="button" value=""><?php
echo __( 'Custom', 'woocommerce' ); ?></button>
</div>
<?php
}
}
要在启用高性能订单存储时使其正常工作,请使用:
use Automattic\WooCommerce\Utilities\OrderUtil;
add_action( 'woocommerce_order_list_table_extra_tablenav', 'admin_hpos_wc_order_list_top_bar_button', 20, 2 );
function admin_hpos_wc_order_list_top_bar_button( $post_type, $which ) {
if( 'shop_order' === $post_type && 'top' === $which ) {
?>
<div class="alignright actions custom">
<button type="submit" name="custom_" style="height:32px;" class="button" value=""><?php
echo __( 'Custom', 'woocommerce' ); ?></button>
</div>
<?php
}
}
代码位于子主题的functions.php文件中(或插件文件中)。
在 WooCommerce legacy 订单上(存储在 WordPress Post 表中):
在启用了 HPOS 的 WooCommerce 上(存储在 WooCommerce 自定义表中):