Woo Commerce Fist Last Name Field restrict NUMBERS from being Used - woocommerce

I need to restrict the use of numbers in the Woo commerce check out fields for first and last name ..
and validate it contains only A-Z letter
any help would be amazing thanks in advance

Add this to your functions.php
add_action('woocommerce_after_checkout_validation', function ( $fields, $errors ) {
if (!preg_match('/^[a-zA-Z]+$/', $fields['billing_first_name'])) {
$errors->add('billing_first_name', 'First name must contain only alphabetic characters');
}
if (!preg_match('/^[a-zA-Z]+$/', $fields['billing_last_name'])) {
$errors->add('billing_last_name', 'Last name must contain only alphabetic characters');
}
}, 10, 2);

Related

wpallimport - how do I replace sku with product id?

I use a feed from a manufacturer to upload to a WooCommerce custom field.
The custom field expects a product id as input, but the feed is not aware of this as it's created by WooCommerce.
I use SKU as the key to compare existing products with the feed.
The imported field is called {metaparent_sku[1]} and the content has several values separated by | , for example:
BDCD7 | BDAUX | BDCA1 | ADUP89
These are unique product sku’s referring to products already in WooCommerce.
What I’d like to do is replace the sku value with the corresponding product ID value in as it exists already in WooCommerce.
I’ve been trying to use str_replace but can’t figure out how to set it up, if that's possible at all. Or should I use lookup?
If anyone could help with some sample code I'd be very grateful.
To find the product ID by a sku field.
[wc_get_product_id_by_sku({sku[1]})]
So in your case that would be:
[wc_get_product_id_by_sku({metaparent_sku[1]})]
For when you have multiple sku's in one field make a custom function.
Place the following function in the Function Editor.
/**
* Find product id(s) by sku(s)
* #param string $sku
* #return string with product id(s)
*/
function wp_all_import_get_product_ids_by_sku($sku, $seperator = "|") {
// define empty $product_ids
$product_ids = array();
// remove spaces from sku
$sku = str_replace(" ", "", $sku);
// convert to array
$skus = explode($seperator, $sku);
// find product ids
foreach ($skus as $sku) {
$product_ids[] = wc_get_product_id_by_sku($sku);
}
// return product ids
return implode(" | ", $product_ids);
}
Then use it like this
[wp_all_import_get_product_ids_by_sku({metaparent_sku[1]})]

Woocommerce : How to add a custom meta_key to checkout billing_phone?

I want to enter the value of digits_phone meta key to be entered as billing_phone for every woocommerce order.
I came up with something like this but it did not work :
//Automatically add the digits phone number of the user, to woocommerce orders for every order
add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data');
function dg_manipulate_checkout_posted_data ($data) {
$data['billing_phone'] =['digits_phone'];
return $data;
}
can anyone please help me to figure this out?
I have never used the plugin myself, take this as a guideline
THIS CODE IS NOT TESTED !
Maybe this question is a better fit for wordpress.stackexchange.com
From the last comment of this post I see that there should be 2 user metadata related to some digits_phone: 'digits_phone' and 'digits_phone_no'
Assuming that the one we want is digits_phone, this code should be a hint in the right direction:
add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data');
function dg_manipulate_checkout_posted_data ($data) {
// save current user data in variable $current_user
$current_user = wp_get_current_user();
//get digits phone from db and save in variable $digits_phone
$digits_phone = get_user_meta( $current_user->ID, 'digits_phone' , true );
// assign to POSTed array and return it
$data['billing_phone'] = $digits_phone;
return $data;
}
Also have a look at How can I convert woocommerce checkout fields in capital letters to get a better picture of manipulating POST data

Gravity forms: Authorize.net Invoice number

