Я использовал этот код для создания повторяющихся наборов сообщений настраиваемые поля в зависимости от количества товара в корзине.
Код: Выделить всё
add_action( 'woocommerce_before_order_notes', 'persons_details' );
function persons_details( $checkout ) {
$count = WC()->cart->get_cart_contents_count();
$i = 0;
for( $k=1; $k 'text',
'class' => array('my-field-class form-row-first'),
'label' => __("Jméno a Příjmení"),
'placeholder' => __(""),
'required' => true,
), $checkout->get_value( 'cstm_full_name' . $i ));
woocommerce_form_field( 'cstm_email' . $i, array(
'type' => 'email',
'class' => array( 'my-field-class form-row-last' ),
'label' => __( "Email" ),
'placeholder' => __(""),
'required' => true,
), $checkout->get_value( 'cstm_email' . $i ));
woocommerce_form_field( 'cstm_phone' . $i, array(
'type' => 'number',
'class' => array('my-field-class form-row-first'),
'label' => __("Telefon"),
'placeholder' => __(""),
'required' => true,
), $checkout->get_value( 'cstm_phone' . $i ));
woocommerce_form_field( 'cstm_bdate' . $i, array(
'type' => 'date',
'class' => array('my-field-class form-row-last'),
'label' => __("Datum narození"),
'placeholder' => __(""),
'required' => true,
), $checkout->get_value( 'cstm_bdate' . $i ));
echo '
';
}
}
add_action( 'woocommerce_checkout_create_order', 'save_custom_checkout_field_order_meta' );
function save_custom_checkout_field_order_meta( $order )
{
$count = WC()->cart->get_cart_contents_count();
$order->update_meta_data( 'cstm_items_count', intval($count) ); // Save the cart contents count as meta data
$i = 0;
for($k=1; $kupdate_meta_data( 'cstm_full_name'.$i, sanitize_text_field($_POST['cstm_full_name'.$i]) );
}
if ( isset($_POST['cstm_email'.$i]) && ! empty($_POST['cstm_email'.$i]) ) {
$order->update_meta_data( 'cstm_email'.$i, sanitize_text_field($_POST['cstm_email'.$i]) );
}
if ( isset($_POST['cstm_phone'.$i]) && ! empty($_POST['cstm_phone'.$i])) {
$order->update_meta_data( 'cstm_phone'.$i, sanitize_text_field($_POST['cstm_phone'.$i]) );
}
if ( isset($_POST['cstm_bdate'.$i]) && ! empty($_POST['cstm_bdate'.$i])) {
$order->update_meta_data( 'cstm_bdate'.$i, sanitize_text_field($_POST['cstm_bdate'.$i]) );
}
}
}
add_action( 'woocommerce_email_order_meta', 'add_email_custom_order_meta', 10, 3 );
function add_email_custom_order_meta( $order, $sent_to_admin, $plain_text ){
$quantity = $order->get_meta('cstm_items_count'); // Get items quantity count from meta data
echo '[list]';
$i = 0;
for( $k=1; $k get_meta('cstm_full_name'.$i) . '
[*]' . __("Email: ") . $order->get_meta('cstm_email'.$i) . '
[*]' . __("Telefon: ") . $order->get_meta('cstm_phone'.$i) . '
[*]' . __("Datum narození: ") . $order->get_meta('cstm_bdate'.$i) . '';
}
echo '[/list]';
}
add_action( 'woocommerce_admin_order_data_after_order_details', 'display_custom_fields_in_admin_order_pages' );
function display_custom_fields_in_admin_order_pages( $order ){
$quantity = $order->get_meta('cstm_items_count'); // Get items quantity count from meta data
echo '
[h4]' . __( 'Your label' ) . '[/h4]
[list]';
$i = 0;
for( $k=1; $k get_meta('cstm_full_name'.$i) . '
[*]' . __("Email: ") . $order->get_meta('cstm_email'.$i) . '
[*]' . __("Telefon: ") . $order->get_meta('cstm_phone'.$i) . '
[*]' . __("Datum narození: ") .$order->get_meta('cstm_bdate'.$i) . '';
}
echo '[/list]
';
}
add_action( 'woocommerce_after_checkout_validation', 'custom_checkout_fields_validation', 20, 2 );
function custom_checkout_fields_validation( $data, $errors ){
$count = WC()->cart->get_cart_contents_count();
$i = 0;
for($k=1; $kadd( 'cstm_full_name'.$i, __( "Prosím vyplňte povinné pole u Účastníka $i \"Jméno a Příjmení\"" ), 'error' );
}
if ( isset($_POST['cstm_email'.$i]) && empty($_POST['cstm_email'.$i]) ) {
$errors->add( 'cstm_email'.$i, __( "Prosím vyplňte povinné pole u Účastníka $i \"Email\"" ), 'error' );
}
if ( isset($_POST['cstm_phone'.$i]) && empty($_POST['cstm_phone'.$i])) {
$errors->add( 'cstm_phone'.$i, __( "Prosím vyplňte povinné pole u Účastníka $i \"Telefon\"" ), 'error' );
}
if ( isset($_POST['cstm_bdate'.$i]) && empty($_POST['cstm_bdate'.$i])) {
$errors->add( 'cstm_bdate'.$i, __( "Prosím vyplňte povinné pole u Účastníka $i \"Datum narození\"" ), 'error' );
}
}
}
Одним из этих полей является адрес электронной почты участника, в котором собраны адреса электронной почты всех студентов курса. Их я хотел бы включить в поле «Копия» для уведомлений по электронной почте о статусе заказа «Обработка» и «Выполнено».
Я пытался изменить этот код:
Код: Выделить всё
add_filter( 'woocommerce_email_headers', 'student_email_notification', 20, 3 );
function student_email_notification( $header, $email_id, $order ) {
// Only for 'wc_course_order' notification
if( 'wc_course_order' != $email_id ) return $header;
$student_emails = array();
$enroll_num = 0;
// Loop though Order IDs
foreach( $order->get_items() as $item_id => $item_data ){
$course_qty = $item_data->get_quantity();
$q = 1;
while ( $q 0 ){
// Remove duplicates (if there is any)
$student_emails = array_unique($student_emails);
// Add the emails to existing recipients
$header .= 'Bcc: ' . implode(',', $student_emails) . "\r\n";
}
return $header;
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... ail-notifi