WCM_ADDUSER_A::add_user_to_membership_plan( $user_id,  $action_data,  $recipe_id,  $args )

Validation function when the action is hit


Parameters Parameters

$user_id

(Required)

$action_data

(Required)

$recipe_id

(Required)

$args

(Required)


Source Source

File: src/integrations/wc-memberships/actions/wcm-adduser-a.php

	public function add_user_to_membership_plan( $user_id, $action_data, $recipe_id, $args ) {


		$plan                  = $action_data['meta'][ $this->action_meta ];
		$check_membership_plan = wc_memberships_is_user_member( $user_id, $plan );

		if ( true === $check_membership_plan && true === wc_memberships_is_user_active_member( $user_id, $plan ) ) {
			$recipe_log_id             = $action_data['recipe_log_id'];
			$args['do-nothing']        = true;
			$action_data['do-nothing'] = true;
			$action_data['completed']  = true;
			$error_message             = esc_attr__( 'This user has already an active membership in the specified membership plan', 'uncanny-automator' );
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_message, $recipe_log_id, $args );
		} else {

			try {
				$arguments       = array(
					'plan_id' => $plan,
					'user_id' => $user_id,
				);
				$user_membership = wc_memberships_create_user_membership( $arguments );
				Automator()->complete_action( $user_id, $action_data, $recipe_id );
			} catch ( WC_REST_Exception $e ) {
				$error_message                       = $e->getMessage();
				$recipe_log_id                       = $action_data['recipe_log_id'];
				$args['do-nothing']                  = true;
				$action_data['do-nothing']           = true;
				$action_data['complete_with_errors'] = true;
				Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_message, $recipe_log_id, $args );
			} catch ( SV_WC_Plugin_Exception $e ) {
			}
		}

		return;
	}