【问题标题】:custom field values not getting to mail自定义字段值未发送到邮件
【发布时间】:2012-11-17 21:08:57
【问题描述】:

我正在使用 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;
}

【问题讨论】:

    标签: php html woocommerce


    【解决方案1】:

    首先是一行:

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

    应该改为:

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

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

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

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

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

    我希望这会有所帮助。

    【讨论】:

    • 哇,彼得!非常感谢! :) 但有一件事要问:这里如何将“我的字段”和“关键字”视为键?键是h3标签吗?如果不是,如何识别为key?
    • 我不确定你在问什么。您是否在问如何使用特定键识别 h3 标签?如果是这样,那么您可以按标签获取元素以创建页面上所有 h3 标签的数组,然后遍历它们并检查标签的 innerHTML 以查看它是否与键匹配。如果这不是你的意思,那就直说吧。
    • 不不,在这种情况下,“我的字段”和“关键字”如何被视为键?
    • 哦,它们不是数组中的键,它们是名为“$keys”的数组中的值。我明白为什么会令人困惑。
    • 兄弟,你也可以解决这个问题吗:stackoverflow.com/questions/13630375/…
    猜你喜欢
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多