Gravity forms limit submission amount - php

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.

Related

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 );

Get page ID in user_register hook Wordpress

in my site I have 2 differents forms for registration. The difference between them that is when I use the second I update a meta info for a user that normally is blank.
I've added the Facebook SSO and works fine only for the first form.
When I use it for the second it doesn't recognize the origin and register the user as the first one (without update meta info).
I've tried to use the register_user hook
function myplugin_registration_save( $user_id ) {
global $post;
if ($post->ID == 816) {
update_user_meta($user_id, 'user_type', 'couple');
}
}
add_action( 'user_register', 'myplugin_registration_save', 999 );
but this hook doesn't return the post ID.
How can I detect I'm in the second form and launch the update_user_meta function when the post id is 816?
Thanks
function myplugin_registration_save( $user_id ) {
$currentPageId = get_the_ID();
if ($currentPageId == 816) {
update_user_meta($user_id, 'user_type', 'couple');
}
}
add_action( 'user_register', 'myplugin_registration_save', 999 );
Try this one it will surely give you the current page id..

how to create unique reference number - wordpress

I have Suppliers which have their own products. WP backend is almost redesigned and there I have a page(with form) where 'admin' can add new supplier and I need to create unique a reference number for each supplier when form will be submitted.
Plus, I have a "Sort by" dropdown and one of the sorting options is by "Reference number".
At first, I thought to use the POST ID as reference number, but don't think that this can be best solution, as POST IDs will be different when some posts will be removed. Also I was thinking to use uniqid() function with some digit limit and only digits.
What is best to reach this? Any ideas?
You can specify a new custom meta field for (i.e. supplier_id) and create a function which ensure that this supplier_id is unique. This function will be executed every time when a supplier form submitted.
The action hook save_post is triggered whenever a post or page is created or updated. So we can use it for this purpose.
From the documentation:
save_post is an action triggered whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email. The data for the post is stored in $_POST, $_GET or the global $post_data, depending on how the post was edited. For example, quick edits use $_POST.
Since this action is triggered right after the post has been saved, you can easily access this post object by using get_post($post_id)
Example:
function save_supplier_id( $post_id, $post, $update ) {
$post_type = get_post_type($post_id);
if ( "supplier" != $post_type ) return;
if ( isset( $_POST['supplier_id'] ) ) {
$my_supplier_id = $_POST['supplier_id'];
if ( ! is_int( $my_supplier_id ) ) $my_supplier_id = 1;
$all_other_suppliers = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'supplier',
'post__not_in' => array( $post_id )
));
$all_other_ids = array_map( function( $supplier ) { return $supplier->ID; }, all_other_suppliers );
if ( count( $all_other_ids ) && in_array( $my_supplier_id, $all_other_ids ) ) {
// ID is already in use by another supplier, let's create an new one
$my_supplier_id = max( $all_other_ids ) + 1;
}
update_post_meta( $post_id, 'supplier_id', $my_supplier_id ) );
}
}
add_action( 'save_post', 'save_supplier_id', 10, 3 );
Explantion:
The format for supplier_idis simple a consecutive number. If the provided id is not an integer, we set it to 1. Now we get all other supplier id's and check if the give id no occur twice. If so, the we get the max id and increase it by 1.

Link between Gravity Forms and Woocommerce APIs

