Billing First Name Should Be Alphabetic - php

add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields( $fields )
{
$fields['billing_first_name']['required'] = false;
return $fields;
}
I want to make First and Last name should be alphabetic only. Right now it's accepting numbers also.
Help will be much appreciated. I also want to limit text of review in cart.
Regards

$('#billing_first_name').on('input',function(){
var node = $(this);
node.val(node.val().replace(/[^a-z]/g,'') ); }
);
Adding this jQuery will work.! You can add for others fields too.

You have to add fields by custom method then use follow script for alphabetic textbox:
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
function custom_override_checkout_fields($fields)
{
$fields['billing']['billing_first_name'] = array(
'label' => __('First name', 'woocommerce'),
'placeholder' => _x('First name', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'text',
'class' => array(
'alpha'
)
);
and same for billing_last_name,
use all of scripts in active theme functions.php
Actually woo-commerce does not give us this functionality. You have to use your custom jquery or javascript. This might help you
You should call this script under wp_head hook
add_action('wp_head', ' alphabet_only ');
function alphabet_only() {
?>
<script type="text/javascript">
$('.alpha').bind('keyup blur',function(){
var node = $(this);
node.val(node.val().replace(/[^a-z]/g,'') ); }
);
</script>
<?php
}
That's it.

Related

PHP : How to change dropdown list to Text ? [Checkout-Page WooCommerce]

WooCommerce on CheckOut-Page , the default of ['billing_state']shows in drop-down list which I want to change it to input[text]. The customer have to key-in the information by themselves.
please help, thanks a lot
this code doesn't work for me. (after the customer key-in the state, it shows the state-code. It should show the state name that the customer-key-in)
add_filter( 'woocommerce_checkout_fields' ,
'y_change_address_input_type', 10, 1 );
function y_change_address_input_type( $fields ) {
$fields['billing']['billing_state']['type'] = 'text';
return $fields;
}
thank you for your comment, currently, I found the solution. I can switch from selection[option] to input[text] on hook woocommerce_default_address_fields, and the code work as I expected. (I don't know why the code will not work on woocommerce_checkout_fields. The code should have work on both woocommerce_default_address_fields and woocommerce_checkout_fields)
This is the work:
add_filter( 'woocommerce_default_address_fields', 'y_edit_state', 40, 1 );
function y_edit_state( $state_fields ) {
$state_fields ['state'] = array(
'label' => 'stae', // Add custom field label
'placeholder' => 'stae', // Add custom field placeholder
'required' => true, // if field is required or not
'class' => array( 'form-row-wide', 'address-field' ), // add class name
'clear' => false, // add clear or not
'type' => 'text', // add field type
'priority' => 80, // Priority sorting option
);
return $state_fields;
}

WooCommerce searchable drop down list

so I've implemented a billing form with some custom fields , my drop down is :
$fields['billing_complex_name'] = array(
'label' => __('Complex Name', 'woocommerce'), // Add custom field label
'placeholder' => _x('E.g Raslouw Gardens', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'select',// add field type
'options' => array(
'' => 'Please select',
'ansaarestate'=> 'Ansaar Estate',
'bangladeshheights'=> 'Bangladesh Heights',
'celticmanor'=> 'Celtic Manor',
'chantelplace'=> 'Chantel Place',
...
'class' => array('my-css'),
'priority' => 51,//
);
And then using jQuery I implement the following for changes:
jQuery(document).ready(function(){
// Your code in here
jQuery(document).on('input','#billing_complex_name', function() {
myFunc();
})
function myFunc() {
// your function code
//var phone_num = jQuery('#billing_phone').val(<?php $phone_number ?>);
var complex_name = jQuery('#billing_complex_name').val();
var suburb = jQuery('#billing_suburb').val();
if (complex_name == 'eldogleneast') {
jQuery("#billing_suburb").val('ELD');
jQuery('#billing_postcode').val('0157');
jQuery("#billing_complex_address").val('');
jQuery("#billing_postcode").prop("readonly", false);
And of course depending on the selection of the complex conditions change such as the postcode or suburb etc.
My question now is:
I need to make the drop down list searchable and have an Other selection , if Other is selected I need a text input for the user to enter in and that will be the selected value (All jQuery then will just not make any changes to the postcode or anything)
Used select2.js library to implement it

Add select2 javascript and css in Woocommerce variable subscription

I am trying to add my select2 js library and css in Woocommerce variable subscriptions.
Its adding if I do view:source but my select box or dropdown doesnt convert as select2 dropdown .. which is working for other pages with same js and css using class mindesk_select2 .. Here is my code / try.
<?php
// Showing fields for variable subscriptions
add_action('woocommerce_product_after_variable_attributes', 'show_WC_Product_Variable_Subscription_Variation_Custom_Fields', 10, 3);
// Saving fields for variable subscriptions
add_action('woocommerce_save_product_variation', 'save_WC_Product_Variable_Subscription_Variation_Custom_Fields', 10, 2);
function show_WC_Product_Variable_Subscription_Variation_Custom_Fields($loop, $variation_data, $variation) {
// Mindesk Licence
$mindesk_license = get_post_meta($variation->ID, 'mindesk_license', true);
woocommerce_wp_select([
'id' => "mindesk_license{$loop}",
'name' => "mindesk_license[{$loop}]",
'wrapper_class' => 'product_custom_field form-row ',
'class' => 'mindesk_select2',
'label' => __('Mindesk License', 'woocommerce'),
'value' => $mindesk_license,
'options' => [
'' => __('Select a value', 'woocommerce'),
'fixed' => __('Fixed', 'woocommerce'),
'floating' => __('Floating', 'woocommerce'),
'network' => __('Network', 'woocommerce')
]
]);
}
function add_admin_scripts($hook) {
global $post;
if ($hook == 'post-new.php' || $hook == 'post.php') {
if ('product' === $post->post_type) {
wp_register_style('mindeskselect2csss', MINDESK_PLUGIN_URL . 'assets/css/select2.min.css');
wp_enqueue_script('mindeskselect22', MINDESK_PLUGIN_URL . 'assets/js/select2.min.js', array('jquery'), null, true);
wp_enqueue_style('mindeskselect2csss');
//wp_enqueue_script('mindeskselect22');
wp_register_style('mindeskwcvariablesubscriptionstyle', MINDESK_PLUGIN_URL . 'assets/css/custom.css');
wp_enqueue_script('mindeskwcvariablesubscriptionscript', MINDESK_PLUGIN_URL . 'assets/js/custom.js', array('jquery'), null, true);
wp_enqueue_style('mindeskwcvariablesubscriptionstyle');
}
}
}
add_action('admin_enqueue_scripts', 'add_admin_scripts', 10, 1);
As you can see i have enqued css and js files and trying to use mindesk_select2 and here is my custom.js file.
custom.js
jQuery(document).ready(function ($) {
$(".mindesk_select2").select2({
allowClear: true,
placeholder: "",
});
});
I have checked all the css and js are called and executed but my dropdown box doesnt work as select2 ...
I have also checked if any js console error there but there are no errors there as well..
Can someone guide me how can I achieve this from here. ..
Any guidance will be so appreciated.
Thanks
Adding CSS class wc-enhanced-select in the element will enable the select2 for the filed. It's already handled in the WooCommerce core.

Billing fields does not add required

I have a code below, it adds the field with label but does not add required field not sure why.
add_filter('woocommerce_checkout_fields',
'override_default_address_fields');
function override_default_address_fields($address_fields)
{
$address_fields['billing']['billing_address_2'] = array(
'label' => __('Mobile', 'woocommerce'),
'required' => true,
);
return $address_fields;
}
Any help is appreciated! thanks in advance
I think you're doing it wrong...
instead of ['billing_address_2'], use your own like ['billing_mobile'].
add_filter('woocommerce_checkout_fields', 'override_default_address_fields');
function override_default_address_fields( $address_fields ) {
$address_fields['billing']['billing_mobile'] = array(
'label' => __('Mobile', 'woocommerce'),
'required' => true,
);
return $address_fields;
}
with what you are doing you're overriding ['billing_address_2']. Which I think you are seeing it as a problem because you are only seeing the Mobile label.

woocommerce additional billing field

I'm trying to add an additional field in woocommerce billing part. I want "title" field to be displayed before the "name" field.
I have tried this:
// Add a new checkout field
function custom_filter_checkout_fields($fields){
$fields['billing_title_field'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_filter_checkout_fields' );
and then
function order_fields($fields) {
$order = array(
"billing_title_field",
"billing_first_name",
"billing_last_name",
"billing_email",
"billing_phone",
"billing_country",
"billing_address_1",
"billing_address_2",
"billing_postcode",
"billing_company"
);
foreach($order as $field)
{
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}
But this returns me no field I have created in the first step. I'ts clear that I'm missing a crusial part of the process, but I cannot figure out what exactly. Search on internet was no success (the methods provided there are adding fields before or after billing fields and I need to add it inside the group of billing fields).
Any help is appreciated! Thanks in advance!
add this plugin
http://phppoet.com/docs/checkout-fields/
and add your field you want
So I guess I found the answer. Actually in the first piece of code I have missed an important thing. That should look like that:
function custom_filter_checkout_fields($fields){
$fields['billing']['billing_title_field'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_filter_checkout_fields');
and then the second piece of code to set the created field to the right place.

Categories