bounty还有5天到期。此问题的答案有资格获得+50声望奖励。TvCasteren希望引起更多的注意这个问题。
我已经创建了一些自定义结帐字段的基础上是否客户购买1或2项。这些字段称为:“Voorletters”、“Roepnaam”、“Achternaam”和“Geboortedatum”。代码工作,因为数据显示在我们的WordPress后端下面的相应订单,但我们希望包括它在我们的电子邮件以及。理想情况下,我们希望将此包含在客户成功付款后收到的订单完成邮件中,同时也包含在管理员收到新订单后收到的确认邮件中。
基于添加多个自定义结帐字段分组在两列WooCommerce回答代码,这里是我有:
// Utility function: Get specific product total cart item quantity
function get_specific_cart_item_quantity() {
$targeted_id = 5027; // <== Product ID to check for.
$item_qty = 0; // Initializing
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ) {
if ( $item['product_id'] == $targeted_id ) {
$item_qty += $item['quantity'];
}
}
return $item_qty;
}
// Add custom checkout fields
add_action('woocommerce_checkout_before_customer_details', 'add_custom_checkout_fields');
function add_custom_checkout_fields() {
$item_qty = get_specific_cart_item_quantity();
if ( $item_qty ) {
$domain = 'woocommerce';
echo '<div id="custom_checkout_fields">
<h3>' . __('<b>Persoonsgegevens BHV</b>', $domain) . '</h3>
<p>' . __('Vul a.u.b. de persoonsgegevens van de BHV kandidaten in hieronder.', $domain) . '</p>
<div class="custom-fields-container">';
$qty1 = $item_qty % 2 == 0 ? $item_qty / 2 : ( $item_qty == 1 ? 1 : ($item_qty + 1) / 2 );
$qty2 = $item_qty == 1 ? 1 : 2;
// Multi-loop to display custom fields based on quantity in 2 columns
for ( $k = 1; $k <= $qty1; $k++ ) {
$qty2 = $item_qty % 2 != 0 && $k == $qty1 ? 1 : 2;
// 2nd Loop (letters)
foreach ( array('Voorletters', 'Roepnaam', 'Achternaam', 'Geboortedatum') as $letter ) {
$key = strtolower($letter);
// 3rd Loop (Columns)
for ( $h = 1; $h <= $qty2; $h++ ) {
$class = $item_qty > 1 ? (($h % 2 == 0) ? 'last' : 'first' ) : 'wide';
$class = $item_qty % 2 != 0 && $k == $qty1 ? 'wide' : $class;
$index = $item_qty == 1 ? 1 : 2;
$index = in_array($class, ['first', 'wide']) ? ($k*2)-1 : $k*2;
$field = "custom_field_{$key}{$index}";
$label = sprintf('%s %d %s', __('Kandidaat', $domain), $index, $letter);
woocommerce_form_field( $field, array(
'type' => 'text',
'label' => $label,
'placeholder' => '',
'class' => array('custom-field form-row-'.$class),
'required' => true, // or false
), WC()->checkout->get_value( $field ) );
}
}
}
echo '</div></div>';
}
}
// Validate custom fields
add_action( 'woocommerce_after_checkout_validation', 'validate_custom_checkout_fields', 10, 2 );
function validate_custom_checkout_fields( $data, $errors ) {
if ( did_action('woocommerce_checkout_process') >= 2 ) return;
$item_qty = get_specific_cart_item_quantity();
if ( $item_qty ) {
$domain = 'woocommerce';
$break = false;
// 1st Loop (Letters)
foreach ( array('Voorletters', 'Roepnaam', 'Achternaam', 'Geboortedatum') as $letter ) {
$key = strtolower($letter);
// 2nd Loop (Numbers)
for ( $i = 1; $i <= $item_qty; $i++ ) {
$field = "custom_field_{$key}{$i}";
if ( isset($_POST[$field]) && empty($_POST[$field]) ) {
$errors->add( 'validation', __('Vul a.u.b. de verplichte velden in.', $domain), 'error' );
$break = true;
break;
}
}
if ( $break ) break;
}
}
}
// Save custom field data as custom order meta data
add_action( 'woocommerce_checkout_create_order', 'save_custom_checkout_fields', 10, 2 );
function save_custom_checkout_fields( $order, $data ) {
$item_qty = get_specific_cart_item_quantity();
if ( $item_qty ) {
// 1st Loop (Letters)
foreach ( array('Voorletters', 'Roepnaam', 'Achternaam', 'Geboortedatum') as $letter ) {
$key = strtolower($letter);
// 2nd Loop (Numbers)
for ( $i = 1; $i <= $item_qty; $i++ ) {
$field = "custom_field_{$key}{$i}";
if ( isset($_POST[$field]) && ! empty($_POST[$field]) ) {
$order->update_meta_data( $field, sanitize_text_field($_POST[$field]) );
}
}
}
}
}
1条答案
按热度按时间vyu0f0g11#
我已经轻松地重温了你所有的代码。以下将在管理员“新订单”和“客户完成订单”电子邮件通知中显示您的自定义字段数据:
参与者自定义字段数据将显示在2列中,有点像结账。
代码放在子主题的functions.php文件中(或插件中)。测试和作品。