HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: //proc/self/cwd/wp-content/plugins/automatewoo/includes/Triggers/Customer_Total_Spend_Reaches.php
<?php
// phpcs:ignoreFile

namespace AutomateWoo;

if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * This trigger hooks in the the order completed action but will only fire once when a users total spend reaches a certain amount.
 *
 * @class Trigger_Customer_Total_Spend_Reaches
 */
class Trigger_Customer_Total_Spend_Reaches extends Trigger_Abstract_Order_Base {

	public $supplied_data_items = [ 'customer', 'order' ];

	/**
	 * Async events required by the trigger.
	 *
	 * @since 4.8.0
	 * @var array|string
	 */
	protected $required_async_events = 'order_status_changed';


	function load_admin_details() {
		$this->title = __( 'Customer Total Spend Reaches', 'automatewoo' );
		$this->description = __( "This trigger checks the customer's total spend each time an order is completed.", 'automatewoo');
		$this->group = __( 'Customers', 'automatewoo' );
	}


	function load_fields() {
		$spend = new Fields\Price();
		$spend->set_name( 'total_spend' );
		$spend->set_title( __( 'Total spend', 'automatewoo' ) );
		$spend->set_description( __( 'Do not add a currency symbol.', 'automatewoo' ) );
		$spend->set_required();

		$this->add_field( $spend );
	}


	/**
	 * Must run after customer totals have been updated
	 */
	function register_hooks() {
		add_action( $this->get_hook_order_status_changed(), [ $this, 'catch_hooks' ], 10, 3 );
	}


	/**
	 * @param $order_id
	 * @param $old_status
	 * @param $new_status
	 */
	function catch_hooks( $order_id, $old_status, $new_status ) {
		if ( $new_status !== 'completed' ) {
			return;
		}

		$this->trigger_for_order( $order_id );
	}


	/**
	 * @param Workflow $workflow
	 *
	 * @return bool
	 */
	function validate_workflow( $workflow ) {
		$customer = $workflow->data_layer()->get_customer();
		$total_spend = $workflow->get_trigger_option( 'total_spend' );

		if ( ! $customer ) {
			return false;
		}

		if ( ! $total_spend || $customer->get_total_spent() < $total_spend ) {
			return false;
		}

		// Only do this once for each user (for each workflow)
		if ( $workflow->get_run_count_for_customer( $customer ) !== 0 ) {
			return false;
		}

		return true;
	}

}