我已经组建了一个WooCommerce商店,该商店使用可变产品根据地点和日期销售活动门票。
当用户将一张票添加到他们的购物篮并检查出我试图获得一系列自定义字段时,根据他们购买的票数来显示。例如,我需要捕获的字段是:
将为参加活动的每个人(即出售的门票数量)捕获这些信息。它还需要能够为正在参加的每个事件捕获此信息。
我在GitHub上偶然发现了一段代码 - https://gist.github.com/joslex/43a5c90af83f9024fd80a71495eac4fc - 似乎有了答案 - 唯一的问题是这部分代码似乎把它绊倒了:
$items = $woocommerce->cart->get_cart();
我已经读过它与在 wp_loaded 之后调用 cart-> get_cart(); 有关但是如果我想在 woocommerce_before_order_notes 我不确定如何避免调用 cart-> get_cart();
以下是GitHub的完整代码:
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$i = 1;
foreach($items as $item => $values) {
$_product = $values['data']->post;
$quantity = $values['quantity'];
$x = 1;
while ($x <= $quantity) {
echo '<div class="camper-group"><h3>' . $_product->post_title . __(' - Camper ' . $x . ' Information') .'</h3>';
woocommerce_form_field( 'camper_name_' . $x, array(
'type' => 'text',
'class' => array('camper form-row-wide'),
'label' => __('Camper ' . $x . ' Name'),
'placeholder' => __(''),
), $checkout->get_value( 'camper_name_' . $x ));
woocommerce_form_field( 'camper_grade_' . $x, array(
'type' => 'select',
'class' => array('camper form-row-wide'),
'label' => __('Camper ' . $x . ' Grade'),
'placeholder' => __(''),
'options' => array(
'blank' => __( 'Select a grade', 'wps' ),
'1st' => __( '1st', 'wps' ),
'2nd' => __( '2nd', 'wps' ),
'3rd' => __( '3rd', 'wps' ),
'4th' => __( '4th', 'wps' ),
'5th' => __( '5th', 'wps' ),
'6th' => __( '6th', 'wps' ),
'7th' => __( '7th', 'wps' ),
'8th' => __( '8th', 'wps' ),
'9th' => __( '9th', 'wps' ),
'10th' => __( '10th', 'wps' )
)
), $checkout->get_value( 'camper_grade_' . $x ));
woocommerce_form_field( 'camper_school_' .$x, array(
'type' => 'text',
'class' => array('camper form-row-wide'),
'label' => __('Camper ' . $x . ' School Name'),
'placeholder' => __('The school your child will attend next fall'),
), $checkout->get_value( 'camper_school_' . $x ));
echo '</div>';
$x++;
}
$i++;
}