Adding a new custom shipping field in woocommerce - wordpress

I'd like to add a new custom billing field in my woocommerce checkout. After adding the following code inside my child theme functions.php, saved it and refreshed the page, I received a blank page. There was no error message. I believe there must be syntax error or something.
add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['shipping']['about_yourself'] = array(
'label' => __('About yourself', 'woocommerce'),
'placeholder' => _x('About yourself', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide');
'clear' => true
);
return $fields;
}
I was wondering if anyone could help me out.
Thank you in advance

The error is in your array field that should be like:
$fields['shipping']['about_yourself'] = array(
'label' => __('About yourself', 'woocommerce'),
'placeholder' => _x('About yourself', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
NOTE: During development process to enable the error_reporting() and set WP_DEBUG true. This is how you enable it during WordPress development in wp-config.php file
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
#ini_set('display_errors', 0);

You have added ; instead of , in 'class' => array('form-row-wide');
So your code would be :
add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['shipping']['about_yourself'] = array(
'label' => __('About yourself', 'woocommerce'),
'placeholder' => _x('About yourself', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}

Related

getting error 500 after trying to add a tab to wordpress customizer

i'm trying to add a tab to my wordpress customizer.
i use the next code:
add_action('customize_register','theme_costumizer_register');
function theme_costumizer_register($wp_customize){
$wp_customize->add_section('facebook_new_section', array(
'title' => 'Social Media',
'priority' => 10
));
$wp_customize->add_settings('facebook',
array(
'default' => 'http://facebook.com/ahiad'
));
$wp_customize->add_control(new WP_Customize_Control($wp_customize,'txtFacebookAddress',
array(
'label' => 'Facebook Link',
'section' => 'facebook_new_section',
'type' => 'text'
)));
}
the problem is every time i run this piece of code in my website, i get an error 500, could not spot the problem here...
You are using $wp_customize->settings which is not not exists, Please use $wp_customize->setting.
Please try below code :
add_action('customize_register','theme_costumizer_register');
function theme_costumizer_register($wp_customize){
$wp_customize->add_section('facebook_new_section', array(
'title' => 'Social Media',
'priority' => 10
));
$wp_customize->add_setting('facebook',
array(
'type' => 'theme_mod', // or 'option'
'capability' => 'edit_theme_options',
'theme_supports' => '', // Rarely needed.
'default' => 'http://facebook.com/ahiad',
'transport' => 'refresh', // or postMessage
'sanitize_callback' => '',
'sanitize_js_callback' => '',
));
$wp_customize->add_control('facebook',
array(
'label' => 'Facebook Link',
'section' => 'facebook_new_section',
'type' => 'text'
));
}
I hope it will helps you. Thanks

How to get the value instead of order_id with get_post_meta()

I've had the following code to add a custom order field in the woocommerce checkout:
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['billing']['billing_reason_for_purchase'] = array(
'label' => __('Reason for purchase', 'woocommerce'),
'placeholder' => _x('Reason for purchase', 'placeholder', 'woocommerce'),
'required' => false,
'type' => 'select',
'class' => array('form-row-first'),
'options' => array(
'option_1' => __('Personal', 'woocommerce'),
'option_2' => __('Academic', 'woocommerce'),
'option_3' => __('Small Business', 'woocommerce'),
'option_4' => __('Large Organization', 'woocommerce')
)
);
return $fields;
}
Then, followed by the below code to 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(!empty($_POST['billing_reason_for_purchase'])) {
update_post_meta($order_id,'Reason for purchase',sanitize_text_field($_POST['billing_reason_for_purchase']));
}
}
Next, display the field on the order edit page:
add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta',10,1);
function my_custom_checkout_field_display_admin_order_meta($order){
//echo '<p><strong>'.__('Reason for purchase').':<strong>'.get_post_meta($order->id, 'Reason for purchase',true).'</p>';
echo '<p><strong>'.__('Reason for purchase').':<strong>'.get_post_meta(get_the_ID(), 'Reason for purchase',true).'</p>';
}
The problem I'm having is if I created a dummy order with choosing "academic" as the reason for purchase, I'd get "option_2" instead of "academic" in the order edit page.
Please help point me in the right direction.
It happens because the value of the selected option (and so of $_POST['billing_reason_for_purchase']) is actually the key of the array (in your example option_2) and not the text related. In fact this is the option tag created:
<option value="option_2">Academic</option>
You're saving only the key of your array.
So you need to retrieve the array of options even on the my_custom_checkout_field_display_admin_order_meta function to get the proper text.
You could copy the array in each function (but is not convenient to duplicate code), or put it in a global variable to be able to access it from anywhere (but then something else could change it), so instead you can use a function that returns the array:
function reasons_for_purchase () {
return array(
'option_1' => __('Personal', 'woocommerce'),
'option_2' => __('Academic', 'woocommerce'),
'option_3' => __('Small Business', 'woocommerce'),
'option_4' => __('Large Organization', 'woocommerce')
);
}
And then use it where you need it:
add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['billing']['billing_reason_for_purchase'] = array(
'label' => __('Reason for purchase', 'woocommerce'),
'placeholder' => _x('Reason for purchase', 'placeholder', 'woocommerce'),
'required' => false,
'type' => 'select',
'class' => array('form-row-first'),
'options' => reasons_for_purchase()
);
return $fields;
}
add_action('woocommerce_admin_order_data_after_billing_address','my_custom_checkout_field_display_admin_order_meta',10,1);
function my_custom_checkout_field_display_admin_order_meta($order){
$reasons = reasons_for_purchase();
$reason = get_post_meta($order->id, 'Reason for purchase', true);
if( isset($reasons[$reason]) )
echo '<p><strong>'.__('Reason for purchase').':</strong> '. $reasons[$reason] .'</p>';
}

