自定义操作按钮进入WooCommerce管理员订单列表中的自定义列



首先,我做了一个自定义列。

function add_example_column($columns) {
$columns['EXAMPLE'] = 'EXAMPLE';
return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'add_example_column' );

之后,我做了一个新的动作。

function example_action($actions) {
$actions['example'] = array (
'url'     => 'https://example.com?action=ups',
'name'    => __( 'Some text', 'woocommerce' ),
'action'  => 'example'
);
return $actions;
}
add_action( 'woocommerce_admin_order_actions', 'example_action', 10, 1 );

然后,我尝试将此操作按钮放入自定义列。

function example_barcode($column, $order_id) {
$order = new WC_Order( $order_id );
if ( $column == 'EXAMPLE') :
if ( $order->has_status( array( 'processing' ) ) ) :
echo '<style>.wc-action-button-ups::after { font-family: FontAwesome !important; content: "f7e0" !important; }</style>';
endif;
endif;
}
add_action( 'manage_shop_order_posts_custom_column', 'example_barcode', 10, 2 );

还是没有成功。

你的代码中有一些错误。要将自定义操作按钮添加到管理订单列表上的自定义附加列中,请使用以下命令:

// Adding a custom comumn
add_filter( 'manage_edit-shop_order_columns', 'add_example_column' );
function add_example_column($columns) {
$columns['ups'] = __('UPS', 'woocommerce');
return $columns;
}
// The column content by row
add_action( 'manage_shop_order_posts_custom_column' , 'add_example_column_contents', 10, 2 );
function add_example_column_contents( $column, $post_id ) {
if ( 'ups' === $column )
{
$order = wc_get_order( $post_id ); // Get the WC_Order instance Object
// Targetting processing orders only
if ( $order->has_status( 'processing' ) )
{
$slug = 'ups';
$url  = '?action=ups&order_id=' . $post_id; // The order Id is required in the URL
// Output the button
echo '<p><a class="button wc-action-button wc-action-button'.$slug.' '.$slug.'" href="'.$url.'" aria-label="'.$slug.'"> </a></p>';
}
}
}
// The CSS styling
add_action( 'admin_head', 'add_custom_action_button_css' );
function add_custom_action_button_css() {
$action_slug = "ups";
echo '<style>.wc-action-button-'.$action_slug.'::after { font-family: woocommerce !important; content: "e029" !important; }</style>';
}

但是您不会像默认的WooCommerce操作列那样获得工具提示功能。

您必须通过一些附加函数处理自定义操作。

注意:钩子woocommerce_admin_order_actions将与默认的自定义 woocommerce 按钮操作一起使用,因此不适用于自定义列。此外,FontAwesome似乎不适用于后端。

代码进入活动子主题(或活动主题(的函数.php文件。经过测试并工作。

最新更新