Replace username by billing first name in Woocommerce checkout registration - php

I've tried to replace username with first name billing using the code below changed from this answer thread, but keep getting 500 error.
If I use first and last name it works but I would prefer to use first name only.
Code is as follows:
add_filter( 'woocommerce_new_customer_data', 'custom_new_customer_data', 10, 1 );
function custom_new_customer_data( $new_customer_data ){
// Complete HERE in this array the wrong usernames you want to replace (coma separated strings)
$wrong_user_names = array( 'info', 'contact' );
// get the first billing name
if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
if( ( ! empty($first_name) ) ) && in_array( $new_customer_data['user_login'], $wrong_user_names ) ){
// the customer billing complete name
$first_name = $first_name;
// Replacing 'user_login' in the user data array, before data is inserted
$new_customer_data['user_login'] = sanitize_user( str_replace( $first_name ) );
}
return $new_customer_data;
}
My question would be, how would I configure WooCommerce to generate the username by the custom fields: First Name (billing_first_name) instead of full name or username?

Try the following, to replace username by the billing firstname during checkout registration:
add_filter( 'woocommerce_new_customer_data', 'customer_username_based_on_firstname', 20, 1 );
function customer_username_based_on_firstname( $new_customer_data ){
// Complete HERE in this array the wrong usernames you want to replace (coma separated strings)
$wrong_user_names = array( 'info', 'contact' );
// get the first billing name
if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
if( ! empty($first_name) && ! in_array( $_POST['billing_first_name'], $wrong_user_names ) ){
// Replacing 'user_login' in the user data array, before data is inserted
$new_customer_data['user_login'] = sanitize_user( $first_name );
}
return $new_customer_data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Your error was coming from str_replace( $first_name ). This php function needs 3 arguments.

Related

Set name or shortened emailadress as username when registering through WooCommerce

WooCommerce has the option: " When creating an account, automatically generate an account username for the customer based on their name, surname or email. "
Currently, it takes the full email address as username. Since it's a membership platform this poses some privacy issues as they can see each other profiles and it shows their 'username' in the URL; mywebsite.com/user/theiremailadress).
How can I use their name+surname or only the beginning of their email? (so nothing after #)
I've already seen a few posts about this topic, like this one, but those are the other way around and not sure how to get this outcome.
Thanks in advance!
Found a solution here that worked for me:
add_filter( 'woocommerce_new_customer_data', 'custom_new_customer_data', 10, 1 );
function custom_new_customer_data( $new_customer_data ){
// get the first and last billing names
if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
if(isset($_POST['billing_last_name'])) $last_name = $_POST['billing_last_name'];
// the customer billing complete name
if( ! empty($first_name) || ! empty($last_name) )
$complete_name = $first_name . ' ' . $last_name;
// Replacing 'user_login' in the user data array, before data is inserted
if( ! empty($complete_name) )
$new_customer_data['user_login'] = sanitize_user( str_replace( ' ', '-', $complete_name ) );
return $new_customer_data;
}

Gravity forms limit submission amount

so i currently have gravity forms installed with a number of different forms. I want users to be able to see all the forms but only able to submit a maximum of 3.
I found Gravity Wiz Limit submissions, im able to limit the email address to being used 3 times but this is only working for 1 form, i need it to allow users to submit 3 different forms (eg. a global limitation)
https://gist.github.com/spivurno/4024361
having looked through this and then finding
add_filter( 'gpls_rule_groups', function( $rule_groups, $form_id ) {
// Update "123" to the ID of your form.
$primary_form_id = 123;
if( $form_id == $primary_form_id ) {
return $rule_groups;
}
$rule_groups = array_merge( $rule_groups, GPLS_RuleGroup::load_by_form( $primary_form_id ) );
foreach( $rule_groups as $rule_group ) {
$rule_group->applicable_forms = false;
}
return $rule_groups;
}, 10, 2 );
and
add_filter( 'gpls_apply_limit_per_form', '__return_false' );
it looks like its possible but how can i implement this ?
Is it for a user or guest or both? By user you could create a user meta and then use the gform_after_submission to add to the meta. Then use a pre_render to check and see if they are maxed out, and redirect them if they are.
add_action( 'gform_after_submission_{form_id}', 'after_submit_{form_id}', 10, 2 );
function after_submit_{form_id}( $entry, $form, $field ) {
$user_id = get_current_user_id();
$meta_key = 'count_submissions';
$get_meta = get_user_meta($user_id, $meta_key, true);
$qty = $get_meta + 1;
update_user_meta( $user_id, $meta_key, $qty );
}
If you need both, you might just create a database table and collect IPs and a submission count. Then update that on submission.

Change New Order email notification from name in Woocommerce

Email notification of New order process has 'My Blog' title.
I look into Woocommerce setting but could not find it.
Any Idea how to change 'My Blog' to 'X Company'
red underline text in attached images.
Plateform: Wordpress + Woocommerce
Update:
What you want to change is the "From name" and it can be changed using:
add_filter('woocommerce_email_from_name', 'change_new_order_email_from_name', 10, 2 );
function change_new_order_email_from_name( $from_name, $email ){
if( $email->id === 'new_order' )
$from_name = __("ACME corp");
return $from_name;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Addition: To add custom placeholders for the email subject (for woocommerce 3.2+):
// Only for woocommerce versions 3.2 + (up to 3.2)
add_filter( 'woocommerce_email_format_string' , 'custom_email_format_string', 20, 2 );
function custom_email_format_string( $string, $email ) {
// Get the instance of the WC_Order object
$order = $email->object;
// Additional wanted placeholders in the array of find / relace pairs
$additional_placeholders = array(
'{shop_company}' => __("ACME corp"),
);
// return the clean string with new replacements
return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Then in your email settings, on the Subject field of an email notification you will be able to replace for example:
Your {site_title} order receipt from {order_date}
by
Your {shop_company} order receipt from {order_date}
Please use this WooCommerce hook woocommerce_email_subject_new_order to change new order email title.
add_filter( 'woocommerce_email_subject_new_order', 'customizing_new_order_subject', 10, 2 );
function customizing_new_order_subject( $formated_subject, $order ){
// Get an instance of the WC_Email_New_Order object
$email = WC()->mailer->get_emails()['WC_Email_New_Order'];
// Get unformatted subject from settings
$subject = $email->get_option( 'subject', $email->get_default_subject() );
// Loop through order line items
$product_names = array();
foreach( $order->get_items() as $item )
$product_names[] = $item->get_name(); // Set product names in an array
// Set product names in a string with separators (when more than one item)
$product_names = implode( ' - ', $product_names );
// Replace "{product_name}" by the product name
$subject = str_replace( '{product_name}', $product_names, $subject );
// format and return the custom formatted subject
return $email->format_string( $subject );
}
For more details see this link
Go to
WooCommerce > Settings > Emails > Processing Orders.
Here you will find a field called "Email Subject". Here, change {site_title} to whatever you want it to appear.
Alternatively, if you want to change the value of {site_title} itself, then head over to Settings > General.
Here you will find the field called Site Title. Change it to whatever you want it to appear.
Let me know if it works!
I found the solution after digging, its very easy.
WooCommerce>Setting>Emails
At the bottom, there is section where you can header and footer text.
Simple.
Really appreciate your help #LoicTheAztec

Stripping Formatting on Contact Form 7 fields

Is it possible to remove certain characters once the user has submitted the form? For example the user selects £10,000 and it would be stripped to 10000 when submitted and sent via email.
TIA
Here's the answer
// define the wpcf7_posted_data callback
function action_wpcf7_posted_data( $array ) {
//'amount' is the name that you gave the field in the CF7 admin.
$amount = $array['amount'];
if( !empty( $amount ) ){
$array['amount'] = preg_replace('/[\£,]/', '', $array['amount']);
}
return $array;
};
add_filter( 'wpcf7_posted_data', 'action_wpcf7_posted_data', 10, 1 );

How can I store the values from a WPForm in WordPress to a MySQL database?

How can I store the values from a WPForm in WordPress to a MySQL database?When we use WPForms where I have to add PHP part to store data? Can we store form data to tables when we are using free version of WPForms without buying it?
If you are using WP Forms Lite you can't do this directly, either you'll need to upgrade to PRO version or build your own custom save actions.
If you decide to go with your own custom version, some details below on how to intersect form submission on WP Forms.
WPForms has some actions you can use to do you own custom actions after form submission:
wpforms_process_complete
do_action( 'wpforms_process_complete', $this->fields, $entry, $form_data, $entry_id );
By using on your own template or plugin the wordpress hook add_action for any of the events described, you are able to get form data and do the handling you need.
Reference for wordpress add_action can be seen on the official documentation.
https://developer.wordpress.org/reference/functions/add_action/
Did a quick snippet that will help you get started:
add_action("wpforms_process_complete", 'function_save_custom_form_data');
function function_save_custom_form_data($params) {
foreach($params as $idx=>$item) {
$field_name = $item['name'];
$fiel_value = $item['value'];
// Do whatever you need
}
return true;
}
Please let me know if you need any further details.
WPForms store all form data in two tables within the native WordPress database. They are:
wp_wpforms_entries: In this table, the field values for entries are stored.
wp_wpforms_entry_meta: This table contains meta information about your entries such as IDs associated and the date that entries were submitted.
After publishing the form, make sure to add a form entry, so we can access the entry from your WordPress dashboard. Additionally, in your form builder, go to Settings » General and make sure that entry storing in WordPress is not disabled.
Create custom tables for example wpforms_entries and wpforms_entry_meta in your database to store form data. Use the provided action hook wpforms_process_complete to store the form entries.
add_action( 'wpforms_process_complete', 'process_entry', 5, 4 );
function process_entry( $form_fields, $entry, $form_data, $entry_id ) {
global $wpdb;
$form_id = $form_data['id'];
$entry_data = array(
'form_id' => $form_id,
'status' => 'publish',
'referer' => $_SERVER['HTTP_REFERER'],
'date_created' => current_time( 'mysql' )
);
// Insert into wpforms_entries custom table.
$success = $wpdb->insert( $wpdb->prefix . 'wpforms_entries', $entry_data );
$entry_id = $wpdb->insert_id;
// Create meta data.
if ( $entry_id ) {
foreach ( $form_fields as $field ) {
$field = apply_filters( 'wpforms_process_entry_field', $field, $form_data, $entry_id );
if ( isset( $field['value'] ) && '' !== $field['value'] ) {
$field_value = is_array( $field['value'] ) ? serialize( $field['value'] ) : $field['value'];
$entry_metadata = array(
'entry_id' => $entry_id,
'meta_key' => $field['name'],
'meta_value' => $field_value,
);
// Insert entry meta.
$wpdb->insert( $wpdb->prefix . 'wpforms_entrymeta', $entry_metadata );
}
}
}
}
Refrence: https://github.com/sanzeeb3/entries-for-wpforms/blob/master/includes/functions-wpfe-core.php#L59
Alternatively, the plugin itself is available: https://wordpress.org/plugins/entries-for-wpforms/
Free version of WPForms does not save the entry details captured in form. Therefore, you will need to write custom code to save the values in your custom DB table and then display them.
Below hook of WPForms can be used for saving data being entered in WPForms
/*hook to save entries coming from WPforms into database*/
add_action( 'wpforms_process_entry_save', array( $this, 'ank_wpforms_save_entries' ), 10, 4 );
public function ank_wpforms_save_entries( $fields, $entry, $form_id, $form_data ) {
//no need to sanitize data coming from WPForms as it is being sanitized in WPForms plugin before this hook using
//wpforms_process_validate_{$field_type} in class-process.php
$data = array();
$data['form_id'] = $form_id;
$data['entry_details'] = $fields;
//additional sanity checks are also performed while json encoding in "add" before adding in database
ank_wpforms_entry()->get_class_instance( 'entry-db' )->add( $data );
}
Alternatively , You can use this free plugin to save entries coming from WPForms into wordpress database and then display them in Wordpress Dashboard - https://wordpress.org/plugins/add-entries-functionality-to-wpforms/

Categories