Add field to wordpress billing fields - php

I'd like to add 2 custom fields to the user billing information in WooCommerce, one for capturing their VAT number and one for the Chamber of Commerce number. These fields need to be displayed during checkout, on the my account > billing address page and also on the WP Admin on the user page so the admin of the website/webshop can check for these values.
I prefer not to use a plugin but to use the child theme's functions.php.
Can anyone please help me with this problem? I looked around on the Wordpress Stack exchange but couldn't find a specific and up-to-date solution for my problem. Also I read the Woocommerce documentation but there it's not explained how to show the custom billing fields on the user admin page in the backend.
Thank you very much in advance!

You can achieve this by using Woocommerce filter woocommerce_checkout_fields.
Here is the code example.
add_filter( 'woocommerce_checkout_fields','checkout_extra_fields');
function checkout_extra_fields($fields){
$fields['billing']['vat_number'] = array(
'label' => __('VAT number', 'my-slug'),
'placeholder' => __('VAT number', 'my-slug'),
'required' => false,
'clear' => false,
'type' => 'text',
);
$fields['billing']['commerce_number'] = array(
'label' => __('Commerce number', 'my-slug'),
'placeholder' => __('Commerce number', 'my-slug'),
'required' => false,
'clear' => false,
'type' => 'text'
);
return $fields;
}
Also, You can get the saved values from backend by this code
$extra_fileds_vat_number = get_post_meta( wf_get_order_id($order),'_vat_number',1);
$extra_fileds_commerce_number = get_post_meta( wf_get_order_id($order),'_commerce_number',1);

Altough I didn't want to use a plugin and Nishad's answer is absolutely correct I found a free plugin (after a really long search) that does exactly what I want. It's called Flexible Checkout Fields and you can create custom fields that show up on Checkout, Woocommerce my account page and on the WP admin user profile page. I really love it!
So for anyone who encounters the same problem, you might want to try this one!

Related

WooCoomerce: Open pop-up upon clicking the terms checkbox

I've started to be a bit desperate here. So I have a check-box in the terms area of my woocommerce checkout to agree that you read our T&C. However, we are offering services that ABSOLUTELY require people to read our terms because they include health hazard information. Obviously, legally we will be fine by offering the terms and having people agree that they read them.
What I want to achieve, though, is that a pop-up opens, when that check-box is clicked. It does not have to be fancy or styled, but since this is in woocommerce I am VERY unsure about where to place what.
A simple <input type="checkbox" id="xxx"> type and javascript was my first guess, but I do not know where to put what in this case since Woocommerce seems to be a rather closed ecosystem, so neither did I find where to place my JS (too many different phps?) nor where to link the checkbox. I was so lost.
Maybe you guys have a different approach or know where to put what.
Thanks!
The below code will add an additional custom T&C checkbox in the checkout page, before the "place order" button:
// Additional terms and conditions check box in checkout page
add_action( 'woocommerce_checkout_after_terms_and_conditions', 'add_terms_and_conditions_checkbox', 20 );
function add_terms_and_conditions_checkbox() {
woocommerce_form_field( 'terms_two', array(
'type' => 'checkbox',
'class' => array( 'terms terms-two' ),
'input_class' => array('woocommerce-form__input-checkbox'),
'label_class' => array('woocommerce-form__label-for-checkbox'),
'label' => '<span>' . sprintf(
__( "I have read and accept %s…", $domain ),
'<a class="terms_link" id="terms_link">'.__( "the terms and conditions", $domain ).'</a>'
) . '</span>',
'required' => true,
), '');
}
Or you can also override ,via your theme, the template file checkout/terms.php which displays the WooCommerce terms and conditions checkbox on checkout page, to make it as you want.
In the below function you can include the related javascript / jQuery script on checkout page for your pop up opening event:
// Auto Show hide checkout shipping fields based on chosen shipping methods
add_action( 'wp_footer', 'custom_checkout_field_script' );
function custom_checkout_field_script() {
// Only on checkout page
if( is_checkout() && ! is_wc_endpoint_url() ):
// Jquery code start
?>
<script>
jQuery(function($){
// Here come your jQuery code
});
</script>
<?php
endif;
}
Code goes in functions.php file of your active child theme (or active theme).
Related: European GDPR additional checkout validation checkbox in Woocommerce

How to override woocommerce customize panel file (in folder includes)

