Created
June 17, 2020 16:34
-
-
Save xlawok/e88748878c738e0b63fbd17add6fc0d9 to your computer and use it in GitHub Desktop.
woocommerce add receipt of invoice to checkout, with validation and display it in wp-admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); | |
// ADD NIP AND REARRANGE | |
function custom_override_checkout_fields( $fields ) { | |
$fields['billing']['pk_nip'] = array( | |
'label' => __('NIP', 'woocommerce'), | |
'placeholder' => _x('Wpisz NIP, jeżeli chcesz otrzymać fakturę', 'placeholder', 'woocommerce'), | |
'required' => false, | |
'class' => array('form-row-wide'), | |
'clear' => true, | |
'priority' =>35, //this way you can change order of fields in checkout | |
); | |
$fields['billing']['pk_invoice_receipt'] = array( | |
'label' => __('Faktura VAT lub paragon', 'woocommerce'), | |
'type' => 'radio', | |
'required' => true, | |
'class' => array('form-row-wide'), | |
'clear' => true, | |
'priority' =>5, | |
'options' => array( | |
'paragon' => 'Paragon', | |
'faktura' => 'Faktura', | |
), | |
); | |
return $fields; | |
} | |
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 ); | |
add_action( 'woocommerce_checkout_update_order_meta', 'pk_save_faktura_paragon' ); | |
function pk_save_faktura_paragon( $order_id ){ | |
if( !empty( $_POST['pk_invoice_receipt'] ) ) | |
update_post_meta( $order_id, 'pk_invoice_receipt', sanitize_text_field( $_POST['pk_invoice_receipt'] ) ); | |
if( !empty( $_POST['pk_nip'] ) ) | |
update_post_meta( $order_id, 'pk_nip', sanitize_text_field( $_POST['pk_nip'] ) ); | |
} | |
function my_custom_checkout_field_display_admin_order_meta($order){ | |
echo '<p><strong>'.__('FAKTURA LUB PARAGON').':</strong> ' . get_post_meta( $order->get_id(), 'pk_invoice_receipt', true ) . '</p>'; | |
echo '<p><strong>'.__('NIP').':</strong> ' . get_post_meta( $order->get_id(), 'pk_nip', true ) . '</p>'; | |
} | |
add_action( 'woocommerce_after_checkout_validation', 'pk_validate_invoice', 10, 2); | |
function pk_validate_invoice( $fields, $errors ){ | |
if ( $fields[ 'pk_invoice_receipt' ]==="faktura"&&(empty($fields[ 'pk_nip' ])||empty($fields[ 'billing_company' ])) ){ | |
$errors->add( 'validation', 'Jeżeli chcesz otrzymać Fakturę, uzupełnij nazwę firmy oraz NIP' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment