I'm working on a custom plugin designed to take data from several checkboxes in an admin meta-box and add that information to an order item as meta-data.
Because the form consists entirely of dynamically inputs (all checkboxes), I am sending a multidimensional array through $_POST. The code below processes and attempts to add metadata to a pre-determined (in the form) order_item.
The strange problem I'm having is that, through the function does work if only one checkbox is ticked, my function doesn't even seem to fire if there is more than one. I suspect this because I use Chrome PHP debug and my PC::debug($) function that logs php to the javascript console only registers when one checkbox is ticked. Is there something about the save_post hook that doesn't like multidimensional arrays?
add_action('save_post', 'curation_robot_submit_callback');
function curation_robot_submit_callback($post_id) {
if (isset($_POST['robot_array'])) {
//Retrieve simple $_POST data
$order_item_qty = $_POST['qty'];
$no_of_products = $_POST['no_of_products'];
$passed_order_item = $_POST['order_item'];
$s_robot_array = array();
// Retrieve array $_POST data. This data is serialized, then base64_encoded.
$i = 0;
foreach($_POST['robot_array'] as $s_robot_array_item) {
$i++;
$s_robot_array[$i] = $s_robot_array_item;
}
// Walk through $s_robot_array and decode/unserialize the data.
// Also set up a $count variable. I use this later.
$robot_array = array();
$count = 0;
foreach($s_robot_array as $item_key => $item) {
$robot_array[$item_key] = unserialize(base64_decode($item));
$count++;
}
//Here, I add the meta-data to the order item.
global $woocommerce, $wpdb;
$order = new WC_Order($post_id);
$order_items = $order - > get_items();
foreach($order_items as $item_id => $order_item) {
if ($item_id == $passed_order_item && $count == $no_of_products) {
foreach($robot_array as $index_value) {
woocommerce_add_order_item_meta($passed_order_item, $index_value['product']['name'], $index_value['product']['sku']);
}
}
}
}
}
Here's an example of what the $_POST form data looks like:
qty:1
no_of_products:3
order_item:116
robot_array[1]:YToxOntzOjc6InByb2R1Y3QiO2E6Mzp7czo0OiJuYW1lIjtzOjExOiJNb250ZW1hcmFubyI7czozOiJza3UiO3M6MzoiMDEzIjtzOjI6ImlkIjtpOjI3NDQ7fX0=
robot_array[2]:YToxOntzOjc6InByb2R1Y3QiO2E6Mzp7czo0OiJuYW1lIjtzOjY6IkdvbmRhciI7czozOiJza3UiO3M6MzoiMDA2IjtzOjI6ImlkIjtpOjI3MTc7fX0=
robot_array[3]:YToxOntzOjc6InByb2R1Y3QiO2E6Mzp7czo0OiJuYW1lIjtzOjU6IkRoYWthIjtzOjM6InNrdSI7czozOiIwMDQiO3M6MjoiaWQiO2k6MjcxNDt9fQ==
My form doesn't have an action either. Should I change this?
Thanks for your time and attention!
Aucun commentaire:
Enregistrer un commentaire