I'm overriding Woocommerce theme. And now I have to override customize panel. There is a few woocommerce default sections in Customize panel to change some Woocommerce data, labels etc. (such as image size, number of post per page, privacy text...). And I want to add my own customize panel fields to make some data in my child theme to be easy to change (they should be placed in the same control panel sections next to Woocommerce default fields).
There is Woocommerce/Includes/Customizer/*customizer files*. So I can simply override that files in Woocommerce folder but when Woocommerce will be updated I'll lose my changes.
So I need to add my customize fields (and I think the best way is write them somewhere in my child theme maybe in functions.php but I didn't manage to do it yet) in the same Woocommerce control panel tabs and sections. (For example in section Product Catalog (that's woocommerce default section) I want to make changeable my own filter label).
Is there any way to do it?
Thanks in advance))
To add your own section with settings under WooCommerce customize panel, just add follows codes snippets in your active theme's functions.php -
add_action( 'customize_register', 'my_custom_customize_register', 99 );
function my_custom_customize_register( $wp_customize ) {
$wp_customize->add_section(
'my_wc_custom_section',
array(
'title' => __( 'My Custom Section', 'text-domain' ),
'priority' => 20,
'panel' => 'woocommerce',
'description' => '',
)
);
$wp_customize->add_setting( 'my_wc_custom_section_settings', array( 'transport' => 'postMessage' ) );
$wp_customize->add_control( 'my_wc_custom_section_settings_control',
array(
'label' => __( 'Custom Text', 'text-domain' ),
'type' => 'text',
'settings' => 'my_wc_custom_section_settings',
'section' => 'my_wc_custom_section',
'priority' => 20,
)
);
}
To display your saved field's value from your custom section settings, just use follows -
echo get_theme_mod( 'my_wc_custom_section_settings' );
Thats it.

Wordpress customizer: Add settings and controls dynamically

In order to simplify things, I have a social icon in my WP theme, that I want to enable users to modify via the WP customizer:
$wp_customize->add_setting(
'tcx_social_twitter',
array(
'default' => 'fa fa-twitter'
)
);
$wp_customize->add_control(
'tcx_social_twitter',
array(
'section' => 'tcx_social',
'label' => 'Social links',
'type' => 'text'
)
);
Then <?php echo get_theme_mod( 'tcx_social_twitter' ); ?> in my theme layout to show the icon to the end user. Everything works great so far.
Now I want to enable future adinistrators of my theme to add as many social icons, as they want. I believe this requires adding settings and controls dynamically, however I faild to find any information on this question in the official WP docs. Any help would be greatly appreciated!

Create new admin section in wordpress - for creating office-pages

Ok, I have been looking for hours and I have to say I am really lost. I am trying to create a new section in the admin section of wordpress that should enable the user to create a new custom "office" page.
There is really many office pages on the site I have been working on (over 30), each with its opening hours, map, and images. I assume the client will want to add more later (or remove them) and they would like to manage it through Wordpress. That would mean adding a section that would enable them to put in the name of the office, opening hours, images and the location and it would create a new office page. I am rather a front-end developer and I have never worked with Wordpress before. I understand the loop, etc, I have read several things about Themes and how to create them but I am seriously stuck with how to create a section in admin area that would enable page creation/deletion with certain options.
Any help is greatly appreciated, just please point me to the correct direction. Web pages, WP codex, tutorials, youtube... whatever that helps. Thanks a bunch!
You can make custom post type..
By writing Code in function.php
function function-name(){
register_post_type( 'post name',
array(
'labels' => array(
'name' => __( 'post name' ),
'singular_name' => __( 'post name' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'post name'),
'supports' => array('title','editor','author','thumbnail','comments','custom-fields'),
)
);
}
add_action('init', 'function-name');
3 steps : -
1. create office post type by using Register Post Type
2. Create office categories by using Register taxonomy
3. create a meta box by using Add Meta Box for extra fields which wordpress doesnt offer by default like (office hours )
hope it helps !

Programmatically creating a CMS/Page in Magento

I saw the following answer to the post Where are Magento static CMS blocks stored? regarding programatically using PHP generating cms/blocks in Magento.
I changed the code to the following
$newBlock = Mage::getModel('cms/page')
->setTitle('Test CMS Page Title')
->setContent('Hello I\'m a new cms page.')
->setIdentifier('this-is-the-page-url')
->setIsActive(true)
->save();
... and it works. I see a new page show up in the CMS Pages area in the backend.
What I need to add to this is the ability to set the content of the other fields in the CMS/Page. Namely:
Layout (trying to set to 1 column)
meta keyword
meta description
fields. These fields are blank currently. I so far haven't been able to figure this part out.
Thanks,
here you go:
$cmsPageData = array(
'title' => 'Test CMS Page Title',
'root_template' => 'one_column',
'meta_keywords' => 'meta,keywords',
'meta_description' => 'meta description',
'identifier' => 'this-is-the-page-url',
'content_heading' => 'content heading',
'stores' => array(0),//available for all store views
'content' => "Hello I'm a new cms page."
);
Mage::getModel('cms/page')->setData($cmsPageData)->save();
The keys of the array are the name of the fields of the cms_page table (check the db). And to know the value, I manually create the cms page I want and then see the value for this entry in the db.

Categories