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.
Related
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;
}
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.
In Woocommerce, I have added the following code to my theme's functions.php file to remove the zip/state fields in /my-account/edit-address/billing:
add_filter('woocommerce_address_to_edit', 'reorder_woocommerce_address_fields', 10, 2);
function reorder_woocommerce_address_fields( $address, $load_address) {
$new_address['billing_first_name'] = $address['billing_first_name'];
$new_address['billing_last_name'] = $address['billing_last_name'];
$new_address['billing_email'] = $address['billing_email'];
$new_address['billing_phone'] = $address['billing_phone'];
$new_address['billing_email'] = array(
'label' => __('Email', 'woocommerce'),
'required' => true,
'class' => array('form-row-first'),
);
$new_address['billing_phone'] = array(
'label' => __('Phone', 'woocommerce'),
'required' => true,
'class' => array('form-row-last'),
'clear' => true
);
$new_address['billing_company'] = $address['billing_company'];
$new_address['billing_country'] = $address['billing_country'];
$new_address['billing_city'] = $address['billing_city'];
$new_address['billing_address_1'] = $address['billing_address_1'];
return $new_address;
}
It is based on "Customizing my-account addresses fields in Woocommerce 3" answer code and it removes the fields…
But there is still an error message saying that the state and zip fields are required. So I added this:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $address_fields ) {
global $woocommerce;
$country = $woocommerce->customer->get_country();
if($country !== 'US'){
$address_fields['state']['required'] = false;
$address_fields['postcode']['required'] = false;
}
return $address_fields;
}
Now address is saved normally but when I am being redirected to /my-account/edit-address/, I see only this:
Where:
Konstantine - is billing_first_name
Berulava - is billing_last_name
Odelia Studio - is billing_company
Digomi - is billing_address_1
Tbilisi - is billing_city
I don't see labels and some fields also are missing.
Any suggestions on what to do?
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.
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.