1 回答

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊
步驟 1)將您的代碼更改為此
/* 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>' . __('Imi? i nazwisko osoby obdarowanej') . '</h3>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Podaj imi? i nazwisko osoby obdarowanej'),
'placeholder' => __(''),
'required' => true,
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
/* Process the checkout */
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['my_field_name'] )
wc_add_notice( __( '<strong>Imi? i nazwisko osoby obdarowanej</strong> jest wymaganym polem' ), 'error' );
}
/* Update the order meta with field value */
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_create_order', 10, 2 );
function my_custom_checkout_field_create_order( $order, $data ) {
if ( ! empty( $_POST['my_field_name'] ) ) {
$order->update_meta_data( '_my_field', sanitize_text_field( $_POST['my_field_name'] ) ); // Order meta data
}
}
/* Display field value on the order edit page */
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order) {
// Get meta
$my_field = $order->get_meta( '_my_field' );
echo '<p><strong>'.__('Imie i nazwisko obdarowanej').':</strong> ' . $my_field . '</p>';
}
步驟 2)覆蓋以下模板文件
https://github.com/woocommerce/woocommerce/blob/3.8.0/templates/emails/email-order-details.php
可以通過(guò)將其復(fù)制到y(tǒng)ourtheme/woocommerce/emails/email-order-details.php來(lái)覆蓋此模板 。
在第84行下添加以下代碼
$my_field = $order->get_meta( '_my_field' );
if ( isset( $my_field ) ) {
?>
<tr>
<th class="td" scope="row" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php echo 'Imie i nazwisko obdarowanej:'; ?></th>
<td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php echo $my_field; ?></td>
</tr>
<?php
}
- 1 回答
- 0 關(guān)注
- 97 瀏覽
添加回答
舉報(bào)