我在 woocommerce 结帐页面上创建了一些自定义字段。我的代码是正确的并且字段显示正确 我已保存现场数据并将其显示在管理面板中。我的代码是正确的,字段显示正确。
我编写了代码,以便在订购新产品时将这些数据包含在管理电子邮件中。 我的代码不正确,信息未显示在电子邮件中。
有关此主题的所有其他 stackoverflow 答案都依赖于已弃用的过滤器
woocommerce_email_order_meta_keys
没有 stackoverflow 可以回答 woocommerce 3.8.0 woocommerce_email_order_meta_fields
过滤。
我正在运行 woocommerce 3.8.0 和 wordpress 5.3。 我将这些文件保存在 wp-content/themes/child-theme/functions.php
我的代码有什么问题吗?我检查了一遍又一遍,却无法找出问题所在。有人可以告诉我我做错了什么吗?我是一名 Ruby on Rails 开发人员,正在尝试自学 PHP 和 WordPress。
add_filter('woocommerce_email_order_meta_fields','custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['custom_field_1'] = array(
'label' => __( 'custom_field_1' ),
'value' => get_post_meta( $order->id, 'custom_field_1', true ),
);
$fields['custom_field_2'] = array(
'label' => __( 'custom_field_2' ),
'value' => get_post_meta( $order->id, 'custom_field_2', true ),
);
$fields['custom_field_3'] = array(
'label' => __( 'custom_field_3' ),
'value' => get_post_meta( $order->id, 'custom_field_3', true ),
);
$fields['custom_field_4'] = array(
'label' => __( 'custom_field_4' ),
'value' => get_post_meta( $order->id, 'custom_field_4', true ),
);
return $fields;
}
我在 woocommerce 结帐页面中的自定义表单字段位于此处
add_filter( 'woocommerce_checkout_fields', 'isca_custom_checkout_fields' );`
function isca_custom_checkout_fields($fields){
$fields['isca_extra_fields'] = array(
'custom_field_1' => array(
'class' => array(
'form-row-first'
),
'type' => 'text',
'required' => true,
'placeholder' => __( 'Name' )
),
'custom_field_2' => array(
'class' => array(
'form-row-last'
),
'type' => 'text',
'required' => true,
'placeholder' => __( 'Nickname' )
),
'custom_field_3' => array(
'class' => array(
'form-row-first'
),
'type' => 'text',
'required' => true,
'placeholder' => __( 'Favorite Exercise' )
),
'custom_field_4' => array(
'class' => array(
'form-row-last'
),
'type' => 'text',
'required' => false,
'placeholder' => __( 'Favorite Stretch' )
),
);
return $fields;
}
然后我使用此代码将其添加到 woocomerce 结帐页面
add_action( 'woocommerce_after_checkout_billing_form' ,'isca_extra_checkout_fields' );
function isca_extra_checkout_fields(){
$checkout = WC()->checkout(); ?>
<br/>
<div class="extra-fields">
<h3><?php _e( 'Fitness Information' ); ?></h3>
<?php
foreach ( $checkout->checkout_fields['isca_extra_fields'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>
<?php }
最佳答案
WordPress 可以选择打开 Debug模式,以便识别和解决错误和警告。当您的代码出现任何错误时,错误将记录在 WordPress 文件夹的 wp-content
文件夹中名为 debug.log
的文件中。因此,在 WordPress 中进行开发时,您必须打开三个选项。
转到 wp-config.php 并添加这三行代码
define('WP_DEBUG', true);
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', true );
您的custom_woocommerce_email_order_meta_field
函数有错误。您调用的订单 ID 不正确。错误日志会显示这一点。不应直接调用订单属性。因此,您必须将 $order->id
更改为 $order->get_id()
。因此将函数更改为
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
if( !$sent_to_admin ){
return;
}
$fields['custom_field_1'] = array(
'label' => __( 'custom field 1' ),
'value' => get_post_meta( $order->get_id(), 'custom_field_1', true ),
);
$fields['custom_field_2'] = array(
'label' => __( 'custom field 2' ),
'value' => get_post_meta( $order->get_id(), 'custom_field_2', true ),
);
$fields['custom_field_3'] = array(
'label' => __( 'custom field 3' ),
'value' => get_post_meta( $order->get_id(), 'custom_field_3', true ),
);
$fields['custom_field_4'] = array(
'label' => __( 'custom field 4' ),
'value' => get_post_meta( $order->get_id(), 'custom_field_4', true ),
);
return $fields;
}
此外,您还没有编写任何代码来保存添加到结账页面的自定义字段。您正在尝试查找未保存在订单元中的值。您需要编写代码以将自定义添加的结帐字段保存到订单元中,如下所示。
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['custom_field_1']) update_post_meta( $order_id, 'custom_field_1', esc_attr($_POST['custom_field_1']));
if ($_POST['custom_field_2']) update_post_meta( $order_id, 'custom_field_2', esc_attr($_POST['custom_field_2']));
if ($_POST['custom_field_3']) update_post_meta( $order_id, 'custom_field_3', esc_attr($_POST['custom_field_3']));
if ($_POST['custom_field_4']) update_post_meta( $order_id, 'custom_field_4', esc_attr($_POST['custom_field_4']));
}
完成。 。 。现在所有内容都已完美添加到电子邮件中(经过测试和确认)。另外,由于您已经说过这些字段显示在管理电子邮件中,因此您必须使用检查该条件
if( !$sent_to_admin ){
return;
}
我已将其添加到 custom_woocommerce_email_order_meta_fields
函数中。
关于php - 使 Woocommerce (3.8.0) 管理电子邮件包含结账页面中的我的自定义字段数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58848381/