← All changes
|
integrations/fluentcart/includes/triggers/complete-purchase.php
+118
-0
5.7.7
→
6.0.4
View file →
| @@ -1,0 +1,118 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Make Purchase | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\FluentCart\Triggers\Make_Purchase | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class AutomatorWP_FluentCart_Complete_Purchase extends AutomatorWP_Integration_Trigger { | |
| 13 | + | |
| 14 | + public $integration = 'fluentcart'; | |
| 15 | + public $trigger = 'fluentcart_complete_purchase'; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Register the trigger | |
| 19 | + * | |
| 20 | + * @since 1.0.0 | |
| 21 | + */ | |
| 22 | + public function register() { | |
| 23 | + | |
| 24 | + automatorwp_register_trigger( $this->trigger, array( | |
| 25 | + 'integration' => $this->integration, | |
| 26 | + 'label' => __( 'User completes a purchase', 'automatorwp' ), | |
| 27 | + 'select_option' => __( 'User completes a <strong>purchase</strong>', 'automatorwp' ), | |
| 28 | + /* translators: %1$s: Product title. %2$s: Number of times. */ | |
| 29 | + 'edit_label' => sprintf( __( 'User completes a purchase %1$s time(s)', 'automatorwp' ), '{times}' ), | |
| 30 | + 'log_label' => __( 'User completes a purchase', 'automatorwp' ), | |
| 31 | + 'action' => 'fluent_cart/order_status_changed_to_completed', | |
| 32 | + 'function' => array( $this, 'listener' ), | |
| 33 | + 'priority' => 10, | |
| 34 | + 'accepted_args' => 1, | |
| 35 | + 'options' => array( | |
| 36 | + 'times' => automatorwp_utilities_times_option(), | |
| 37 | + ), | |
| 38 | + 'tags' => array_merge( | |
| 39 | + automatorwp_fluentcart_get_order_tags(), | |
| 40 | + automatorwp_utilities_times_tag() | |
| 41 | + ) | |
| 42 | + ) ); | |
| 43 | + | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Trigger listener | |
| 48 | + * | |
| 49 | + * @since 1.0.0 | |
| 50 | + * | |
| 51 | + * @param array $data | |
| 52 | + * | |
| 53 | + */ | |
| 54 | + public function listener( $data ) { | |
| 55 | + | |
| 56 | + $user_id = get_current_user_id(); | |
| 57 | + | |
| 58 | + // Bail if user is not logged | |
| 59 | + if ($user_id === 0) { | |
| 60 | + return; | |
| 61 | + } | |
| 62 | + | |
| 63 | + $order = $data['order']; | |
| 64 | + $order_id = $order->id; | |
| 65 | + | |
| 66 | + // Trigger user product purchased | |
| 67 | + automatorwp_trigger_event( array( | |
| 68 | + 'trigger' => $this->trigger, | |
| 69 | + 'user_id' => $user_id, | |
| 70 | + 'order_id' => $order_id, | |
| 71 | + 'order_data' => $order, | |
| 72 | + ) ); | |
| 73 | + | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Register the required hooks | |
| 78 | + * | |
| 79 | + * @since 1.0.0 | |
| 80 | + */ | |
| 81 | + public function hooks() { | |
| 82 | + | |
| 83 | + // Log meta data | |
| 84 | + add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); | |
| 85 | + | |
| 86 | + parent::hooks(); | |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * Trigger custom log meta | |
| 91 | + * | |
| 92 | + * @since 1.0.0 | |
| 93 | + * | |
| 94 | + * @param array $log_meta Log meta data | |
| 95 | + * @param stdClass $trigger The trigger object | |
| 96 | + * @param int $user_id The user ID | |
| 97 | + * @param array $event Event information | |
| 98 | + * @param array $trigger_options The trigger's stored options | |
| 99 | + * @param stdClass $automation The trigger's automation object | |
| 100 | + * | |
| 101 | + * @return array | |
| 102 | + */ | |
| 103 | + function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 104 | + | |
| 105 | + // Bail if action type don't match this action | |
| 106 | + if( $trigger->type !== $this->trigger ) { | |
| 107 | + return $log_meta; | |
| 108 | + } | |
| 109 | + | |
| 110 | + $log_meta['order_id'] = ( isset( $event['order_id'] ) ? $event['order_id'] : 0 ); | |
| 111 | + | |
| 112 | + return $log_meta; | |
| 113 | + | |
| 114 | + } | |
| 115 | + | |
| 116 | +} | |
| 117 | + | |
| 118 | +new AutomatorWP_FluentCart_Complete_Purchase(); | |