WPLoyalty Knowledgebase
Back to WPLoyaltySupportMy Account
  • WPLoyalty Knowledgebase
  • Installation & Updates
    • Installation
    • Updates
    • License Key Validation
  • Quick Start Guides
    • Creating a Loyalty Points program
    • Creating a Referral Program
    • Creating a Customer Reward Page
  • Campaigns
    • Creating a Campaign
    • Campaign Types
    • Campaign Rules
    • Creating a Reward
  • Campaign Types
    • Points for Purchase
    • Reward for spending
    • Review Reward
    • Referral Campaign
    • Social Share Campaigns
    • Birthday Reward
    • Sign Up Reward
    • Social Pages Follow Campaign
    • Achievement Campaign
      • Custom Action
  • Reward Types
    • Points Conversion
    • Fixed Discount
    • Percentage Discount
    • Free Product
    • Free Shipping
  • Add-ons
    • WPLoyalty - Launcher
    • WPLoyalty - Points Expiry
    • WPLoyalty - Multi Currency
    • WPLoyalty - Discount Rule Integration
    • WPLoyalty - Judge.Me
    • WPLoyalty- Migration
      • Migration from WPSwings Points and Rewards
      • Migration from WooCommerce Loyalty Points & Rewards
      • Migration from WooCommerce Points and Rewards
    • WPLoyalty - Guest Referral
  • WPLoyalty - Brand Condition
    • WooCommerce Brands
    • Perfect Brands for WooCommerce
    • Page
  • WPLoyalty - Level Action
  • Customers, Levels & VIP
    • Creating Levels / Badges
    • Creating Tiered Levels / VIP Customer Rewards
    • Importing Customers and points
  • Translating WPLoyalty
    • Using LocoTranslate
    • Translating Dynamic Strings with LocoTranslate
    • Using WPML
    • Translate dynamic strings via WPML
    • Using TranslatePress
  • Guides
    • How does WPLoyalty work on websites with multiple languages ?
    • How to create user role specific campaigns in WPLoyalty ?
    • Purchase History conditions with examples
  • Settings
    • Display Settings
    • Customizing Emails for Points & Rewards Earned / Expiry
  • Developers
    • REST API with WPLoyalty PRO Plugin
Powered by GitBook
On this page

Was this helpful?

  1. Campaign Types
  2. Achievement Campaign

Custom Action

Use WPLoyalty's Custom Action to reward customers for specific actions with flexible, personalized campaigns.

PreviousAchievement CampaignNextPoints Conversion

Last updated 22 days ago

Was this helpful?

Introduction to Custom Action Functionality in WPLoyalty

The Custom Action feature in WPLoyalty provides an additional method for store owners to award loyalty points to customers for specific actions. This functionality is especially beneficial for campaigns like Achievement Campaigns, where users can earn points by reaching milestones such as Daily Sign-In or Moving Up a Level. The Custom Action mechanism allows for greater flexibility, enabling points and rewards to be triggered by custom events like form submissions or other user-specific actions.

This feature can be extended to a limitless number of custom triggers, provided the necessary conditions are met, such as the inclusion of a user_email parameter in the action or filter. In this documentation, we will guide you through setting up Custom Actions in WPLoyalty using an example of integrating it with Elementor Forms.


Overview of Custom Action

The Custom Action functionality in WPLoyalty allows you to customize how and when your customers earn loyalty points beyond predefined actions such as sign-ins or purchases. By implementing custom hooks, store owners can trigger actions such as earning loyalty points when users perform specific tasks on the website, such as submitting a form or interacting with other third-party integrations.

Requirements

To successfully set up a custom action:

  • The action or filter you want to trigger must include user_email as a parameter. This ensures that the custom action can be executed for the correct user.

  • Store owners should have basic knowledge of WordPress actions and filters to trigger the custom logic.

Please note that before proceeding, you must have installed and activated the WPLoyalty PRO plugin. It is important to note that the below-discussed Custom Action requires the PRO version.


Implementation Example: Integrating Custom Action with Elementor Forms

In this section, we will walk through an example where customers can earn points when submitting an Elementor Form. This will use the elementor_pro/forms/new_record hook to trigger the custom action. Kindly check with the

Scenario: Earn Points on Form Submission

Goal: Allow a customer to earn loyalty points when they submit a form, ensuring that they only earn once.

Step 1: Design the Form

  1. Create the form in Elementor where customers can input their details (e.g., name, email, etc.).

  2. Get the form name and the ID of the email field from the form settings.

Step 2: Create the Hook to Trigger the Custom Action

