Monthly Archives: May 2015

Formidable entry creation date

Quick tip to fetch entry creation date with Formidable API.
global $frm_entry,$frm_entry_meta;
$entries = $frm_entry->getAll("form_id=50"); //replace 50 with your form id
foreach($entries as $entry){
echo $entry->created_at; //entry creation date generally in format('Y-m-d H:i:s')
}

Hope this helps! Please share your experience and tips or tricks while working with FP.

Customize WooCommerce Points and Rewards

WooCommerce Points and Rewards is a fantastic extension from WC team that lets us setup reward points for customers. By default, it can be configured for users to earn points for certain actions like “account signup” & “writing a review”. My requirement was to add another action called “Points earned for first login per day”. I will share the code along with relevant comments and screenshots to showcase the implementation.
Add the below filters and actions in functions.php file of your WP theme (Child themes recommended).
Ref: API Docs for WC Points and Rewards
//Filter to add the option of points for login per day
add_filter( 'wc_points_rewards_action_settings', 'points_rewards_for_login_per_day' );
function points_rewards_for_login_per_day( $settings ) {
$settings[] = array(
'title' => __( 'Points earned for first login per day' ),
'desc_tip' => __( 'Enter the amount of points earned when a customer logs in for the first time each day.' ),
'id' => 'wc_points_rewards_login_per_day',
);
return $settings;
}
//Filter to add events description for Points Log tab
add_filter( 'wc_points_rewards_event_description', 'points_rewards_for_login_per_day_event_description', 10, 3 );
function points_rewards_for_login_per_day_event_description( $event_description, $event_type, $event ) {
$points_label = get_option( 'wc_points_rewards_points_label' );
switch ( $event_type ) {
case 'wc-add-points-login-per-day': $event_description = sprintf( __( '%s earned for first login of the day' ), $points_label );
break;
}
return $event_description;
}
//Action to capture the log in event. My requirement is to register the first log in of the day
function func_on_login($user_login, $user) {
$points = get_option( 'wc_points_rewards_login_per_day' ); //get points configured during rewards setup in admin
$lastlogindatetime = get_user_meta( $user->ID, 'last_login_time', true ); //I am persisting the value of first log in of the day in a user meta variable
if ( $lastlogindatetime != '' && (date( 'Y-m-d', time() ) != date( 'Y-m-d', $lastlogindatetime )) ) {
if ( ! empty( $points ) ) {
WC_Points_Rewards_Manager::increase_points( $user->ID, $points, 'wc-add-points-login-per-day' );
}
update_user_meta( $user->ID, 'last_login_time', time() );
} else if ($lastlogindatetime == '') {
if ( ! empty( $points ) ) {
WC_Points_Rewards_Manager::increase_points( $user->ID, $points, 'wc-add-points-login-per-day' ); //increase point on first log in of the day
}
add_user_meta( $user->ID, 'last_login_time', time() );
}
}
add_action('wp_login', 'func_on_login', 10, 2);

Also attached are two screenshots corresponding to the first two filters, one for action settings and the other for Points Log tab. The results are highlighted in boxes.WC Action settingsWC Points Log tab
Hope this helps! Please feel free to share your experiences with WooCommerce customization and if there is a better way to achieve the same results as described in this post.

Create Formidable entries from outside using nusoap web service

Nusoap makes designing web services in PHP easy. My requirement was to use Nusoap to design a web service that an external system can use to post entries in Formidable.
Nusoap can be downloaded here. Once downloaded, explode the zip file and upload *lib* folder to the project directory. We will be creating two files: postdata.php and createfpentry.php. The first file will act as the client to post requests. The latter will act as the web service server to handle post requests and insert formidable entries.
postdata.php
<?php
require_once ('lib/nusoap.php'); //imports the nusoap library
//set the post parameter
$param = array( 'fpformfieldslist' => array( 'v1' => trim($_GET['<var1>']),
'v2' => trim($_GET['<var2>']),
'v3' => trim($_GET['<var3>'])
)
);
$client = new nusoap_client('<hostname>/createfpentry.php'); //new object referring web service server
$response = $client->call('insert_fp_entry',$param); //invoke insert_fp_entry method from the server
//Process result
if($client->fault) {
//echo fault code and fault string: $client->faultcode and $client->faultstring
}
else {
echo $response; //echo response on successful web service call
}
?>

createfpentry.php
<?php
require_once ('lib/nusoap.php'); //import nusoap library
require_once( $_SERVER['DOCUMENT_ROOT'] . '/path/to/wp-load.php' ); //load WordPress from outside
$server = new soap_server; //server object
$server->register('insert_fp_entry'); //register insert_fp_entry method
function insert_fp_entry($fpformfieldslist) {
global $frm_entry, $frm_entry_meta;
$isCreated = $frm_entry->create(array(
'form_id' => 25, //replace 25 with your form id
'item_meta' => array(
152 => $fpformfieldslist['v1'], //replace 152 with your field id. You can set other fields with values equal to the different GET parameters
),
));
$result = 'Entry successfully created'; //you can make conditional edits to the response
return $result;
}
$server->service($HTTP_RAW_POST_DATA);
exit();
?>

The URL to access the web service is http://<host>/postdata.php?v1=test1&v2=test2&v3=test3. This will create an entry in formidable backend. The parameter values should be escaped for special characters.

Hope this helped! I shall be posting more on this subject in future posts. Please share your experiences and let me know if there is a better way to achieve similar results.