如何在WooCommerce电子邮件订单中显示自定义域?

时间:2016-04-22 作者:Daniel Morton

在Woocommerce的订单电子邮件表单中,我无法从select下拉列表中显示自定义字段数据。数据显示在管理页面顺序中,而不是电子邮件?

    add_action( \'woocommerce_after_order_notes\', \'my_custom_checkout_field\' );
function my_custom_checkout_field( $checkout ) {

echo \'<div class="clear"></div><br/>\';
echo \'<div id="my_custom_checkout_field"><h3 style="margin:0 0 0.3em;">Payment Options</h3>\';
echo \'<p style="margin:0 0 0.8em;clear:left;">Please select your preferred payment method below:</p>\';

global $current_user; 
get_currentuserinfo(); 
if ( user_can( $current_user, "wholesale_buyer" ) ){ 
        echo \'<div class="pTrade" style="clear:left;">\';
            woocommerce_form_field( \'pTrade\', array( 
              \'type\'             => \'select\', 
              \'class\'            => false, 
              \'label\'            => __(\'\', \'woocommerce\'), 
              \'required\'         => false,
              \'options\'          => array(
                    \'\' => __(\'Select\', \'woocommerce\' ),
                    \'paypal\' => __(\'PayPal invoice\', \'woocommerce\' ),
                    \'bank\' => __(\'Bank Transfer\', \'woocommerce\' ),
                    \'credit\' => __(\'Credit Account\', \'woocommerce\' ),
                    \'cash\' => __(\'Pickup & Pay\', \'woocommerce\' )
                    )
            ), $checked );
        echo \'</div>\';
    } else {
        echo \'<div class="pRetail" style="clear:left;">\';
            woocommerce_form_field( \'pRetail\', array( 
              \'type\'             => \'select\', 
              \'class\'            => false, 
              \'label\'            => __(\'\', \'woocommerce\'), 
              \'required\'         => false,
              \'options\'          => array(
                    \'\' => __(\'Select\', \'woocommerce\' ),
                    \'paypal\' => __(\'PayPal invoice\', \'woocommerce\' ),
                    \'bank\' => __(\'Bank Transfer\', \'woocommerce\' ),
                    \'cash\' => __(\'Pickup & Pay\', \'woocommerce\' )
                    )
            ), $checked );
        echo \'</div>\';
}
echo \'<p style="margin:0 0 0.8em;clear:left;color:red;"><strong>Note:</strong> Selecting to pay through "PayPal invoice" adds 5% to your order total. <br/> You will recieve an email from PayPal requesting the amount below + 5%. </p>\';
echo \'</div>\';
echo \'<div class="clear"></div>\';
}

/** Update the order meta with field value  WORKING ****************/
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[\'pTrade\']) update_post_meta( $order_id, \'Trade payment method\', esc_attr($_POST[\'pTrade\']));
        if ($_POST[\'pRetail\']) update_post_meta( $order_id, \'Retail payment method\', esc_attr($_POST[\'pRetail\']));
    }

/** Add the field to order email **/
add_filter(\'woocommerce_email_order_meta_keys\', \'my_custom_checkout_field_order_meta_keys\');
    function my_custom_checkout_field_order_meta_keys($keys) {
        echo \'<h3>Payment Option :</h3>\';
        $keys[\'Trade pay by:\'] = \'pTrade\';
        $keys[\'Retail pay by:\'] = \'pRetail\';
        return $keys;
    }
我读过这个文件-https://gist.github.com/mikejolley/1604009 还有一些人,但仍然没有表现出任何喜悦!。

1 个回复
SO网友:Prasad Nevase

如果您提供的代码是您用来实现所需的唯一代码,那么这就是不完整的代码。下面是代码的测试版本,运行良好。我希望这有助于:

/* 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( \'p_trade\', array(
        \'type\'          => \'select\',
        \'class\'         => array(\'p-trade orm-row-wide\'),
        \'label\'         => __(\'Trade Details\'),
        \'placeholder\'   => __(\'Enter a number\'),
        \'clear\'     => true,
        \'options\' => array( \'trade-1\' => \'Trade One\', \'trade-2\' => \'Trade Two\', \'trade-3\' => \'Trade Three\', \'trade-4\' => \'Trade Four\' )
        ), $checkout->get_value( \'p_trade\' ));

        woocommerce_form_field( \'p_retail\', array(
            \'type\'          => \'select\',
            \'class\'         => array(\'p-retail orm-row-wide\'),
            \'label\'         => __(\'Retail Details\'),
            \'placeholder\'   => __(\'Enter a number\'),
            \'clear\'     => true,
            \'options\' => array(\'retail-1\' => \'Retail One\', \'retail-2\' => \'Retail Two\', \'retail-3\' => \'Retail Three\', \'retail-4\' => \'Retail Four\')
        ), $checkout->get_value( \'p_retail\' ));
    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[\'p_trade\']) update_post_meta( $order_id, \'p_trade\', esc_attr($_POST[\'p_trade\']));
    if ($_POST[\'p_retail\']) update_post_meta( $order_id, \'p_retail\', esc_attr($_POST[\'p_retail\']));
}

/* Add the fields to order email */

add_filter( \'woocommerce_email_order_meta_keys\', \'my_custom_checkout_field_order_meta_keys\' );
function my_custom_checkout_field_order_meta_keys( $keys ) {
                echo \'<h3>Payment Option:</h3>\';
        $keys[\'Trade\'] = \'p_trade\';
        $keys[\'Retail\'] = \'p_retail\';
        return $keys;
}

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: