如何添加签出参与者列表并显示在订单详细信息页面中。使用功能, wordpress动作和过滤器
#1
在结帐页面的订单备注字段后添加字段。在function.php中添加以下代码以获取以下屏幕。在这里, 我们显示购物车-1行中的总商品。因为1个参与者是那个命令。
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
global $woocommerce;
$class_basket_count = $woocommerce->cart->cart_contents_count;
$class_basket_count = $class_basket_count - 1;
if($class_basket_count >= 1){
echo '<div id="my_custom_checkout_field"><h3>' . __('Participant List') . '</h3>';
for ($i=1; $i <= $class_basket_count; $i++ ){
woocommerce_form_field( 'guest-name-'.$i, array(
'type' => 'text', 'class' => array('my-field-class form-row-wide'), 'label' => __('Guest Name'), 'placeholder' => __('Enter Name'), ), $checkout->get_value( 'guest-name-'.$i ));
woocommerce_form_field( 'guest-email-'.$i, array(
'type' => 'text', 'class' => array('my-field-class form-row-wide'), 'label' => __('Guest Email'), 'placeholder' => __('Enter Email'), ), $checkout->get_value( 'guest-email-'.$i ));
}
echo '</div>';
}
}
添加和更新参与者列表的元值。在function.php中添加以下代码
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
global $woocommerce;
$class_basket_count = $woocommerce->cart->cart_contents_count;
for ($i=1; $i <= $class_basket_count; $i++ ){
if ( ! empty( $_POST['guest-name-'.$i] ) ) {
update_post_meta( $order_id, 'guest-name-'.$i, sanitize_text_field( $_POST['guest-name-'.$i] ) );
update_post_meta( $order_id, 'guest-email-'.$i, sanitize_text_field( $_POST['guest-email-'.$i] ) );
}
}
}
在订单详细信息页面显示列表。见屏幕简短。
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
$totla_class_ordered = $order->get_item_count();
echo '<h4>Participant Details</h4>';
echo '<div class="participant-details"><table>';
echo '<thead><tr><th>Guest Name</th><th>Guest Email</th></tr></thead>';
for ($i=1; $i <= $totla_class_ordered; $i++ ){
echo '<tr><td>'.get_post_meta( $order->id, 'guest-name-'.$i, true ) . '</td><td>'.get_post_meta( $order->id, 'guest-email-'.$i, true ) . '</td>';
}
echo '</table></div>';
}
#2
在发送的电子邮件中添加参与者列表:(可能对某人有用)
add_action('woocommerce_email_customer_details', 'add_list_to_emails', 15, 4 );
function add_verification_id_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
global $woocommerce;
$total_class_ordered = $order->get_item_count();
if ($total_class_ordered > 1) {
echo '<h3>Details of extra participants</h3>';
echo '<div class="participant-details" style="margin-top: 10px;"><table>';
echo '<thead><tr><th style="text-align: left;">Name</th><th style="text-align: left;">Email</th></tr></thead>';
for ( $i = 1; $i <= $total_class_ordered; $i ++ ) {
echo '<tr><td>' . get_post_meta( $order->get_id(), 'guest-name-' . $i, true ) .
'</td><td>' . get_post_meta( $order->get_id(), 'guest-email-' . $i, true ) .
'</td></tr>';
}
echo '</table></div>';
}
}
评论前必须登录!
注册