我在woocommerce结帐页面上创建了一些自定义字段。我的代码正确,并且字段显示正确 我已经保存了字段数据,并将其显示在管理面板中。我的代码正确,并且字段显示正确。
每当订购新产品时,我已经编写了将这些数据包括在管理电子邮件中的代码。 我的代码不正确,信息也没有显示在电子邮件中。
有关此主题的所有其他stackoverflow答案均取决于不推荐使用的过滤器
woocommerce_email_order_meta_keys
woocommerce 3.8.0 woocommerce_email_order_meta_fields
没有可解决的stackoverflow
过滤器。
我正在运行woocommerce 3.8.0和wordpress 5.3。 我将这些文件保存在wp-content / themes / child-theme / functions.php
WTF我的代码有问题吗?我已经一遍又一遍地检查了一下,但我不知道出了什么问题。有人可以告诉我我在做什么错吗?我是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 }
答案 0 :(得分:1)
WordPress可以选择打开调试模式,以便可以识别和解决错误和警告。错误将记录在wordpress文件夹debug.log
文件夹中名为wp-content
的文件中,而您的代码将出错。因此,在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;
}
此外,您还没有编写代码来保存添加到结帐页面的自定义字段。您正在尝试查找未保存在订单元中的值。您需要编写代码以保存自定义添加的结帐字段,以便按如下所示对meta进行排序。
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
函数中添加的。