Assigning points for completed orders

We have to start the implementation by connecting WooCommerce and MyCred plugins to award points for completed orders. First, we need to find the ways for tracking the completion of a WooCommerce order. The simplest way is to use a search engine to check the availability of order complete hook, or go through the documentation of the WooCommerce plugin. We can find an action called woocommerce_order_status_completed for handling tasks after the completion of order. Let's take a look at the implementation of order complete hook:

add_action( 'woocommerce_order_status_completed',  'wpqpi_payment_complete' );
public function wpqpi_payment_complete( $order_id ) {
// Execute code after the order is successfully completed
}

We can define the action using the add_action function with a specific callback function. The order ID is passed as a parameter to this function, and hence we can execute any tasks based on order details. The next task is to identify how we can add points in MyCred using custom code.

We have few tasks to be implemented before we can add points to orders. First, we have to add a new hook to enable points for WooCommerce orders. MyCred points are added through built-in and custom hooks. The available hooks can be viewed by visiting the Points | Hooks section in WordPress admin. You need to go through the MyCred documentation to find the necessary hooks for adding custom MyCred hooks. So, let's create a new hook for enabling points for WooCommerce orders:

add_filter( 'mycred_setup_hooks', 'wpqpi_woocommerce_hooks', 10, 2 );
function wpqpi_woocommerce_hooks( $installed, $point_type ) {
$installed['wpqpi_woo_purchase'] = array('title' => __( 'Points for WooCommerce Purchases', 'wpqpi' ), 'description' => __( 'User will get points for completing product purchases.', 'wpqpi' ), 'callback' => array( 'WPQPI_WooCommerce_Hooks' ) );
return $installed;
}

The mycred_setup_hooks filter is used to add custom hooks to MyCred, or remove existing hooks. In this scenario, we add a new hook called wpqpi_woo_purchase for awarding points for WooCommerce purchases. The array defines the name of the hook, the description, and the PHP class to implement the point awarding procedure.

We have added a custom class called WPQPI_WooCommerce_Hooks. Once this code is added, you will see a new hook named Points for WooCommerce Purchases in the Available Hooks section. You can drag the hook to Active Hooks section to make it work, as shown in the following screenshot:

The next step is to implement the class to award points for WooCommerce purchases. So, we need to implement the mycred_load_hooks action to load the hook set up in the previous section. Let's take a look at the implementation of the WPQPI_WooCommerce_Hooks class inside the mycred_load_hooks action:

add_action( 'mycred_load_hooks', 'wpqpi_load_custom_taxonomy_hook', 10 );
function wpqpi_load_custom_taxonomy_hook() {
class WPQPI_WooCommerce_Hooks extends myCRED_Hook {

public function __construct( $hook_prefs, $type ) {
parent::__construct( array( 'id' => 'wpqpi_woo_purchase', 'defaults' => array( 'creds' => 1, 'log' => '%plural% for purchasing a product' ) ), $hook_prefs, $type );
}

}
}

Inside the hook, we just define the class by extending the myCRED_Hook core class of MyCred plugin. Then, we add the class constructor with unique ID and default settings. The settings include the number of points awarded by default and the text for the log entry of awarding points. This class executes a built-in function called run, while loading the hooks. This will be the point of integration where we define the WooCommerce order completed hook. Let's see how we can implement the run function after the class constructor:

public function run() {
add_action( 'woocommerce_order_status_completed', array( $this, 'wpqpi_payment_complete') );
}

We can define any number of actions in the loading process of the MyCred hook created earlier. Here, we have the woocommerce_order_status_completed action and the callback function ready to be executed on successful orders. Now, we can move into the process of awarding points by implementing the wpqpi_payment_complete function, as shown here:

public function wpqpi_payment_complete( $order_id ) {
$order = wc_get_order( $order_id );
$total = $order->get_total();
$credits = (int) $total / 10;
$user = $order->get_user();
$user_id = $user->ID;
$this->core->add_creds( 'wpqpi_woo_purchasing', $user_id, $credits, $this->prefs['log'], 0, '', $m );
}

First, we use the $order_id parameter to load the order details using the wc_get_order function. Next, we get the order total by calling the get_total function on the $order object, and divide it by 10 to generate the number of MyCred points for the order. Next, we get the ID of the user who purchased the products by using the get_user function on the $order object. Finally, we call the MyCred add_creds function to add the points to the user for the order.

The add_creds function has three required parameters and some optional parameters. The first three parameters are used respectively for the reference ID, user ID, and number of points to be awarded. The other parameters are not important for the scenario used in this book. Now, the two plugins are integrated to award MyCred points to the user on successful WooCommerce order completion. We have used an event of one plugin to integrate with another plugin by modifying the data of the second plugin.