我创建了两个插件:
我已经成功地在注册表单,结帐页面和用户仪表板面板中添加了我的字段,但在结帐页面上我无法用预先设定的值填充它们。
第一个插件用于在注册时添加新字段。新字段为:'billing_bulstat'; 'billing_mol'和'billing_adres'。它们已成功保存并显示在用户仪表板面板中。代码是:
<?php
/**
* Plugin Name: WooCommerce Registration Fields in Luga.bg - Traders Store
* Plugin URI: http://www.luga.bg/traders
* Description: My Custom registration fields in Luga.bg - Traders Store
* Version: 2.0
* Author: Nikolay Grudev
* Author URI: http://www.luga.bg/traders
* License: GPL3
*/
/**
* Add new register fields for WooCommerce registration in Luga.bg - Traders Store.
*
* @return string Register fields HTML.
*/
function wooc_extra_register_fields() {
?>
<p class="form-row form-row-first">
<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span
class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( !
empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?> <span
class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( !
empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
</p>
<div class="clear"></div>
<p class="form-row form-row-wide">
<label for="reg_billing_company"><?php _e( 'Име на фирмата', 'woocommerce' ); ?> <span
class="required">*</span></label>
<input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php if ( ! empty(
$_POST['billing_company'] ) ) esc_attr_e( $_POST['billing_company'] ); ?>" />
</p>
<div class="clear"></div>
<p class="form-row form-row-first">
<label for="reg_billing_bulstat"><?php _e( 'ЕИК', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_bulstat" id="reg_billing_bulstat" value="<?php if ( ! empty(
$_POST['billing_bulstat'] ) ) esc_attr_e( $_POST['billing_bulstat'] ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="reg_billing_city"><?php _e( 'City', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php if ( ! empty(
$_POST['billing_city'] ) ) esc_attr_e( $_POST['billing_city'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_mol"><?php _e( 'МОЛ (Материално отговорно лице)', 'woocommerce' ); ?> <span
class="required">*</span></label>
<input type="text" class="input-text" name="billing_mol" id="reg_billing_mol" value="<?php if ( ! empty( $_POST
['billing_mol'] ) ) esc_attr_e( $_POST['billing_mol'] ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="reg_billing_adres"><?php _e( 'Адрес за фактуриране', 'woocommerce' ); ?> <span
class="required">*</span></label>
<input type="text" class="input-text" name="billing_adres" id="reg_billing_adres" value="<?php if ( ! empty(
$_POST['billing_adres'] ) ) esc_attr_e( $_POST['billing_adres'] ); ?>" />
</p>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
/**
* Validate the extra register fields.
*
* @param string $username Current username.
* @param string $email Current email.
* @param object $validation_errors WP_Error object.
*
* @return void
*/
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$validation_errors->add( 'billing_first_name_error', __( '<strong>Грешка</strong>: Първото име е
задъжлително!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( '<strong>Грешка</strong>: Фамилията е
задължителна!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_company'] ) && empty( $_POST['billing_company'] ) ) {
$validation_errors->add( 'billing_company_error', __( '<strong>Грешка</strong>: Името на фирмата е
задължително!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_bulstat'] ) && empty( $_POST['billing_bulstat'] ) ) {
$validation_errors->add( 'billing_bulstat_error', __( '<strong>Грешка</strong>: ЕИК на фирмата е
задължителен!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_city'] ) && empty( $_POST['billing_city'] ) ) {
$validation_errors->add( 'billing_city_error', __( '<strong>Грешка</strong>: Градът е задължителен!.',
'woocommerce' ) );
}
if ( isset( $_POST['billing_mol'] ) && empty( $_POST['billing_mol'] ) ) {
$validation_errors->add( 'billing_mol_error', __( '<strong>Грешка</strong>: МОЛ е задължително!.',
'woocommerce' ) );
}
if ( isset( $_POST['billing_adres'] ) && empty( $_POST['billing_adres'] ) ) {
$validation_errors->add( 'billing_adres_error', __( '<strong>Грешка</strong>: Адресът за фактуриране е
задължителен!.', 'woocommerce' ) );
}
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
/**
* Save the extra register fields.
*
* @param int $customer_id Current customer ID.
*
* @return void
*/
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_first_name'] ) ) {
// WordPress default first name field.
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
// WooCommerce billing first name.
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
// WordPress default last name field.
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
// WooCommerce billing last name.
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
if ( isset( $_POST['billing_company'] ) ) {
// WooCommerce company name
update_user_meta( $customer_id, 'company', sanitize_text_field( $_POST['billing_company'] ) );
// WooCommerce billing company name.
update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
}
if ( isset( $_POST['billing_bulstat'] ) ) {
// WooCommerce company bulstat
update_user_meta( $customer_id, 'bulstat', sanitize_text_field( $_POST['billing_bulstat'] ) );
// WooCommerce billing company bulstat
update_user_meta( $customer_id, 'billing_bulstat', sanitize_text_field( $_POST['billing_bulstat'] ) );
}
if ( isset( $_POST['billing_city'] ) ) {
// WooCommerce company city
update_user_meta( $customer_id, 'city', sanitize_text_field( $_POST['billing_city'] ) );
// WooCommerce billing company city
update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
}
if ( isset( $_POST['billing_mol'] ) ) {
// WooCommerce company mol
update_user_meta( $customer_id, 'mol', sanitize_text_field( $_POST['billing_mol'] ) );
// WooCommerce billing company mol
update_user_meta( $customer_id, 'billing_mol', sanitize_text_field( $_POST['billing_mol'] ) );
}
if ( isset( $_POST['billing_adres'] ) ) {
// WooCommerce company address
update_user_meta( $customer_id, 'adres', sanitize_text_field( $_POST['billing_adres'] ) );
// WooCommerce billing company address
update_user_meta( $customer_id, 'billing_adres', sanitize_text_field( $_POST['billing_adres'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
add_action( 'woocommerce_checkout_process', 'ws_billing_fields_save', 10, 1 );
function ws_billing_fields_save( $user_id ){
if ( isset( $_POST['billing_bulstat'] ) ) {
update_user_meta($user_id, 'billing_bulstat', $_POST['billing_bulstat']);
}
if ( isset( $_POST['billing_mol'] ) ) {
update_user_meta($user_id, 'billing_mol', $_POST['billing_mol']);
}
if ( isset( $_POST['billing_adres'] ) ) {
update_user_meta($user_id, 'billing_adres', $_POST['billing_adres']);
}
}
/**
* Dopylnitelni poleta v User Profil na Dashboard v Wordpress
*/
function eri_add_custom_user_profile_fields( $user ) {
?>
<!-- Field Title -->
<h3><?php _e('Company Information', 'eribootstrap'); ?></h3>
<table class="form-table">
<tr>
<th>
<label for="billing_bulstat">
<?php _e('ЕИК', 'eribootstrap'); ?>
</label>
</th>
<td>
<input type="text" name="billing_bulstat" id="billing_bulstat" value="<?php echo
esc_attr( get_the_author_meta( 'billing_bulstat', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e('Въведи ЕИК.', 'eribootstrap'); ?></span>
</td>
</tr><!-- field ends here -->
<tr>
<th>
<label for="billing_mol"><?php _e('МОЛ', 'eribootstrap'); ?>
</label></th>
<td>
<input type="text" name="billing_mol" id="billing_mol" value="<?php echo esc_attr(
get_the_author_meta( 'billing_mol', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e('Въведете МОЛ на фирмата', 'eribootstrap'); ?
></span>
</td>
</tr><!-- field ends here -->
<tr>
<th>
<label for="billing_adres"><?php _e('Адрес за фактуриране', 'eribootstrap'); ?>
</label></th>
<td>
<textarea name="billing_adres" id="billing_adres" class="regular-text"><?php echo esc_attr( get_the_author_meta(
'billing_adres', $user->ID ) ); ?>
</textarea><br />
<span class="description"><?php _e('Въведете адрес за фактуриране', 'eribootstrap'); ?></span>
</td>
</tr><!-- field ends here -->
</table>
<?php }
function eri_save_custom_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return FALSE;
// Update and Save Field
update_usermeta( $user_id, 'billing_bulstat', $_POST['billing_bulstat'] );
update_usermeta( $user_id, 'billing_mol', $_POST['billing_mol'] );
update_usermeta( $user_id, 'billing_adres', $_POST['billing_adres'] );
}
add_action( 'show_user_profile', 'eri_add_custom_user_profile_fields' );
add_action( 'edit_user_profile', 'eri_add_custom_user_profile_fields' );
add_action( 'personal_options_update', 'eri_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'eri_save_custom_user_profile_fields' );
第二个插件在结帐页面上添加了三个新的自定义字段。它们会显示,但不会自动填充。我使用$ checkout-&gt; get_value('my_field_name'));功能,但是不起作用。
我想让它们自动填充预先设定的值。字段相同:'billing_bulstat'; 'billing_mol'和'billing_adres'。代码是:
<?php
/*
Plugin Name: Woocommerce Checkout fileds
Plugin URI: https://www.luga.bg/traders
Description: Customized Woocommerce checkout fields
Version: 1
Author: Nikolay Grudev
Author URI: http://www.luga.bg/
*/
/**
* set customized Woocommerce checkout fields
*/
add_filter( 'woocommerce_checkout_fields' , 'customize_fields' );
function customize_fields( $fields ) {
// make fields required:
$fields['billing']['billing_company']['required'] = true;
return $fields;
}
// Remove some checkout billing fields
function kia_filter_billing_fields($fields){
unset( $fields["billing_country"] );
unset( $fields["billing_address_1"] );
unset( $fields["billing_address_2"] );
unset( $fields["billing_state"] );
unset( $fields["billing_postcode"] );
return $fields;
}
add_filter( 'woocommerce_billing_fields', 'kia_filter_billing_fields' );
/**
* 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"><h2>' . __('Extra Information') . '</h2>';
woocommerce_form_field(
'billing_bulstat', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('EIK'),
'placeholder' => __('Enter something'),
'required' => true,
),$checkout->get_value( 'billing_bulstat' ) );
woocommerce_form_field(
'billing_mol', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('MOL'),
'placeholder' => __('Enter something'),
'required' => true,
), $checkout->get_value( 'billing_mol' ) );
woocommerce_form_field(
'billing_adres', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Adres za fakturirane'),
'placeholder' => __('Enter something'),
'required' => true,
), $checkout->get_value( 'billing_adres' ) );
echo '</div>';
}
/**
* Validate the custom field.
*/
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['billing_bulstat'] )
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
if ( ! $_POST['billing_mol'] )
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
if ( ! $_POST['billing_adres'] )
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
}
/**
* Save 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 ( ! empty( $_POST['billing_bulstat'] ) ) {
update_post_meta( $order_id, 'billing_bulstat', sanitize_text_field( $_POST['billing_bulstat'] ) );
}
if ( ! empty( $_POST['billing_mol'] ) ) {
update_post_meta( $order_id, 'billing_mol', sanitize_text_field( $_POST['billing_mol'] ) );
}
if ( ! empty( $_POST['billing_adres'] ) ) {
update_post_meta( $order_id, 'billing_adres', sanitize_text_field( $_POST['billing_adres'] ) );
}
}
/**
* 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){
echo '<p><strong>'.__('Extra Information').':</strong> ' . get_post_meta( $order->id, 'billing_bulstat', true )
. '</p>';
echo '<p><strong>'.__('Extra Information').':</strong> ' . get_post_meta( $order->id, 'billing_mol', true ) .
'</p>';
echo '<p><strong>'.__('Extra Information').':</strong> ' . get_post_meta( $order->id, 'billing_adres', true ) .
'</p>';
}
// display the extra data on order recieved page and my-account order review
function kia_display_order_data( $order_id ){ ?>
<h2><?php _e( 'Additional Info' ); ?></h2>
<table class="shop_table shop_table_responsive additional_info">
<tbody>
<tr>
<th><?php _e( 'Bulstat: ' ); ?></th>
<td><?php echo get_post_meta( $order_id, 'billing_bulstat', true ); ?></td>
</tr>
<tr>
<th><?php _e( 'MOL: ' ); ?></th>
<td><?php echo get_post_meta( $order_id, 'billing_mol', true ); ?></td>
</tr>
<tr>
<th><?php _e( 'Adres za fakturirane: ' ); ?></th>
<td><?php echo get_post_meta( $order_id, 'billing_adres', true ); ?></td>
</tr>
</tbody>
</table>
<?php }
add_action( 'woocommerce_thankyou', 'kia_display_order_data', 20 );
add_action( 'woocommerce_view_order', 'kia_display_order_data', 20 );
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( 'Danni za firmata', 'woocommerce' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Firma: ' ) . '</strong>' . get_post_meta( $order->id, '_billing_company', true
) . '</p>';
echo '<p><strong>' . __( 'Bulstat: ' ) . '</strong>' . get_post_meta( $order->id, 'billing_bulstat',
true ) . '</p>';
echo '<p><strong>' . __( 'MOL: ' ) . '</strong>' . get_post_meta( $order->id, 'billing_mol', true ) .
'</p>';
echo '<p><strong>' . __( 'Adres za fakturirane: ' ) . '</strong>' . get_post_meta( $order-
>id, 'billing_adres', true ) . '</p>'; ?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );
// WooCommerce 2.3+
function kia_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['billing_bulstat'] = array(
'label' => __( 'Bulstat' ),
'value' => get_post_meta( $order->id, 'billing_bulstat', true ),
);
$fields['billing_mol'] = array(
'label' => __( 'MOL' ),
'value' => get_post_meta( $order->id, 'billing_mol', true ),
);
$fields['billing_adres'] = array(
'label' => __( 'Adrez za fakturirane' ),
'value' => get_post_meta( $order->id, 'billing_adres', true ),
);
return $fields;
}
add_filter('woocommerce_email_order_meta_fields', 'kia_email_order_meta_fields', 10, 3 );
?>
哪里错了?为什么我的字段没有按照其值来填充atomaticaly?
感谢您的帮助! 该网站是www.luga.bg/traders