How to update order item meta data from everywhere?

Viewed 9004

I am developing a cycle renting site in which user select the date range (in days) from start date to end. And I am storing that data using wc_add_order_item_meta() function as a meta data of the order, right.

Then, I wanted to give user a functionality like he could change or extend the days by going through the orders page. This is also done, I have created a modal and added a ajax request on change of dates on modal.

Now, I sent data to functions.php using AJAX and I used wc_update_order_item_meta() to update the meta data.

Here's my function looks like :

add_action( 'wp_ajax_update_date_picker', 'update_date_picker' );

function update_date_picker() {
    $p_da = $_POST['p_date'];
    $r_da = $_POST['r_date'];
    $t_dy = $_POST['t_days'];
    $order_id = $_POST['order_id'];

    $order = wc_get_order($order_id);

    foreach ($order->get_items() as $item_id => $item_obj) {

        $pd = wc_update_order_item_meta($item_id, 'pickup_hidden_datetime', $p_da);
        $rd = wc_update_order_item_meta($item_id, 'return_hidden_datetime', $r_da);
        $td = wc_update_order_item_meta($item_id, 'return_hidden_days', $t_dy);

        if($pd&&$rd&&$td){
            echo "Nice work, Bilal";
        }
        die();
    }

}

This is updating too but not at all the places. I mean, it's giving me right response, when I refresh, it's also giving the updated data but when I go through the view item page it's showing there the old data only instead of updated one. Even when I check the orders in the admin, it's showing the old meta data there too.

Any idea would be appreciated, Thanks in Advance.

1 Answers
Related