I was wondering If you knew how to add an invoice code to the forms in Authorize.net.
I check the authorize.net feed settings but they do not ask for an invoice code.Then, I started to do some research and found the hook gform_authorizenet_save_entry_id that could be used to create that invoice code.
The problem comes that there is no documentation about this hook. It was only mentioned as one of the updates. So, I'm creating a hidden field with an {entry_id} as a default value and trying to find a way to pass it as an Invoice Number.
Any help would be appreciated. Thanks :)
Update:
I was able to add a transaction code to the form using the following Snippet
//Adding the transaction code
add_filter( 'gform_authorizenet_transaction_pre_capture', 'set_invoice_number', 10, 5 );
function set_invoice_number( $transaction, $form_data, $config, $form, $entry ) {
if ( $form['id'] == 6 ) {
// your submission ID format to be inserted into the hidden field
$SubmissionID = 'RW-' . $entry['id'];
$transaction->invoice_num = $SubmissionID;
}
return $transaction;
}
I got the invoice number to become "RW-" but the $entry['id'] is not printing anything
You can assign an invoice number using an input field by adding this code to your theme's functions.php file:
add_filter('gform_authorizenet_transaction_pre_capture', 'set_invoice_number', 10, 4);
function set_invoice_number($transaction, $form_data, $config, $form)
{
$transaction->invoice_num = rgpost('input_YOUR INPUT FIELD NUMBER HERE');
}
If the input field is the hidden field containing the form's entry id that should accomplish what you're wanting.
I use the following code to append the "Form Name" to the invoice number passed to Authorize.net. I believe that the previous answer would work if the first field you create in each form was the "Invoice Number" field, but if you add the field later, or add it to your previously created forms, they wouldn't all have the same field number so it may not work. This code will use the automatically generated unique invoice number and add the form name.
i.e. Form Name: "Yearly Subscription" Auto Invioce Number: "1234567890"
Info that Authorize.net receives: "1234567890-Yearly_Subscription"
I have found that instead of adding custom functions to your theme file, it is better to build a custom plugin and add them there instead. This way if your theme updates, you won't lose your functions. The code to create your plugin, along with the functions for your Authorize.net code is included below. Save this file as My_Custom_Functions_Plugin.php and upload it to your web host in the "wp-content/plugins/" folder and then activate it. Next time you need to add any customized functions, just add them at the end of this file.
<?php
/*
Plugin Name: My_Custom_Functions_Plugin
Plugin URL: http://WEBSITE
Description: Custom Functions and Scripts for WEBSITE
Version: 1.0.0
Author: NAME
Author URI: http://WEBSITE
*/
// Functions for apending form name to authorize.net invoice
add_filter( 'gform_authorizenet_transaction_pre_capture', 'invoice_num', 10, 4 );
function invoice_num( $transaction, $form_data, $config, $form ) {
$transaction->invoice_num = uniqid() . '-' . rgar( $form_data, 'form_title' );
gf_authorizenet()->log_debug( 'gform_authorizenet_transaction_pre_capture: ' . print_r( $transaction, 1 ) );
return $transaction;}
// End of Authorize.net Function
// Add any additional Functions below this comment line to keep your themes functions.php file from getting overwritten on theme updates. Copy and paste this comment line before each function and give it a description to help keep you organized about what your function does
?>
Hope this helps!

Username only lowercase and numbers in WP multi?

We are using wordpress in multi sub-domains at work.
So we have administrator for each domain who can create users for subscribing editing contributing.
The problem is, our company edit the rule that all logins/usernames have to be like this "first-name.family-name" ex "barack.obama"... :-)
But the administrator can not do it, only super admin can do it !
In sub-domain administrator mode, there is a message that says : "Only the lowercase letters a-z and numbers allowed".
So we want to make it possible to admin to create users with first-name.family-name, but we try to search how to change it in the code, without finding it.
If someone can help, thanks !
In multisite usernames are validated with wpmu_validate_user_signup, which you can find in /wp-includes/ms-functions.php#L462.
You can add a dot there, after the 0-9, which is not recommended, since you will then hate to update WordPress core.
if ( $user_name != $orig_username || preg_match( '/[^a-z0-9.]/', $user_name ) ) {
So, a better solution would be to create a plugin for it.
You could try to add the following in a php-file and add it to /wp-content/mu-plugins/
<?php
/*
Plugin Name: wpmu no username error
*/
function wpmu_no_username_error( $result ) {
$error_name = $result[ 'errors' ]->get_error_messages( 'user_name' );
if ( empty ( $error_name )
or false===$key=array_search( __( 'Only lowercase letters (a-z) and numbers are allowed.' ), $error_name)
) {
return $result;
}
// only remove the error we are disabling, leaving all others
unset ( $result[ 'errors' ]->errors[ 'user_name' ][$key] );
/**
* re-sequence errors in case a non sequential array matters
* e.g. if a core change put this message in element 0 then get_error_message() would not behave as expected)
*/
$result[ 'errors' ]->errors[ 'user_name' ] = array_values( $result[ 'errors' ]->errors[ 'user_name' ] );
return $result;
}
add_filter( 'wpmu_validate_user_signup', 'wpmu_no_username_error' );
I didn't try it though. This is based on "Changing the username character limit from four to less characters".
I hope this helps. At least it should get you close. GL with it!

wordpress bloginfo filter name and description

I was trying to edit my wordpress site name and description, I got that I have to add a filter plugin like so: add_filter( 'bloginfo', 'My_function_name', 10, 2 );.
It works, but I can't understand why we have to add the 10, 2 parameters? and which values it belongs to?
Also there wasn't enough information in codex.wordpress.org
Here is my working plugin :
function edit_bloginfo($text,$show ){
if (isset($_COOKIE['switch_language'])) {
if($show == 'description'){
$text = 'New description';
}
if($show == 'name'){
$text = 'New site name';
}
return $text;
}
}
add_filter( 'bloginfo', 'edit_bloginfo', 10, 2 );
In add_filter( 'filter_hook', 'your_function', 10, 2 ); expression, the 10, 2 parameters are:
the first: the priority of your filter function during the execution of all filter functions. it is a number between
1 and ∞ ( use a reasonable one : ) ), many thanks to #brasofilo: look here.
the latest: the number of parameters that will be passed to your
function. The number of parameters declared and the number of parameters passed has to be the same.
Take a look to WordPress Codex Plugin API
Hope it helps!

Resources