ADVANCED_COUPONS_USER_RECEIVES_CREDIT::user_receives_credit( $data )

Validation function when the trigger action is hit


Parameters Parameters

$data

(Required)


Source Source

File: src/integrations/advanced-coupons/triggers/advanced-coupons-user-receives-credit.php

	public function user_receives_credit( $data ) {

		if ( ! isset( $data['type'] ) || 'decrease' === $data['type'] ) {
			return;
		}

		$user_id = ( isset( $data['user_id'] ) ) ? intval( $data['user_id'] ) : 0;

		if ( 0 === $user_id ) {
			// Its a logged in recipe and
			// user ID is 0. Skip process
			return;
		}

		$recipes            = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
		$conditions         = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );
		$acfwc_amount       = Automator()->get->meta_from_recipes( $recipes, 'ACFWC_AMOUNT' );
		$matched_recipe_ids = array();
		$balance            = floatval( $data['amount'] );
		$order_id           = 0;

		if ( isset( $data['action'] ) && isset( $data['object_id'] ) && intval( $data['object_id'] ) > 0 ) {
			$order_id = intval( $data['object_id'] );
		}

		//Add where Point Type & Current Balances Matches
		foreach ( $recipes as $recipe_id => $recipe ) {
			foreach ( $recipe['triggers'] as $trigger ) {
				$trigger_id = $trigger['ID'];
				if ( intval( '-1' ) === intval( $conditions[ $recipe_id ][ $trigger_id ] ) ) {
					$matched_recipe_ids[] = array(
						'recipe_id'  => $recipe_id,
						'trigger_id' => $trigger_id,
					);
				} else {
					if ( 'GT' === $conditions[ $recipe_id ][ $trigger_id ] ) {
						if ( $balance > floatval( $acfwc_amount[ $recipe_id ][ $trigger_id ] ) ) {
							$matched_recipe_ids[] = array(
								'recipe_id'  => $recipe_id,
								'trigger_id' => $trigger_id,
							);
						}
					} elseif ( 'EQ' === $conditions[ $recipe_id ][ $trigger_id ] || intval( '-1' ) === intval( $conditions[ $recipe_id ][ $trigger_id ] ) ) {
						if ( floatval( $acfwc_amount[ $recipe_id ][ $trigger_id ] ) === $balance ) {
							$matched_recipe_ids[] = array(
								'recipe_id'  => $recipe_id,
								'trigger_id' => $trigger_id,
							);
						}
					} elseif ( 'NOT_EQ' === $conditions[ $recipe_id ][ $trigger_id ] || intval( '-1' ) === intval( $conditions[ $recipe_id ][ $trigger_id ] ) ) {
						if ( floatval( $acfwc_amount[ $recipe_id ][ $trigger_id ] ) !== $balance ) {
							$matched_recipe_ids[] = array(
								'recipe_id'  => $recipe_id,
								'trigger_id' => $trigger_id,
							);
						}
					} elseif ( 'LT' === $conditions[ $recipe_id ][ $trigger_id ] || intval( '-1' ) === intval( $conditions[ $recipe_id ][ $trigger_id ] ) ) {
						if ( $balance < floatval( $acfwc_amount[ $recipe_id ][ $trigger_id ] ) ) {
							$matched_recipe_ids[] = array(
								'recipe_id'  => $recipe_id,
								'trigger_id' => $trigger_id,
							);
						}
					} elseif ( 'GT_EQ' === $conditions[ $recipe_id ][ $trigger_id ] || intval( '-1' ) === intval( $conditions[ $recipe_id ][ $trigger_id ] ) ) {
						if ( $balance >= floatval( $acfwc_amount[ $recipe_id ][ $trigger_id ] ) ) {
							$matched_recipe_ids[] = array(
								'recipe_id'  => $recipe_id,
								'trigger_id' => $trigger_id,
							);
						}
					} elseif ( 'LT_EQ' === $conditions[ $recipe_id ][ $trigger_id ] || intval( '-1' ) === intval( $conditions[ $recipe_id ][ $trigger_id ] ) ) {
						if ( $balance <= floatval( $acfwc_amount[ $recipe_id ][ $trigger_id ] ) ) {
							$matched_recipe_ids[] = array(
								'recipe_id'  => $recipe_id,
								'trigger_id' => $trigger_id,
							);
						}
					}
				}
			}
		}

		if ( ! empty( $matched_recipe_ids ) ) {
			foreach ( $matched_recipe_ids as $matched_recipe_id ) {
				$pass_args = array(
					'code'             => $this->trigger_code,
					'meta'             => $this->trigger_meta,
					'user_id'          => $user_id,
					'recipe_to_match'  => $matched_recipe_id['recipe_id'],
					'trigger_to_match' => $matched_recipe_id['trigger_id'],
					'ignore_post_id'   => true,
					'is_signed_in'     => true,
				);

				$args = Automator()->maybe_add_trigger_entry( $pass_args, false );

				if ( $args ) {
					foreach ( $args as $result ) {
						if ( true === $result['result'] ) {
							if ( isset( $result['args'] ) && isset( $result['args']['get_trigger_id'] ) ) {
								$trigger_meta = array(
									'user_id'        => $user_id,
									'trigger_id'     => (int) $result['args']['trigger_id'],
									'trigger_log_id' => $result['args']['get_trigger_id'],
									'run_number'     => $result['args']['run_number'],
								);

								if ( 0 !== $order_id ) {
									$trigger_meta['meta_key']   = 'order_id';
									$trigger_meta['meta_value'] = $order_id;
									Automator()->insert_trigger_meta( $trigger_meta );
								}

								$trigger_meta['meta_key']   = 'ACFWC_AMOUNT';
								$trigger_meta['meta_value'] = $balance;
								Automator()->insert_trigger_meta( $trigger_meta );

								$trigger_meta['meta_key']   = 'USERTOTALCREDIT';
								$trigger_meta['meta_value'] = Automator()->helpers->recipe->advanced_coupons->get_current_balance_of_the_customer( $user_id );

								Automator()->insert_trigger_meta( $trigger_meta );

								$trigger_meta['meta_key']   = 'USERLIFETIMECREDIT';
								$trigger_meta['meta_value'] = Automator()->helpers->recipe->advanced_coupons->get_total_credits_of_the_user( $user_id );

								Automator()->insert_trigger_meta( $trigger_meta );

							}
							Automator()->maybe_trigger_complete( $result['args'] );
						}
					}
				}
			}
		}
	}