WooCommerce Orders metabox:运行 php 自定义提交操作代码
WooCommerce Orders metabox: Run php code on custom submit action
在 Woocommerce 中,我已经能够在自定义 metabox 的订单编辑页面上添加自定义提交按钮。
这是我的代码(添加到function.php
Wordpress 主题中):
add_action( 'add_meta_boxes', 'MY_order_meta_boxes' );
function MY_order_meta_boxes() {
add_meta_box(
'woocommerce-order-verifyemail',
__( 'Trusted List' ),
'order_meta_box_content',
'shop_order',
'side',
'default'
);
}
function order_meta_box_content( $order_id ) {
global $woocommerce, $table_prefix, $wpdb;
$order = new WC_Order( $order_id );
$customeremail = $order->get_billing_email();
?>
<form method="post" action="CURRENT_FILE_URL">
<input type="submit" name="submit" value="submit"/>
</form>
<?php
if(isset($submit)) {$order->add_order_note(sprintf("test2"));}
?>
<?php
return $order_id;
}
但是我不知道为什么,点击按钮(提交)时代码没有运行。
如何运行一些自定义代码,当提交按钮被点击这个自定义 metabox 时?
...我认为你应该使用 $_POST['submit'] 而不是 $submit
要使这项工作如您所愿,您还需要一些东西。我还删除了不必要的代码和一些错误。另外 <imput>
"submit"
ID 过于通用,可能会出现意外错误。
您将能够在挂接到 save_post
操作挂钩的自定义函数中执行任何操作(或保存):
// Add a custom metabox
add_action( 'add_meta_boxes', 'trusted_list_order_meta_boxes' );
function trusted_list_order_meta_boxes() {
add_meta_box(
'woocommerce-order-verifyemail',
__( 'Trusted List' ),
'trusted_list_order_meta_box_content',
'shop_order',
'side',
'default'
);
}
// Custom metabox content
function trusted_list_order_meta_box_content( $post ){
$customeremail = get_post_meta( $post->ID, '_billing_email', true);
$button_text = __( 'Add Note action', 'woocommerce' );
echo '<form method="post" action="CURRENT_FILE_URL">
<input type="submit" name="submit_trusted_list" value="' . $button_text . '"/>
<input type="hidden" name="trusted_list_nonce" value="' . wp_create_nonce() . '">
</form>';
}
// Saving or doing an action when submitting
add_action( 'save_post', 'trusted_list_save_meta_box_data' );
function trusted_list_save_meta_box_data( $post_id ){
// Only for shop order
if ( 'shop_order' != $_POST[ 'post_type' ] )
return $post_id;
// Check if our nonce is set (and our cutom field)
if ( ! isset( $_POST[ 'trusted_list_nonce' ] ) && isset( $_POST['submit_trusted_list'] ) )
return $post_id;
$nonce = $_POST[ 'trusted_list_nonce' ];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) )
return $post_id;
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
if ( ! current_user_can( 'edit_shop_order', $post_id ) && ! current_user_can( 'edit_shop_orders', $post_id ) )
return $post_id;
// Action to make or (saving data)
if( isset( $_POST['submit_trusted_list'] ) ) {
$order = wc_get_order( $post_id );
// $customeremail = $order->get_billing_email();
$order->add_order_note(sprintf("test2"));
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
代码已在 Woocommerce 3+ 上测试并有效。您将获得:
In your custom Metabox content function you will not be able to get any data using $_POST
submitted data… So for example $_POST['submit']
will be always empty.
在 Woocommerce 中,我已经能够在自定义 metabox 的订单编辑页面上添加自定义提交按钮。
这是我的代码(添加到function.php
Wordpress 主题中):
add_action( 'add_meta_boxes', 'MY_order_meta_boxes' );
function MY_order_meta_boxes() {
add_meta_box(
'woocommerce-order-verifyemail',
__( 'Trusted List' ),
'order_meta_box_content',
'shop_order',
'side',
'default'
);
}
function order_meta_box_content( $order_id ) {
global $woocommerce, $table_prefix, $wpdb;
$order = new WC_Order( $order_id );
$customeremail = $order->get_billing_email();
?>
<form method="post" action="CURRENT_FILE_URL">
<input type="submit" name="submit" value="submit"/>
</form>
<?php
if(isset($submit)) {$order->add_order_note(sprintf("test2"));}
?>
<?php
return $order_id;
}
但是我不知道为什么,点击按钮(提交)时代码没有运行。
如何运行一些自定义代码,当提交按钮被点击这个自定义 metabox 时?
...我认为你应该使用 $_POST['submit'] 而不是 $submit
要使这项工作如您所愿,您还需要一些东西。我还删除了不必要的代码和一些错误。另外 <imput>
"submit"
ID 过于通用,可能会出现意外错误。
您将能够在挂接到 save_post
操作挂钩的自定义函数中执行任何操作(或保存):
// Add a custom metabox
add_action( 'add_meta_boxes', 'trusted_list_order_meta_boxes' );
function trusted_list_order_meta_boxes() {
add_meta_box(
'woocommerce-order-verifyemail',
__( 'Trusted List' ),
'trusted_list_order_meta_box_content',
'shop_order',
'side',
'default'
);
}
// Custom metabox content
function trusted_list_order_meta_box_content( $post ){
$customeremail = get_post_meta( $post->ID, '_billing_email', true);
$button_text = __( 'Add Note action', 'woocommerce' );
echo '<form method="post" action="CURRENT_FILE_URL">
<input type="submit" name="submit_trusted_list" value="' . $button_text . '"/>
<input type="hidden" name="trusted_list_nonce" value="' . wp_create_nonce() . '">
</form>';
}
// Saving or doing an action when submitting
add_action( 'save_post', 'trusted_list_save_meta_box_data' );
function trusted_list_save_meta_box_data( $post_id ){
// Only for shop order
if ( 'shop_order' != $_POST[ 'post_type' ] )
return $post_id;
// Check if our nonce is set (and our cutom field)
if ( ! isset( $_POST[ 'trusted_list_nonce' ] ) && isset( $_POST['submit_trusted_list'] ) )
return $post_id;
$nonce = $_POST[ 'trusted_list_nonce' ];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) )
return $post_id;
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
if ( ! current_user_can( 'edit_shop_order', $post_id ) && ! current_user_can( 'edit_shop_orders', $post_id ) )
return $post_id;
// Action to make or (saving data)
if( isset( $_POST['submit_trusted_list'] ) ) {
$order = wc_get_order( $post_id );
// $customeremail = $order->get_billing_email();
$order->add_order_note(sprintf("test2"));
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
代码已在 Woocommerce 3+ 上测试并有效。您将获得:
In your custom Metabox content function you will not be able to get any data using
$_POST
submitted data… So for example$_POST['submit']
will be always empty.