Create a hook that will trigger the custom action when a user submits the form.

add_action('elementor_pro/forms/new_record', function($record, $handler) {
  $form_name = $record->get_form_settings('form_name');
  if ('My form' !== $form_name) {
      return;
  }
  $form_data = $record->get('fields');
  $email_field_id = 'email';
  $submitted_email = isset($form_data[$email_field_id]['value']) ? $form_data[$email_field_id]['value'] : '';

  do_action('custom_newsletter_submitted', $submitted_email);
}, 10, 2);

Explanation:

  • We first check if the submitted form is the one we created by verifying the form name.

  • Next, we retrieve the email field from the form using the field ID.

  • Once we obtain the email, we trigger a custom action (custom_newsletter_submitted) with the email as a parameter.

Step 3: Trigger the Custom Action

Now, trigger the Custom Action with the email parameter:

add_action('custom_newsletter_submitted', function($email) {
   if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
       return;
   }
   $action_data = [
       'user_email'           => $email,
       'achievement_type'     => 'custom_action',
       'action_sub_type'      => 'extra_action',
       'action_sub_value'     => 0,
       'allowed_campaign_ids' => [34]
   ];
   if (class_exists('\Wlr\App\Premium\Helpers\Achievement')) {
       $achievement_helper = \Wlr\App\Premium\Helpers\Achievement::getInstance();
       $achievement_helper->applyEarnCustomAction($action_data);
   }
}, 10, 1);

Explanation:

  • We construct the action data using the email in the user_email field.

  • The achievement_type is set to custom_action.

  • We define which campaigns the user can earn from by setting the allowed_campaign_ids parameter.

  • The applyEarnCustomAction method is called to apply the points.

Step 4: Validate Before Processing the Earnings

We will now validate the custom action before it is processed using the wlr_achievement_check_status hook.

add_filter('wlr_achievement_check_status', function($status, $data) {
   if (!isset($data['campaign_id']) || empty($data['campaign_id'])) {
      return false;
   }
   if (!in_array($data['campaign_id'], $data['allowed_campaign_ids'])) {
      return false;
   }
   //Check for one time only
   $earn_campaign_where = [
      'user_email'       => ['operator' => '=', 'value' => $data['user_email']],
      'action_type'      => ['operator' => '=', 'value' => 'achievement'],
      'transaction_type' => ['operator' => '=', 'value' => 'credit'],
      'campaign_id'      => ['operator' => '=', 'value' => $data['campaign_id']],
      'action_sub_type'  => ['operator' => '=', 'value' => 'extra_action']
   ];
   if (!class_exists('\Wlr\App\Models\EarnCampaignTransactions')) {
      return false;
   }
   $earn_campaign_transaction = new \Wlr\App\Models\EarnCampaignTransactions();
   $transaction_data = $earn_campaign_transaction->getQueryData($earn_campaign_where, '*', [], false);
   if (!empty($transaction_data)) {
      return false;
   }

   return true;
}, 10, 2);

Explanation:

  • First, we validate that the campaign_id matches the allowed campaigns.

  • Next, we query the EarnCampaignTransactions model to check if the user has already earned from this campaign.

  • If a transaction exists, we prevent the earning, otherwise, we allow the earning.


Advanced Tips

Bonus Tip: Customizing the Earning Logic

You can further customize the earning logic by interacting with the EarnCampaignTransactions object:

  • You can loop through past transactions to limit the number of times a user can earn for a particular action.

  • Modify the logic to meet specific business needs, like awarding points based on the number of actions performed.

The Custom Action functionality in WPLoyalty is highly customizable to fit any business requirements, and you can tailor the logic to suit your needs, whether you're allowing one-time earnings or setting limits based on user behavior.


By following this guide, you can integrate custom actions into your WPLoyalty campaigns, offering customers more ways to earn points and rewards based on their interactions with your site.

The above-discussed Custom Action scenarios require a PRO version.

Next Steps:

You may also want to check out these helpful guides:

Still Unclear ?

For reference, watch the video tutorial: [].

If you need any assistance, please create a at our ticket system. We are always happy to assist you :)

Get WPLoyalty PRO
Elementor Developer Documentation
Video link
Get WPLoyalty PRO
To learn about setting up Tiered Levels or VIP Customer Rewards, click here.
For more information on additional conditions available in WPLoyalty, click here.
If you want to learn about offering points for every dollar customers spend, create a Point for Purchase Campaign.
To discover how to convert earned points to discounts or dollars, create a Point Conversion Reward.
To learn about the various campaign types available, click here.
support request