I have a Woocommerce site, and I use Gravity Forms to further expand each order.
I am coding a management tool that consumes both APIs to make some statistics and other administration tools.
I can get a list of the Gravity Forms entries, and also a list of the orders. The problem I have is that I don't know how can I get the entry that is related to a particular order.
Is there a way to do this?
have you tried with the woocomerce history plugin or fetching the raw metadata out the item¿?
wc_get_order_item_meta($order_item_id, "_gravity_forms_history");
wc_get_order_item_meta($order_item_id, "_gravity_form_data");
keep in mind that this will require a new endpoint to be created is not put of the box.
The last time I worked with the WooCommerce Gravity Forms Product Addons (a year or so ago) it did not store the order ID in the entry (would have to happen after the entry is created and after the order is created), or the entry ID in the order. The latter probably makes more sense but both would require custom code.
Again, it's been some time since I worked with the add-on. I'd ping WC support and see if they any tips on implementing support for this.
This is where I found the link between WooCommerce and the gravity forms product addon:
In the database, find the order in the table your_table_prefix_posts, and grab its ID. I was filtering for the post_type "shop_order."
In the table your_table_prefix_woocommerce_order_items, find the ID just found and filter for "line_item" in the "order_item_type" column, and grab the "order_item_id."
Use that "order_item_id" to find the order's meta in the table your_table_prefix_woocommerce_order_itemmeta.
All of the order's gravity forms data is in there. It looks like there is no actual tie between what woo does and gravity, except that the form is filled out and it data is grabbed and stuck into your_table_prefix_woocommerce_order_itemmeta. I cannot find anything that ties a particular order to a particular gf entry, but you can get the data from Woo, and at least use that to search GF entries.
I was able to do this using Gravity Forms, Gravity Forms Product Addons, and Woocommerce using a three step process:
STEP 1: Get GF Entry ID/s as the entry is made and store it in a session. This happens before checkout is complete and sometimes before the user is logged in.
add_action( 'gform_after_submission', 'blb_get_lead_entry_id', 10, 2 );
function blb_get_lead_entry_id( $entry, $form ) {
$meta_value = $entry['id'];
//session array with all the entries because GF creates 2 entries each time for validation purposes apparently
if (!isset($_SESSION['entryclump'])) {
$_SESSION['entryclump'] = array();
}
$_SESSION['entryclump'][] = $meta_value;
//also an array with just the current entry which will end up be the later of the two entries added by GF
$_SESSION[ 'gf-entry-id' ] = $meta_value;
}
STEP 2: Include the entry you just gathered ($_SESSION[ 'gf-entry-id' ]) with the Cart Item Meta and then save it.
In this case, i am saving a field called "_gf_entry_ID" which will get added to the woocommerce_order_itemmeta table with the correct order_item_id and the later of the two GF entries as the meta_value.
//add cart item data
add_filter( 'woocommerce_add_cart_item_data', 'blb_add_gfentry_to_cart_data', 10, 3 );
function blb_add_gfentry_to_cart_data( $cartItemData, $productId, $variationId ) {
$entryid=$_SESSION[ 'gf-entry-id' ];
$cartItemData['GFentryID'] = $entryid;
return $cartItemData;
unset($_SESSION[ 'gf-entry-id' ]);
}
//add cart item data: session stuff
add_filter( 'woocommerce_get_cart_item_from_session', 'blb_cart_item_session', 10, 3 );
function blb_cart_item_session( $cartItemData, $cartItemSessionData, $cartItemKey ) {
if ( isset( $cartItemSessionData['GFentryID'] ) ) {
$cartItemData['GFentryID'] = $cartItemSessionData['GFentryID'];
}
return $cartItemData;
}
//save the data
add_action( 'woocommerce_add_order_item_meta', 'blb_save_gfentry', 10, 3 );
function blb_save_gfentry( $itemId, $values, $key ) {
if ( isset( $values['GFentryID'] ) ) {
wc_add_order_item_meta( $itemId, '_gf_entry_ID', $values['GFentryID'] );
}
}
STEP 3 (optional): Update the GF Form to reflect the correct created_by user ID. Now that checkout is complete and the user is logged in, we can do that.
function blb_woocommerce_thankyou( $order_id ) {
//Current User
$currentUserID = wp_get_current_user()->ID;
//GF Entry Array for Order
$order = new WC_Order( $order_id );
$items = $order->get_items();
$order_item_ids = array();
$gf_entry_ids = array();
foreach ( $items as $key=>$item ) {
$gf_entry_ids[] = $item['item_meta']['_gf_entry_ID'][0];
}
//First real quick clear all the entries in the entry clump (in case the user was already logged in)
//This is important because GF creates two forms for product add ons with Woocommerce and we only want one to show up in the list and edit plugin
$entryclump = $_SESSION[ 'entryclump' ];
foreach ( $entryclump as $entry ) {
global $wpdb;
$wpdb->update('wp_rg_lead', array('created_by' => null), array('id' => $entry));
}
//Update wp_rg_lead
if (($currentUserID!=0) && (isset($_SESSION[ 'entryclump' ])) ) {
foreach ( $gf_entry_ids as $gf_entry_id ) {
global $wpdb;
$wpdb->update('wp_rg_lead', array('created_by' => $currentUserID), array('id' => $gf_entry_id));
} //foreach
} //if
unset($_SESSION[ 'entryclump' ]);
};
add_action( 'woocommerce_thankyou', 'blb_woocommerce_thankyou', 10, 1 );

Wordpress updating screen_layout_post user meta for 1 column layout

I am trying to get Wordpress to show a 1 column layout when adding a new post using the following code in my functions file.
function wpsnippy_one_columns_posts_layout( $columns ) {
$columns['vehicle'] = 1;
return $columns; } add_filter( 'screen_layout_columns', 'wpsnippy_one_columns_posts_layout' ); function wpsnippy_screen_layout_posts() {
return 1; } add_filter( 'get_user_option_screen_layout_post', 'wpsnippy_screen_layout_posts' );
This is working fine if logged in as admin, however I have a user type 'seller' and it is not working for seller, I actually need it to be the other way around, 1 column if seller 2 if anything else.
Your input will be greatly appreciated, many thanks.
We can filter the function get_user_option() and force 1 column:
add_filter( 'get_user_option_screen_layout_post', function( $result, $option, $user )
{
if( in_array( 'seller', $user->roles ) )
$result = '1';
return $result;
}, 10, 3 );
Note that the option is defined like: "screen_layout_$page".

Categories