1

我正在使用 woocommerce 插件...实际上这是我对 php 的怀疑。我正在使用一些自定义字段,所以我应该将这些自定义字段的值获取到我的电子邮件中。

当我使用一个自定义字段“我的字段”时,我正在为我的电子邮件获取价值,但我不了解如何将所有自定义字段的值获取到我的电子邮件..

以下是适用于单个自定义字段的代码:(来自这里https://gist.github.com/3905785:)

/**
 * Add the field to the checkout
 **/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
    woocommerce_form_field( 'my_field_name', array( 
        'type'          => 'checkbox', 
        'class'         => array('my-field-class form-row-wide'), 
        'label'         => __('Fill in this field'), 
        'placeholder'   => __('Enter a number'),
        ), $checkout->get_value( 'my_field_name' ));

    echo '</div>';
}

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
}

/**
 * Add the field to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');

function my_custom_checkout_field_order_meta_keys( $keys ) {
    $keys[] = 'My Field';
    return $keys;
}


我为 2 个自定义字段尝试了下面的代码:(下面的代码没有将任何自定义字段值发送到我的邮件..)请告诉我我在下面的代码中做错了什么:

/**
 * Add the field to the checkout
 **/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
    woocommerce_form_field( 'my_field_name', array( 
        'type'          => 'checkbox', 
        'class'         => array('my-field-class form-row-wide'), 
        'label'         => __('Fill in this field'), 
        'placeholder'   => __('Enter a number'),
        ), $checkout->get_value( 'my_field_name' ));

    echo '</div>';



  echo '<div id="my_custom_checkout_field"><h3>'.__('Keywords').'</h3>';

    woocommerce_form_field( 'keywords', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Fill in this field'),
        'placeholder'       => __('Enter something'),
        ), $checkout->get_value( 'keywords' ));

    echo '</div>';
}

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));

        if ($_POST['keywords']) update_post_meta( $order_id, 'Keywords', esc_attr($_POST['keywords']));
}

/**
 * Add the field to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');

function my_custom_checkout_field_order_meta_keys( $keys ) {
    $keys[] = 'My Field,Keywords';
    return $keys;
}
4

1 回答 1

1

首先是一行:

$keys[] = 'My Field,Keywords';

应改为:

$keys[] = 'My Field';
$keys[] = 'Keywords';

您的代码是使用字符串“My Field,Keywords”创建一个数组条目,而不是使用字符串“My Field”和“Keywords”创建 2 个数组条目。

这绝对是问题的原因之一。

这可能并不重要,但您应该更改第二个 div 标签的 id - 具有相同 id 的多个元素并不好。

进行这些更改,然后重试。如果还有其他问题,我们可以依次解决。

我希望这有帮助。

于 2012-11-29T18:27:29.983 回答