以下代码根据购物车中虚拟产品的数量注册自定义字段:
function cart_has_virtual_product() {
if ( ! is_a( WC()->cart, 'WC_Cart' ) ) {
return 0;
}
$virtual_product_count = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
if ( $product->is_virtual() ) {
$virtual_product_count += $cart_item['quantity'];
}
}
return $virtual_product_count;
}
add_filter('woocommerce_form_field', 'addHeadingsInBetweenFormFields', 10, 4);
function addHeadingsInBetweenFormFields($field, $key, $args, $value = null) {
if (is_checkout() & $key === 'order_jmeno_ucastnika') {
$a = '<h3>Údaje o účastníkovi kurzu</h3>';
return $a . $field;
}
return $field;
}
function add_custom_checkout_fields( $fields ) {
$virtual_product_count = cart_has_virtual_product();
for ( $i = 1; $i <= $virtual_product_count; $i++ ) {
$fields['order']['order_jmeno_ucastnika' . $i] = array(
'label' => 'Jméno účastníka ' . $i,
'placeholder' => 'Jméno Příjmení',
'required' => true,
'class' => array( 'form-row-wide' ),
'clear' => true
);
$fields['order']['order_email_ucastnika' . $i] = array(
'type' => 'email',
'label' => 'E-mail účastníka ' . $i,
'placeholder' => '',
'required' => true,
'class' => array( 'form-row-wide' ),
'clear' => true
);
$fields['order']['order_title' . $i] = array(
'label' => 'Titul/y' . $i,
'placeholder' => 'Titul/y',
'required' => false,
'class' => array( 'form-row-wide' ),
'clear' => true
);
$fields['order']['order_birthdate' . $i] = array(
'type' => 'date',
'label' => 'Datum narození' . $i,
'placeholder' => 'DD.MM.RRRR',
'required' => true,
'class' => array( 'form-row-wide' ),
'clear' => true
);
}
return $fields;
}
然后这些字段转到管理中的订单详细信息:
function save_custom_checkout_fields( $order_id, $posted_data ) {
$virtual_product_count = cart_has_virtual_product();
for ( $i = 1; $i <= $virtual_product_count; $i++ ) {
if ( isset( $_POST['order_jmeno_ucastnika' . $i] ) ) {
update_post_meta( $order_id, 'order_jmeno_ucastnika' . $i, sanitize_text_field( $_POST['order_jmeno_ucastnika' . $i] ) );
}
if ( isset( $_POST['order_email_ucastnika' . $i] ) ) {
update_post_meta( $order_id, 'order_email_ucastnika' . $i, sanitize_email( $_POST['order_email_ucastnika' . $i] ) );
}
if ( isset( $_POST['order_title' . $i] ) ) {
update_post_meta( $order_id, 'order_title' . $i, sanitize_text_field( $_POST['order_title' . $i] ) );
}
if ( isset( $_POST['order_birthdate' . $i] ) ) {
update_post_meta( $order_id, 'order_birthdate' . $i, sanitize_text_field( $_POST['order_birthdate' . $i] ) );
}
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'save_custom_checkout_fields', 10, 2 );
然后当订单完成时,我们应该向字段中的所有收件人发送电子邮件'order_email_ucastnika。这就是问题所在。当我有收件人设置为帐单电子邮件,它工作正常。但现在电子邮件不发送。
function send_zoom_link_on_order_complete( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
$virtual_product_count = cart_has_virtual_product();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$zoom_link = get_post_meta( $product_id, 'zoom_link', true );
$custom_info = get_post_meta( $product_id, 'custom_info', true );
if ( $zoom_link ) {
for ( $i = 1; $i <= $virtual_product_count; $i++ ) {
$to = get_post_meta( $order_id, 'order_email_ucastnika' . $i, true );
$subject = 'my subject ' . $item->get_name();
$message = 'link: ' . $zoom_link . $custom_info;
// add headers
$headers = array(
'From: mysite.com <orders@mysite.com>',
'Content-Type: text/html; charset=UTF-8'
);
wp_mail( $to, $subject, $message, $headers );
}
}
}
}
add_action('woocommerce_order_status_completed', 'send_zoom_link_on_order_complete');
你觉得会出什么问题吗?
1条答案
按热度按时间7eumitmz1#
好吧,找到问题了。问题出在变量$virtual_product_count = cart_has_virtual_product();从购物车中获取数据而不是从订单中获取数据。
正确的代码如下所示: