I have created some jQuery to add input text boxes as an array to my product form depending on quantity. This is working fine. The problem I have now is attaching the data to the order as product meta.
I found this post which goes into detail on how to do this with one input box but doesnt really cover how to do it with more than one as an array. I see in the comments where someone asked but the answer is for predefined name attributes and mine are created on the fly depending on how many items they want. Here is a sample of adding more than one but its not an array.
Here is the fiddle with the html form/jQuery demonstrating what it does and how the inputs are added. So how do I get this info to attach to the order items?
Here is how I am trying to attach it based on the above post:
//Debugging
function add_name_on_tshirt_field() {
echo '<pre>';
foreach($_REQUEST['student-name'] as $student_key => $value) {
print_r(WC()->session->get( $cart_item_key.'_'.$student_key ));
}
//print_r($_REQUEST['student-name']);
echo '</pre>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_name_on_tshirt_field' );
//Make sure names are filled out
function student_name_validation() {
if ( empty( $_REQUEST['student-name'] ) ) {
wc_add_notice( __( 'Please enter a Student Name…', 'woocommerce' ), 'error' );
return false;
}
return true;
}
add_action( 'woocommerce_add_to_cart_validation', 'student_name_validation', 10, 3 );
//Save session data
function save_student_name_field( $cart_item_key, $product_id = null, $quantity= null, $variation_id= null, $variation= null ) {
if( isset( $_REQUEST['student-name'] ) ) {
$keys = array();
foreach($_REQUEST['student-name'] as $student_key => $value) {
$keys[] = $student_key;
WC()->session->set( $cart_item_key.'_'.$student_key, $value);
}
}
}
add_action( 'woocommerce_add_to_cart', 'save_student_name_field', 1, 5 );
//Cart page display
function render_meta_on_cart_item( $title = null, $cart_item = null, $cart_item_key = null ) {
//if( $cart_item_key && WC()->session->__isset( $cart_item_key.'_student_name' ) ) {
echo $title. '<dl class="">';
echo '<dt class="">Student(s): </dt>';
foreach($_REQUEST['student-name'] as $student_key => $value) {
echo '<dd class=""><p>'. WC()->session->get( $cart_item_key.'_'.$student_key ) .'</p></dd>';
}
echo '</dl>';
//}else {
// echo $title;
//}
}
add_filter( 'woocommerce_cart_item_name', 'render_meta_on_cart_item', 1, 3 );
//Checkout page display
function custom_meta_on_checkout_order_review_item( $quantity = null, $cart_item = null, $cart_item_key = null ) {
//if( $cart_item_key && WC()->session->__isset( $cart_item_key.'_student_name' ) ) {
echo $quantity. '<dl class="">';
echo '<dt class="">Student(s): </dt>';
foreach($_REQUEST['student-name'] as $student_key => $value) {
echo '<dd class=""><p>'. WC()->session->get( $cart_item_key.'_'.$student_key ) .'</p></dd>';
}
echo '</dl>';
//}
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_meta_on_checkout_order_review_item', 1, 3 );
//Add order item meta
function add_meta_on_checkout_order_review_item( $quantity = null, $cart_item = null, $cart_item_key = null ) {
//if( $cart_item_key && WC()->session->__isset( $cart_item_key.'_student_name' ) ) {
echo $quantity. '<dl class="">';
echo '<dt class="">Student(s): </dt>';
foreach($_REQUEST['student-name'] as $student_key => $value) {
echo '<dd class=""><p>'. WC()->session->get( $cart_item_key.'_'.$student_key ) .'</p></dd>';
}
echo '</dl>';
//}
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'add_meta_on_checkout_order_review_item', 1, 3 );