在woocommerce中如何在结帐页面上添加参与者列表并在订单详细信息页面中显示

时间:2015-05-13 10:08:00

标签: wordpress-plugin woocommerce wordpress-theming wordpress

如何在结账时添加参与者列表并在订单详细页面中显示。使用功能和wordpress操作和过滤器

2 个答案:

答案 0 :(得分: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] ) );
    }

}
}

在订单详细信息页面中显示列表。见屏幕简短。 enter image description here

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>';
}

答案 1 :(得分:0)

将参与者列表添加到发送的电子邮件中:(可能对某人有帮助)

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>';
}

}