first commit

This commit is contained in:
2024-11-10 21:08:49 +01:00
commit 0d932ce5ee
14455 changed files with 2567501 additions and 0 deletions

View File

@@ -0,0 +1,244 @@
<?php
/*
* Plugin Name: WooCommerce NIP
Plugin URI:
Description: Dodatkowe pole - mumer NIP - dla WooCommerce
Version: 1.1.0
Author: Krzysztof Busłowicz
Author URI: http://www.sirta.pl
Text Domain: woocommerce-nip
Domain Path: /languages/
License: GPL
License URI: https://www.gnu.org/licenses/gpl.html
*/
defined( 'ABSPATH' ) or die( 'Cheating? No script kiddies please!' );
/***
*Multilanguage support
***/
add_action('plugins_loaded', 'wnip_load_textdomain');
function wnip_load_textdomain() {
load_plugin_textdomain( 'woocommerce-nip', false, dirname( plugin_basename(__FILE__) ) . '/languages/' );
}
/**
*Adding field to user profiles
// Hook in */
add_filter( 'woocommerce_billing_fields', 'my_custom_billing_fields' );
// Function Hook
Function my_custom_billing_fields( $fields ) {
$fields['billing_nip'] = array(
'label' => __('NIP','woocommerce-nip'),
'placeholder' => _x('Podaj NIP jeśli będziesz potrzebować faktury', 'placeholder', 'woocommerce-nip'),
'required' => false,
'class' => array('my-field-class form-row-wide'),
'clear' => true
);
// just copy same format if youd like to add more fields
return $fields;
}
/**
* Adding address fields to user registration form
**/
/**
* Add new register fields for WooCommerce registration.
*
* @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( 'Company Name', 'woocommerce' ); ?> </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>
<p class="form-row form-row-wide">
<label for="reg_billing_address_1"><?php _e( 'Address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_address_1" id="reg_billing_address_1" value="<?php if ( ! empty( $_POST['billing_address_1'] ) ) esc_attr_e( $_POST['billing_address_1'] ); ?>" />
</p>
<p class="form-row form-row-first">
<label for="reg_billing_postcode"><?php _e( 'Postcode / Zip', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php if ( ! empty( $_POST['billing_postcode'] ) ) esc_attr_e( $_POST['billing_postcode'] ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="reg_billing_city"><?php _e( 'Town / City', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_city" id="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_nip"><?php _e( 'Polish NIP ID','woocommerce-nip' ); ?> </label>
<input type="text" class="input-text" name="billing_nip" id="reg_billing_nip" value="<?php if ( ! empty( $_POST['billing_nip'] ) ) esc_attr_e( $_POST['billing_nip'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action( 'woocommerce_register_form', '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', __( 'First name is required!', 'woocommerce-nip' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( 'Last name is required!.', 'woocommerce-nip' ) );
}
if ( isset( $_POST['billing_address_1'] ) && empty( $_POST['billing_address_1'] ) ) {
$validation_errors->add( 'billing_address_1_error', __( 'Address is required!', 'woocommerce-nip' ) );
}
if ( isset( $_POST['billing_postcode'] ) && empty( $_POST['billing_postcode'] ) ) {
$validation_errors->add( 'billing_postcode_error', __( 'Post code / Zip is required!.', 'woocommerce-nip' ) );
}
if ( isset( $_POST['billing_city'] ) && empty( $_POST['billing_city'] ) ) {
$validation_errors->add( 'billing_city_error', __( 'Town / City is required!.', 'woocommerce-nip' ) );
}
}
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 billing_company
update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
}
if ( isset( $_POST['billing_address_1'] ) ) {
// WooCommerce billing address
update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
}
if ( isset( $_POST['billing_postcode'] ) ) {
// WooCommerce billing phone
update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
}
if ( isset( $_POST['billing_city'] ) ) {
// WooCommerce billing city
update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
}
if ( isset( $_POST['billing_nip'] ) ) {
// WooCommerce billing nip
update_user_meta( $customer_id, 'billing_nip', sanitize_text_field( $_POST['billing_nip'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
/**
*Displaying new nip field in the backend
**
*/
add_action( 'show_user_profile', 'ws_update_user_profile' );
add_action( 'edit_user_profile', 'ws_update_user_profile' );
function ws_update_user_profile( $user ){ ?>
<h3>Dane Firmy</h3>
<table class="form-table">
<tr>
<th><label for="billing_nip">Numer NIP</label></th>
<td><input type="text" name="billing_nip" value="<?php echo esc_attr(get_the_author_meta( 'billing_nip', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<?php
}
add_action( 'personal_options_update', 'save_extra_fields' );
add_action( 'edit_user_profile_update', 'save_extra_fields' );
function save_extra_fields( $user_id ){
update_user_meta( $user_id,'billing_nip', sanitize_text_field( $_POST['billing_nip'] ) );
}
/**
* Update 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 ($_POST['billing_nip']) update_post_meta( $order_id, 'Nip', esc_attr($_POST['billing_nip']));
}
/**
* Display field value on the order edition page
**/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta');
function my_custom_checkout_field_display_admin_order_meta($post){
$key_1_values = get_post_meta( $post->id, 'Nip' );
echo '<strong>NIP</strong>: '.$key_1_values[0];
}
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
$keys[] = 'Nip';
return $keys;
}