0

I added "VAT number" extra field in Woocommerce checkout page and in order to store meta data I have put the following code in functions.php:

/* Save VAT Number in the order meta */
add_action( 'woocommerce_checkout_update_user_meta', 'wda_woocommerce_checkout_update_user_meta', 10, 2 );
function wda_woocommerce_checkout_update_user_meta( $customer_id, $posted ) {
    if (isset($posted['vat_number'])) {
        $dob = sanitize_text_field( $posted['vat_number'] );
        update_user_meta( $customer_id, 'vat_number', $dob);
    }
}

In order to display/edit VAT Number in order edit screen I have also put the following:

/* Display/Edit VAT Number in order edit screen */
function wda_display_order_data_in_admin( $order ){  ?>
        <div class="address">
        <?php 
            echo '<p><strong>' . __( 'VAT n.' ) . ':</strong>' . $order->get_meta( '_vat_number' ) . '</p>'; ?>
        </div>
        <div class="edit_address">
            <?php woocommerce_wp_text_input( array( 'id' => '_vat_number', 'label' => __( 'VAT n.' ), 'wrapper_class' => '_billing_company_field' ) ); ?>
        </div>

<?php }
add_action( 'woocommerce_admin_order_data_after_billing_address', 'wda_display_order_data_in_admin' );

function wda_save_extra_details( $order_id, $post ){
    $order = wc_get_order( $order_id );
    $order->update_meta_data( '_vat_number', wc_clean( $_POST[ '_vat_number' ] ) );
    //$order->update_meta_data( '_another_field', wc_clean( $_POST[ '_another_field' ] ) );
    $order->save_meta_data();
}
add_action( 'woocommerce_process_shop_order_meta', 'wda_save_extra_details', 45, 2 );

The issue is that VAT number is not displayed on order page/edit. What might be wrong?

-1

Simple answer..I had to update also order meta...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.