我在woocommerce结帐页面上创建了一些自定义字段。我的代码是正确的,并且字段显示正确我已经保存了字段数据并将其显示在管理面板中。我的代码正确,并且字段显示正确。
每当订购新产品时,我已经编写了将这些数据包括在管理电子邮件中的代码。我的代码不正确,信息未显示在电子邮件中。
关于此主题的所有其他stackoverflow答案均取决于不推荐使用的过滤器woocommerce_email_order_meta_keys
woocommerce 3.8.0woocommerce_email_order_meta_fields
过滤器没有答案。
我正在运行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 }
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中的值。您需要编写代码以保存自定义添加的结帐字段,以便按以下顺序订购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
功能中添加的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句