WooCommerce - formatted billing address - php

I am little depressed for a few days now, trying to find a solution for this, so any help would be much appreciated.
In myfunctions.php in Wordpress, I added the following:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
// Our hooked in function - $address_fields is passed via the filter!
function custom_override_default_address_fields( $address_fields ) {
unset($address_fields['postcode']);
$address_fields['company']['required'] = false;
$address_fields['chamber_of_commerce_no'] = array(
'label' => __('Chamber of Commerce No', 'woocommerce'),
'placeholder' => _x('Chamber of Commerce No', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
$address_fields['fiscal_code'] = array(
'label' => __('Fiscal Code', 'woocommerce'),
'placeholder' => _x('For romanian companies', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$address_fields['vat_code'] = array(
'label' => __('VAT Code', 'woocommerce'),
'placeholder' => _x('For foreign companies', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$address_fields['bank_account_no'] = array(
'label' => __('Bank Account No', 'woocommerce'),
'placeholder' => _x('Bank Account No', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
$address_fields['bank'] = array(
'label' => __('Bank', 'woocommerce'),
'placeholder' => _x('Bank', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
return $address_fields;
}
After this, I tried adding the fields to get_formatted_billing_address function:
public function get_formatted_billing_address() {
if ( ! $this->formatted_billing_address ) {
// Formatted Addresses
$address = apply_filters( 'woocommerce_order_formatted_billing_address', array(
'company' => $this->billing_company,
'address_1' => $this->billing_address_1,
'address_2' => $this->billing_address_2,
'city' => $this->billing_city,
'country' => $this->billing_country,
'bank_account_no' => $this->billing_bank_account_no,
'bank' => $this->billing_bank,
'chamber_of_commerce_no'=> $this->billing_chamber_of_commerce_no,
'fiscal_code' => $this->billing_fiscal_code,
'vat_code' => $this->billing_vat_code
), $this );
$this->formatted_billing_address = WC()->countries->get_formatted_address( $address );
}
return $this->formatted_billing_address;
}
It did not work.
Then I tried adding a filter:
add_filter ('woocommerce_order_formatted_billing_address', 'custom_override_formatted_billing_address');
function custom_override_formatted_billing_address () {
$address = array(
'company' => $this->billing_company,
'address_1' => $this->billing_address_1,
'address_2' => $this->billing_address_2,
'city' => $this->billing_city,
'state' => $this->billing_state,
'country' => $this->billing_country,
'bank_account_no' => $this->billing_bank_account_no,
'bank' => $this->billing_bank,
'chamber_of_commerce_no'=> $this->billing_chamber_of_commerce_no,
'fiscal_code' => $this->billing_fiscal_code,
'vat_code' => $this->billing_vat_code
);
return $address;
}
Still, it did not work.
How can do I make this work?
Thanks!

You need to do this:
add_filter('woocommerce_localisation_address_formats', 'your_func')

Related

Display Woocommerce checkout custom field - checkbox - in admin order and admin email

I've setup 4 fields in the woocommerce checkout form which are displaying correctly in the edit order page and customer panel, but I'm not able to display correctly the checkbox as a yes or no (results in a 1 or 0) and can't get those custom fields in the new order admin email.
I've tried different codes to add the fields in the new order email, but all resulted in PHP errors or just didn't show the fields.
The code I've used is:
// Campi extra checkout
add_filter( 'woocommerce_billing_fields' , 'add_billing_field_extra' );
function add_billing_field_extra( $fields ) {
$new_order_fields = array();
foreach($fields as $key => $value){
if($key == 'billing_country') {
$new_order_fields['billing_fattura'] = array(
'type' => 'checkbox',
'label' => __('Vuoi la fattura?', 'woocommerce'),
'class' => array('form-row-wide'),
'clear' => true
);
$new_order_fields['billing_piva'] = array(
'label' => __('Partita Iva', 'woocommerce'),
'placeholder' => _x('Partita iva', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'show' => true
);
$new_order_fields['billing_cf'] = array(
'label' => __('Codice Fiscale', 'woocommerce'),
'placeholder' => _x('Codice Fiscale', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'show' => true
);
$new_order_fields['billing_sdi'] = array(
'label' => __('SDI', 'woocommerce'),
'placeholder' => _x('SDI', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'show' => true
);
}
$new_order_fields[$key] = $value;
}
return $new_order_fields;
}
// Aggiungere il campo Piva AMMINISTRAZIONE BACKEND
add_filter( 'woocommerce_admin_billing_fields' , 'add_admin_field_cfpiva' );
function add_admin_field_cfpiva( $fields ) {
$fields['fattura'] = array(
'label' => __('Fattura', 'woocommerce'),
'show' => true
);
$fields['piva'] = array(
'label' => __('P.IVA', 'woocommerce'),
'show' => true
);
$fields['cf'] = array(
'label' => __('Codice Fiscale', 'woocommerce'),
'show' => true
);
$fields['sdi'] = array(
'label' => __('SDI', 'woocommerce'),
'show' => true
);
return $fields;
}
function blindo_mostra_nascondi_campi_woocommerce() {
wc_enqueue_js( "
jQuery('input#billing_fattura').change(function(){
if (! this.checked) {
// HIDE IF NOT CHECKED
jQuery('#billing_piva_field').fadeOut();
jQuery('#billing_piva_field input').val('');
jQuery('#billing_cf_field').fadeOut();
jQuery('#billing_cf_field input').val('');
jQuery('#billing_sdi_field').fadeOut();
jQuery('#billing_sdi_field input').val('');
} else {
// SHOW IF CHECKED
jQuery('#billing_piva_field').fadeIn();
jQuery('#billing_cf_field').fadeIn();
jQuery('#billing_sdi_field').fadeIn();
}
}).change();
");
}

How to add a custom checkout field in specific location on WooCommerce

Im adding a custom address field in checkout with this code:
add_filter( 'woocommerce_checkout_fields' , 'checkout_address_details_fields' );
// Our hooked in function – $fields is passed via the filter!
function checkout_address_details_fields( $fields ) {
$fields['shipping']['shipping_address_details'] = array(
'label' => __('Añade más detalles a tu dirección', 'woocommerce'),
'placeholder' => _x('Bloque X Apartemento XXX (Opcional)', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['billing_address_details'] = array(
'label' => __('Añade más detalles a tu dirección', 'woocommerce'),
'placeholder' => _x('Bloque X Apartamento XXX (Opcional)', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
It is adding the field correctly, however, the field is added at the end below phone field, I need to add this custom field in the 2nd place, below the Street Address field.
You need to use the "priority" argument to set the correct location of your checkout fields like:
add_filter( 'woocommerce_checkout_fields' , 'checkout_address_details_fields' );
function checkout_address_details_fields( $fields ) {
$fields['shipping']['shipping_address_details'] = array(
'label' => __('Añade más detalles a tu dirección', 'woocommerce'),
'placeholder' => _x('Bloque X Apartemento XXX (Opcional)', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'priority' => 55, // <===== Here
);
$fields['billing']['billing_address_details'] = array(
'label' => __('Añade más detalles a tu dirección', 'woocommerce'),
'placeholder' => _x('Bloque X Apartamento XXX (Opcional)', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'priority' => 55, // <===== Here
);
return $fields;
}
It should work.

fetch all row values with same user id in php?

I am working on wordpress website and i just stuck over one sql query.
I need to get the values from database table which contain same user id.
here is my table:
And when i run select query in mysql it is fetching result correct:
But i dont know how to get this result in my php template.
this is my query i am using in php:
$current_user = wp_get_current_user();
$id=$current_user->ID;
$skill1 =mysql_query("SELECT Skills FROM wp_candidate_skills WHERE user_id='{$id}' ");
I cant use get->result because it is showing error that get->result must contain object. Something like this error is.
This is what i am using after Mysql_query:
$arr = mysql_fetch_array($skill1);
print_r($arr);
And the Output is:
Wordpress code:
public static function init_fields() {
if ( self::$fields )
return;
$current_user = wp_get_current_user();
$user = $current_user->user_login;
$useremail = $current_user->user_email;
$id=$current_user->ID;
$skill1 =mysql_query("SELECT ID,user_id,Skills FROM wp_candidate_skills WHERE user_id=$id");
$skill_note = mysql_fetch_array($skill1);
$result = mysql_query("SELECT * FROM wp_candidate_details WHERE user_id='{$id}' LIMIT 1");
$expe_note = mysql_fetch_assoc($result);
$job_title = $expe_note['job-cat1'];
$user_loc = $expe_note['location_one_id'];
$row_num = mysql_query("SELECT COUNT(Skills) FROM wp_candidate_skills WHERE user_id='{$id}' " );
$row_cnt = mysql_fetch_array($row_num);
$arr = mysql_fetch_array($skill1);
print_r($arr);
mysql_free_result($skill1);
$skill_area = $skill_note['Skills'];
self::$fields = apply_filters( 'submit_resume_form_fields', array(
'resume_fields' => array(
'candidate_name' => array(
'label' => __( 'Your name', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => __( 'Your full name', 'wp-job-manager-resumes' ),
'priority' => 1,
'value' => $user
),
'candidate_email' => array(
'label' => __( 'Your email', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => __( 'you#yourdomain.com', 'wp-job-manager-resumes' ),
'priority' => 2,
'value' => $useremail
),
'candidate_title' => array(
'label' => __( 'Professional title', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => __( 'e.g. "Web Developer"', 'wp-job-manager-resumes' ),
'priority' => 3,
'value' => $job_title
),
'candidate_location' => array(
'label' => __( 'Location', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => __( 'e.g. "London, UK", "New York", "Houston, TX"', 'wp-job-manager-resumes' ),
'priority' => 4,
'value' => $user_loc
),
'resume_skills' => array(
'label' => __( 'Skills', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => false,
'placeholder' => __( 'Comma separate a list of relevant skills', 'wp-job-manager-resumes' ),
'priority' => 9,
'value' => $skill_area
),
'links' => array(
'label' => __( 'URL(s)', 'wp-job-manager-resumes' ),
'type' => 'links',
'required' => false,
'placeholder' => '',
'description' => __( 'Optionally provide links to any of your websites or social network profiles.', 'wp-job-manager-resumes' ),
'priority' => 10,
'fields' => array(
'name' => array(
'label' => __( 'Name', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => '',
'priority' => 1
),
'url' => array(
'label' => __( 'URL', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => 'http://',
'priority' => 2
)
)
),
'candidate_education' => array(
'label' => __( 'Education', 'wp-job-manager-resumes' ),
'type' => 'education',
'required' => false,
'placeholder' => '',
'priority' => 11,
'fields' => array(
'location' => array(
'label' => __( 'School name', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => ''
),
'qualification' => array(
'label' => __( 'Qualification(s)', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => ''
),
'date' => array(
'label' => __( 'Start/end date', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => ''
),
'notes' => array(
'label' => __( 'Notes', 'wp-job-manager-resumes' ),
'type' => 'textarea',
'required' => false,
'placeholder' => ''
)
)
),
'candidate_experience' => array(
'label' => __( 'Experience', 'wp-job-manager-resumes' ),
'type' => 'experience',
'required' => false,
'placeholder' => '',
'priority' => 12,
'fields' => array(
'employer' => array(
'label' => __( 'Employer', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => ''
),
'job_title' => array(
'label' => __( 'Job Title', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => ''
),
'date' => array(
'label' => __( 'Start/end date', 'wp-job-manager-resumes' ),
'type' => 'text',
'required' => true,
'placeholder' => ''
),
'notes' => array(
'label' => __( 'Notes', 'wp-job-manager-resumes' ),
'type' => 'textarea',
'required' => false,
'placeholder' => ''
)
)
),
'resume_file' => array(
'label' => __( 'Resume file', 'wp-job-manager-resumes' ),
'type' => 'file',
'required' => false,
'description' => sprintf( __( 'Optionally upload your resume for employers to view. Max. file size: %s.', 'wp-job-manager-resumes' ), size_format( wp_max_upload_size() ) ),
'priority' => 13,
'placeholder' => ''
),
)
) );
if ( ! get_option( 'resume_manager_enable_resume_upload' ) )
unset( self::$fields['resume_fields']['resume_file'] );
if ( ! get_option( 'resume_manager_enable_categories' ) || wp_count_terms( 'resume_category' ) == 0 )
unset( self::$fields['resume_fields']['resume_category'] );
if ( ! get_option( 'resume_manager_enable_skills' ) )
unset( self::$fields['resume_fields']['resume_skills'] );
}
Help me out guys! Thanks
Try this code, `$skill1 = $wpdb->get_results("SELECT ID,user_id,Skills FROM wp_candidate_skills WHERE user_id='$id'");
$count = count($skill1);
$array = array();
foreach ($skill1 as $key => $value) {
$sk = $value->Skills;
$array[] = $sk;
}
$fixId =array();
$fixId[0] ='';
for($i=0;$i<=$count-1;$i++)
{
$temp = $array[$i];
$fixId[0] = $fixId[0].$temp.',';
}
$data = $fixId[0] ;
$words = array_filter(preg_split('/\s*,\s*/', $data));
$temp = implode(', ', $words);`
It will fetch data as an array with comma inserted between two values.
Use get_results like below:
<?php
global $wpdb; //important
$result = $wpdb->get_results("SELECT Skills FROM wp_candidate_skills WHERE user_id='$id' ");
foreach($result as $row)
{
echo $row->Skills."<br>";
}
Display mysql table data in a wp page
Use the code like below:
public static function init_fields() {
if ( self::$fields )
return;
global $wpdb;
$current_user = wp_get_current_user();
$user = $current_user->user_login;
$useremail = $current_user->user_email;
$id=$current_user->ID;
$result = $wpdb->get_results("SELECT ID,user_id,Skills FROM wp_candidate_skills WHERE user_id=$id");
print_r($result);
die();
}
Try this way
$current_user = wp_get_current_user();
$id=$current_user->ID;
$skill1 =mysql_query("SELECT Skills FROM wp_candidate_skills WHERE user_id='".$id."' ");
$arr = mysql_fetch_array($skill1);
print_r($arr);

How to get the custom field value at thank you page

I adda ed the below code to function.php page and it shows the select box at checkout page fine. But how can i get the value of selected item at the thank you page.
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['billing']['point_side'] = array(
'label' => __(
'<strong>Select Where Your Points Should Go</strong>',
'woocommerce'
),
'placeholder' => _x(
'custom_field', 'placeholder',
'woocommerce'
),
'required' => true,
'clear' => false,
'type' => 'select',
'class' => array('form-row-wide'),
'options' => array(
'default1' => __('Defult1', 'woocommerce'),
'ls' => __('Left Side', 'woocommerce'),
'rs' => __('Right Side', 'woocommerce')
)
);
return $fields;
}
Any helps appreciated.

Listing city taxonomy as woocommerce chekout shipping city dropdown

I have managed to add custom checkout city dropdown field by following method
$fields['shipping']['shipping_city'] = array(
'label' => __('City', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => true,
'clear' => true,
'type' => 'select',
'class' => array('own-css-name'),
'options' => array(
'New_York_City' => __('New York City', 'woocommerce' ),
'Chicago' => __('Chicago', 'woocommerce' ),
'Dallas' => __('Dallas', 'woocommerce' )
)
);
I want to get the option values from city taxonomy so I have tried the following method but it doesn't seems to work (http://i.stack.imgur.com/dasIm.jpg)
$args = array(
'child_of' => 0,
'type' => 'product',
'taxonomy' => 'city',
'hide_empty' => 0
);
$categories = get_categories( $args );
foreach ($categories as $category) {
$cityArray[] = "'".$category->slug."' => __('".$category->name."', 'woocommerce' )";
}
$fields['shipping']['shipping_city2'] = array(
'label' => __('City', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => true,
'clear' => true,
'type' => 'select',
'class' => array('own-css-name'),
'options' => array(implode( ', ', $cityArray ) )
);
Try this .
Just replace the
For Loop with
foreach ($categories as $category) {
$cityArray[$category->slug] = $category->name;
}
Set option like this
'options' => array_values($cityArray)
Have tested the same and the results where as follows
Let me know if this worked for you.

Categories