1

So,

I have a custom metabox, with multiple fields on it, and the amount of boxes is variable (could be 3, could be a lot more, depending on order quantity number)

I'm trying to write a save function that will loop though all the fields, see which ones need updating/saving, then do the save

SIDE NOTE: The custom metabox is added to a WooCommerce Order

The metabox is displaying fine on the order edit page

Here's what I have so far:

function order_add_delegates_metabox() {
    add_meta_box(
        "order-delegates", // metabox id
        "Ticket Delegates", // metabox title
        "delegates_meta_markup", // metabox markup callback
        "shop_order", // post type
        "normal", // metabox position
        "low", // metabox priority on screen
    );
}
add_action("add_meta_boxes", "order_add_delegates_metabox");

function delegates_meta_markup() {

    global $post;

    wp_nonce_field(basename(__FILE__), "add_delegates_nonce"); ?>

    <table class="wp-list-table widefat fixed striped table-view-list">
        <thead>
            <tr>
                <th>Ticket Type</th>
                <th>Name</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $order = new WC_Order($post->ID);
            $qty = $order->get_item_count();
            for($x = 1; $x <= $qty; $x++) { ?>
            <tr>
                <td>
                    <select id="ticket_type_<?= $x; ?>" name="ticket_type_<?= $x; ?>" style="width: 100%">
                        <option>--</option>
                        <?php foreach($order->get_items() as $item_id => $item) {
                            echo '<option value="'.str_replace(' ', '_', $item->get_name()).'">'.$item->get_name().'</option>';
                        } ?>
                    </select>
                </td>
                <td>
                    <input type="text"
                        id="del_name_<?= $x; ?>"
                        name="del_name_<?= $x; ?>"
                        placeholder="Delegate Name"
                        style="width: 100%"
                        value="" />
                </td>
                <td>
                    <input type="email"
                        id="del_email_<?= $x; ?>"
                        name="del_email_<?= $x; ?>"
                        placeholder="[email protected]"
                        style="width: 100%"
                        value="" />
                </td>
            </tr>
            <?php } ?>
        </tbody>
    </table>

<?php }

function save_custom_meta_box($post_id, $post) {

    // nonce check
    if(!isset($_POST['add_delegates_nonce']) || !wp_verify_nonce($_POST['add_delegates_nonce'], 'agm')) {
        return $post_id;
    }

    // check current user permissions
    $post_type = get_post_type_object($post->post_type);

    if(!current_user_can($post_type->cap->edit_post, $post_id)) {
        return $post_id;
    }

    // do not save the data if autosave
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    // define your own post type here
    if('shop_order' !== $post->post_type) {
        return $post_id;
    }

    // save fields data

    return $post_id;

}
add_action("save_post", "save_custom_meta_box", 10, 3);

Any ideas?

I can't think how I'm going to do it, as each field is generating and will be saved with it's own meta key (ticket_type_1, ticket_type_2 ... del_name_1, del_name_2 ... del_email_1, del_email_2)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.