Отобразить пользовательские поля оформления заказа WooCommerce в деталях заказа и сделать их редактируемыми - PullRequest
0 голосов
/ 01 марта 2019

Я обновляю плагин для WooCommerce, и у меня есть файл hooks.php, в котором я обрабатываю хуки WooCommerce.Я добавил четыре новых поля для выставления счетов, и мне нужно отобразить их в деталях заказа;Мне также нужно сделать их редактируемыми.

Вот код, в котором мне удается отобразить мои поля:

add_action('woocommerce_admin_order_data_after_billing_address', function($order) 
  echo p(strong(__('Codice Fiscale', 'fatt-24')).': <br />' . order_c_fis($order));
  echo p(strong(__('Partita Iva / VAT number', 'fatt-24')).': <br />' . order_p_iva($order));
  echo p(strong(__('Codice destinatario', 'fatt-24')) . ': <br />' . order_recipientcode($order));
  echo p(strong(__('Indirizzo PEC', 'fatt-24')) . ': <br />' . order_pec_address($order));
}, 10, 1);

Я не могу отредактировать и сохранить его.

Ответы [ 2 ]

0 голосов
/ 14 мая 2019

Наконец-то я нашел это решение:

  <div class="edit_address">
    <?php 
/* here I make the fields editable */
if (customer_use_cf()){
        woocommerce_wp_text_input( array( 'id' => '_billing_fiscalcode', 'label' =>__('Codice Fiscale', 'fatt-24'), 'wrapper_class' => '_billing_company_field' ) ); // here I make the fields editable
}

if (customer_use_vat()){
        woocommerce_wp_text_input( array( 'id' => '_billing_vatcode', 'label' => __('Partita Iva / VAT number', 'fatt-24'), 'wrapper_class' => '_billing_company_field' ) );
}

 if(customer_use_recipientcode()) {
            woocommerce_wp_text_input( array( 'id' => '_billing_recipientcode', 'label' => __('Codice destinatario', 'fatt-24'), 'wrapper_class' => '_billing_company_field' ) );
            woocommerce_wp_text_input( array( 'id' => '_billing_pecaddress', 'label' => __('Indirizzo PEC', 'fatt-24'), 'wrapper_class' => '_billing_company_field' ) );
 }?>
    </div>

    <?php

}, 10, 1);

/* here I update the post */

 add_action( 'woocommerce_process_shop_order_meta',  function ( $post_id, $post ){
    update_post_meta( $post_id, '_billing_pecaddress', wc_clean( sanitize_text_field($_POST[ '_billing_pecaddress' ] )) ); // here I update the post
    update_post_meta( $post_id, '_billing_recipientcode ', wc_clean(sanitize_text_field( $_POST[ '_billing_recipientcode' ] )) );
    update_post_meta( $post_id, '_billing_fiscalcode', wc_clean( sanitize_text_field($_POST[ '_billing_fiscalcode' ]) ) );
    update_post_meta( $post_id, '_billing_vatcode', wc_clean( sanitize_text_field($_POST[ '_billing_vatcode' ]) ) );
    }, 45, 2 );
0 голосов
/ 01 марта 2019

Для отображения в деталях заказа администратора в виде редактируемых полей

add_filter('woocommerce_admin_billing_fields', 'custom_admin_billing_fields', 10, 1);

function custom_admin_billing_fields($fields) {
    $fields['billing_fiscalcode'] = array(
        'label' => __('billing_fiscalcode', 'woocommerce'),
        'show' => true,
    );
    $fields['billing_vatcode'] = array(
        'label' => __('billing_vatcode', 'woocommerce'),
        'show' => true,
    );
    $fields['billing_recipientcode'] = array(
        'label' => __('billing_recipientcode', 'woocommerce'),
        'show' => true,
    );
    $fields['billing_pecaddress'] = array(
        'label' => __('billing_pecaddress', 'woocommerce'),
        'show' => true,
    );
    return $fields;
}

add_action('woocommerce_admin_order_data_after_billing_address', 'display_billing_options_value_in_admin_order', 10, 1);

function display_billing_options_value_in_admin_order($order) {


    if ($value = get_post_meta($order->get_id(), 'billing_fiscalcode', true))
        echo '<p><strong>' . __('Billing Fiscalcode', 'woocommerce') . ':</strong> ' . $value . '</p>';
    if ($value = get_post_meta($order->get_id(), 'billing_vatcode', true))
        echo '<p><strong>' . __('billing_vatcode', 'woocommerce') . ':</strong> ' . $value . '</p>';
    if ($value = get_post_meta($order->get_id(), 'billing_recipientcode', true))
        echo '<p><strong>' . __('billing_recipientcode', 'woocommerce') . ':</strong> ' . $value . '</p>';
    if ($value = get_post_meta($order->get_id(), 'billing_pecaddress', true))
        echo '<p><strong>' . __('billing_pecaddress', 'woocommerce') . ':</strong> ' . $value . '</p>';
}
...