我创建了一些jQuery,根据数量将输入文本框作为数组添加到我的产品表单中。这工作正常。我现在遇到的问题是将数据附加到订单作为产品元。
我发现this post详细介绍了如何使用一个输入框执行此操作,但并未真正介绍如何使用多个数组作为数组。我在评论中看到有人问,但答案是针对预定义的名称属性,我的动态是根据他们想要的项目创建的。 这是a sample of adding more than one,但它不是数组。
Here is the fiddle使用html表单/ jQuery演示它的作用以及如何添加输入。那么如何将此信息附加到订单商品上呢?
以下是基于以上帖子我试图附加它的方法:
//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 );