I add a custom meta box on order edit page and trying to save data but got 405 error (the data saved successfully and code seems to be work):
// Adding Meta container admin shop_order pages
add_action( 'add_meta_boxes', function() {
add_meta_box(
'tracking_field',
__('Tracking number','woodmart-child'),
'add_tracking_field_content',
wc_get_page_screen_id('shop-order'),
'side',
'core'
);
});
// Adding Meta field in the meta container on admin shop_order pages
function add_tracking_field_content( $order ) {
echo '<input type="text" style="width:100%" name="ttn_number" placeholder="'. __('Tracking number', 'woodmart-child') .'" value="'. $order->get_transaction_id() .'">';
echo '<input type="hidden" name="ttn_nonce" value="'. wp_create_nonce() .'">';
}
add_action( 'woocommerce_update_order', 'save_tracking_number', 10, 2 );
function save_tracking_number($order_id, $order){
if ( ! isset($_REQUEST['ttn_nonce']) && ! wp_verify_nonce($_REQUEST['ttn_nonce']) ) {
return $order_id;
}
$ttn_number = sanitize_text_field( $_POST['ttn_number'] );
$order->set_transaction_id($ttn_number);
if(!empty($ttn_number)) {
$order->update_status('wc-arrival-shipment', '', true);
}
$order->save();
}
I use WooCommerce with HPOS activated function, so the default hook save_post_shop_order
not working for me.
I found woocommerce_update_order
hook, but I think it was a bad idea or it just not allowed to use $order->set_transaction_id()
.
2
Answers
Setting custom transaction ID might not be the best option. You can add a custom meta box in the order. Also changed the nonce handling a bit and added the action.
To fix the 405 error when saving data in a custom WooCommerce meta box:
wp-content/debug
.log file.This should help identify and resolve the issue with the 405 error.