Woocommerce new fields

How can I add 2 new fields on the same row? Now They are on 2 rows.
add_filter('woocommerce_checkout_fields','reorder_woo_fields');
function reorder_woo_fields($fields) {
$fields2['billing']['billing_ico'] = array(
'label' => __('ICO', 'woocommerce'),
'placeholder' => _x('ICO', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-first'),
'clear' => true
);
$fields2['billing']['billing_dic'] = array(
'label' => __('DIC', 'woocommerce'),
'placeholder' => _x('DIC', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-last'),
'clear' => true
);
return $fields2;
}
Cheers.
Use your class names form-row-first & form-row-last in CSS and set them to a specified width and display inline.

woocommerce Customising checkout fields

I am trying to customise the woocommerce checkout page. The below works fine:
$fields['billing']['billing_city'] = array(
'label' => __('Town / City:', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'label_class' => array('infotown'),
'clear' => true,
'placeholder' => _x('', 'placeholder')
);
Where as the following does not:
$fields['billing']['billing_state'] = array(
'label' => __('State:', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'label_class' => array('infocounty'),
'clear' => true,
'placeholder' => _x('', 'placeholder')
);
There's no difference between the 2, apart from the label, and the label class.
Any ideas?
try adding
- woocommerce language support, which is for translators
- check the add_action function in which you are adding it too
- spacing can be a problem in the wrong places also
to add language support, try so
$fields['billing']['billing_state'] = array(
'label' => __('State:', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'label_class' => array('infocounty'),
'clear' => true,
'placeholder' => _x('', 'placeholder', 'woocommerce')
);
where i added "woocommerce" next to placeholder. :)

How to add a select custom field to checkout page in WooCommerce?

I'm adding extra fields to the checkout page in WooCommerce,
I've added basic text fields ok, but I want a dropdown or select box with a few options,
Here is what I've done so far but I've made an error somewhere
$fields['billing']['billing_meat'] = array(
'label' => __('Food options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false
'type' => 'select',
'options' => array( // array of key => value pairs for select options
__('I eat meat', 'woocommerce') => __('I eat mate', 'woocommerce'),
__('meat is gross', 'woocommerce') => __('meat is gross', 'woocommerce'),
Maybe I'm not defining 'type' field correctly?
thanks loads
If that is your exact code, then the problem is that you are missing a comma after 'clear' => false.
I've tested this and it works:
$fields['billing']['billing_meat'] = array(
'label' => __('Food options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'select',
'options' => array(
'eat-meat' => __('I eat maet', 'woocommerce' ),
'not-meat' => __('Meat is gross', 'woocommerce' )
)
);
Note that I also did not use __() on the options array keys. It's better to not translate those.
this is working. here's my code
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'dropdown' );
// Our hooked in function - $fields is passed via the filter!
function dropdown( $fields ) {
$fields['billing']['dropdown'] = array(
'label' => __('dropdown', 'woocommerce'),
'placeholder' => _x('dropdown', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'type' => 'select',
'options' => array(
'option 1' => __('option 1', 'woocommerce' ),
'option 2' => __('option 2', 'woocommerce' )
)//end of options
);
return $fields;
}
this works. but it seems that the value is not being saved under _billing_dropdown. I used "admin columns" plugin to add a field in the order table (admin side). under my custom field "dropdown", no value shows up. where can i retrieve the value? same goes with my other custom field "purpose" that is under "_billing_purpose" and is type=>'textarea'. thanks!
here's a screenshot
https://scontent-b-hkg.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/10696182_985415141473105_5302358697439449940_n.jpg?oh=74cb5ebc6b1ad6dd0c29e51293b61fdf&oe=5502613C

Resources