skip to Main Content

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


  1. 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.

    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'
        );
    });
    
    
    function add_tracking_field_content($order) {
        echo '<input type="text" style="width:100%" name="ttn_number" placeholder="' . __('Tracking number', 'woodmart-child') . '" value="' . esc_attr($order->get_meta('_ttn_number')) . '">';
        echo '<input type="hidden" name="ttn_nonce" value="' . esc_attr(wp_create_nonce('ttn_nonce')) . '">';
    }
    
    add_action('woocommerce_process_shop_order_meta', 'save_tracking_number');
    function save_tracking_number($order_id) {
        if (!isset($_POST['ttn_nonce']) || !wp_verify_nonce($_POST['ttn_nonce'], 'ttn_nonce')) {
            return;
        }
    
        $ttn_number = sanitize_text_field($_POST['ttn_number']);
        update_post_meta($order_id, '_ttn_number', $ttn_number);
    
        if (!empty($ttn_number)) {
            $order = wc_get_order($order_id);
            $order->update_status('wc-arrival-shipment', '', true);
        }
    }
    
    Login or Signup to reply.
  2. To fix the 405 error when saving data in a custom WooCommerce meta box:

    1. Check HTTP Method: Ensure you’re using the POST method in your form or request.
    2. Verify Server Configuration: Make sure your server allows the POST method. This might involve checking .htaccess or Nginx configurations.
    3. Review Hook Usage: Confirm that woocommerce_update_order is the correct hook and that nonce validation and data sanitization are properly implemented.
    4. Check HPOS Compatibility: Ensure High-Performance Order Storage (HPOS) doesn’t interfere with saving the meta box data.
    5. Enable Debugging: Turn on WordPress debugging to identify any specific issues in the wp-content/debug.log file.
    6. Permissions Check: Make sure the user role has the necessary permissions to edit orders
    7. Test in a Clean Environment: Disable other plugins and use a default theme to rule out conflicts.

    This should help identify and resolve the issue with the 405 error.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search