В Wordpress 5.3.2 и с WooCommerce 4.0.1 я пытаюсь добавить новые поля в область выставления счетов и редактировать и сохранять их в порядке admin.
Я пробовал код, доступный на https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ - добавление специального специального поля, но редактирование и сохранение пользовательского поля не помогло.
Я искал в Интернете и нашел следующее решение, позволяющее мне редактировать поля: но я все еще не могу сохранить их в порядке области администратора:
// Add fiels in checkout
add_filter( 'woocommerce_checkout_fields' , 'adauga_campuri_companie' );
function adauga_campuri_companie( $fields ) {
$fields['billing']['checkbox_pj'] = array(
'type' => 'checkbox',
'label' => __('Factura pe persoana juridica', 'woocommerce'),
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['billing_ncompanie'] = array(
'label' => __('Nume companie', 'woocommerce'),
'placeholder' => _x('Nume companie', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['billing_jcompanie'] = array(
'label' => __('Nr. inregistare Registrul Comertului', 'woocommerce'),
'placeholder' => _x('Nr. inregistare Registrul Comertului', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['billing_cuicompanie'] = array(
'label' => __('Cod Unic Inregistrare', 'woocommerce'),
'placeholder' => _x('Cod Unic Inregistrare', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['billing_banca'] = array(
'label' => __('Banca', 'woocommerce'),
'placeholder' => _x('Banca', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['billing_cont'] = array(
'label' => __('Cont Bancar', 'woocommerce'),
'placeholder' => _x('Cont bancar', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
//Show the fields only when the checkbox is checked
add_action( 'woocommerce_after_checkout_form', 'afisare_conditionata_date_companie', 6);
function afisare_conditionata_date_companie() {
?>
<script type="text/javascript">
function uncheck() {
jQuery('#billing_ncompanie_field').hide();
jQuery('#billing_jcompanie_field').hide();
jQuery('#billing_cuicompanie_field').hide();
jQuery('#billing_banca_field').hide();
jQuery('#billing_cont_field').hide();
}
function check() {
jQuery('#billing_ncompanie_field').show();
jQuery('#billing_jcompanie_field').show();
jQuery('#billing_cuicompanie_field').show();
jQuery('#billing_banca_field').show();
jQuery('#billing_cont_field').show();
}
console.log('before condition');
if (jQuery('#checkbox_pj:checked').length == 0) {
uncheck();
} else {
check();
}
jQuery('input#checkbox_pj').change(function(){
if (this.checked) {
check();
} else {
uncheck();
}
});
</script>
<?php
}
/**
* Validate the fields at checkout
*/
add_action('woocommerce_checkout_process', 'validare_campuri_companie');
function validare_campuri_companie() {
if (isset( $_POST['checkbox_pj'] ))
{
if ( ! $_POST['billing_ncompanie'] )
wc_add_notice( __( 'Va rugam sa introduceti numele companiei.' ), 'error' );
if ( ! $_POST['billing_jcompanie'] )
wc_add_notice( __( 'Va rugam sa introduceti Nr. inregistare la Registrul Comertului.' ), 'error' );
if ( ! $_POST['billing_cuicompanie'] )
wc_add_notice( __( 'Va rugam sa introduceti Codul Unic de Inregistrare.' ), 'error' );
if ( ! $_POST['billing_banca'] )
wc_add_notice( __( 'Va rugam sa introduceti banca.' ), 'error' );
if ( ! $_POST['billing_cont'] )
wc_add_notice( __( 'Va rugam sa introduceti contul bancar.' ), 'error' );
}
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'date_companie_update_order_meta' );
// Save the custom editable field value as order meta data and update order item meta data
function date_companie_update_order_meta( $order_id ) {
if (isset( $_POST['checkbox_pj'] ))
{
if ( ! empty( $_POST['billing_ncompanie'] ) ) {
update_post_meta( $order_id, 'Nume companie', sanitize_text_field( $_POST['billing_ncompanie'] ) );
}
if ( ! empty( $_POST['billing_jcompanie'] ) ) {
update_post_meta( $order_id, 'Nr. inregistare Registrul Comertului', sanitize_text_field( $_POST['billing_jcompanie'] ) );
}
if ( ! empty( $_POST['billing_cuicompanie'] ) ) {
update_post_meta( $order_id, 'Cod Unic Inregistrare', sanitize_text_field( $_POST['billing_cuicompanie'] ) );
}
if ( ! empty( $_POST['billing_banca'] ) ) {
update_post_meta( $order_id, 'Banca', sanitize_text_field( $_POST['billing_banca'] ) );
}
if ( ! empty( $_POST['billing_cont'] ) ) {
update_post_meta( $order_id, 'Cont Bancar', sanitize_text_field( $_POST['billing_cont'] ) );
}
}
}
// Add the fields to the emails
add_filter('woocommerce_email_order_meta_keys', 'campuri_companie_order_meta_keys');
function campuri_companie_order_meta_keys( $keys ) {
$keys[] = 'Nume companie' ;
$keys[] = 'Nr. inregistare Registrul Comertului';
$keys[] = 'Cod Unic Inregistrare';
$keys[] = 'Banca' ;
$keys[] = 'Cont Bancar' ;
return $keys;
}
//Add the fiels to thank you page and view order
add_action( 'woocommerce_thankyou', 'detalii_companie_comanda_thanyou_page', 20 );
add_action( 'woocommerce_view_order', 'detalii_companie_comanda_thanyou_page', 20 );
function detalii_companie_comanda_thanyou_page( $order_id ){
$meta = get_post_meta( $order_id, 'checkbox_pj', true );
if (isset($meta) && !empty(get_post_meta( $order_id, 'Nume companie', true ))) {
?>
<h2>Date facturare companie</h2>
<p>
Nume companie: <?php echo get_post_meta( $order_id, 'Nume companie', true ); ?>
<br/>
Nr. inregistare Registrul Comertului: <?php echo get_post_meta( $order_id, 'Nr. inregistare Registrul Comertului', true ); ?>
<br/>
Cod Unic Inregistrare: <?php echo get_post_meta( $order_id, 'Cod Unic Inregistrare', true ); ?>
<br/>
Banca: <?php echo get_post_meta( $order_id, 'Banca', true ); ?>
<br/>
Cont bancar: <?php echo get_post_meta( $order_id, 'Cont Bancar', true ); ?>
</p>
<?php
}
}
// Backend: Display editable custom billing fields in admin
add_filter( 'woocommerce_admin_billing_fields' , 'order_admin_custom_fields' );
function order_admin_custom_fields( $fields ) {
global $theorder;
$fields['checkbox_pj'] = array(
'type' => 'checkbox',
'label' => __('Factura pe persoana juridica', 'woocommerce'),
'value' => get_post_meta( $theorder->id, 'checkbox_pj', true ),
'class' => array('form-field-wide'),
'clear' => true
);
$fields['ncompanie'] = array(
'label' => __( 'Nume companie', 'woocommerce' ),
'value' => get_post_meta( $theorder->id, 'Nume companie', true),
'show' => true,
'wrapper_class' => 'form-field-wide',
'style' => '',
);
$fields['jcompanie'] = array(
'label' => __( 'Nr. inregistare Registrul Comertului', 'woocommerce' ),
'value' => get_post_meta( $theorder->id, 'Nr. inregistare Registrul Comertului', true),
'show' => true,
'wrapper_class' => 'form-field-wide',
'style' => '',
);
$fields['cuicompanie'] = array(
'label' => __( 'Cod Unic Inregistrare', 'woocommerce' ),
'value' => get_post_meta( $theorder->id, 'Cod Unic Inregistrare', true),
'show' => true,
'wrapper_class' => 'form-field-wide',
'style' => '',
);
$fields['banca'] = array(
'label' => __( 'Banca', 'woocommerce' ),
'value' => get_post_meta( $theorder->id, 'Banca', true),
'show' => true,
'wrapper_class' => 'form-field-wide',
'style' => '',
);
$fields['cont'] = array(
'label' => __( 'Cont Bancar', 'woocommerce' ),
'value' => get_post_meta($theorder->id, 'Cont Bancar', true),
'show' => true,
'wrapper_class' => 'form-field-wide',
'style' => '',
);
return $fields;
}
//Save the fields
add_action( 'woocommerce_process_shop_order_meta', 'save_custom_code_after_order_details', 12, 2 );
function save_custom_code_after_order_details( $post_id, $post ) {
if(isset( $_POST['checkbox_pj'] ))
{
if ( ! empty( $_POST['billing_ncompanie'] ) ) {
update_post_meta( $post_id, '_billing_ncompanie', sanitize_text_field( $_POST['billing_ncompanie'] ) );
}
if ( ! empty( $_POST['billing_jcompanie'] ) ) {
update_post_meta( $post_id, '_billing_jcompanie', sanitize_text_field( $_POST['billing_jcompanie'] ) );
}
if ( ! empty( $_POST['billing_cuicompanie'] ) ) {
update_post_meta( $post_id, '_billing_cuicompanie', sanitize_text_field( $_POST['billing_cuicompanie'] ) );
}
if ( ! empty( $_POST['billing_banca'] ) ) {
update_post_meta( $post_id, '_billing_banca', sanitize_text_field( $_POST['billing_banca'] ) );
}
if ( ! empty( $_POST['billing_cont'] ) ) {
update_post_meta( $post_id, '_billing_cont', sanitize_text_field( $_POST['billing_cont'] ) );
}
}
}
Я был бы очень признателен за любую помощь в создании кода (для редактирования и сохранения пользовательских полей в области администратора).
Спасибо!