Automattic\WooCommerce\Internal\Orders

OrderAttributionController::on_init()publicWC 1.0

Hook into WordPress on init.

Method of the class: OrderAttributionController{}

Return

null. Nothing (null).

Usage

$OrderAttributionController = new OrderAttributionController();
$OrderAttributionController->on_init();

OrderAttributionController::on_init() code WC 9.2.3

public function on_init() {
	// Bail if the feature is not enabled.
	if ( ! $this->feature_controller->feature_is_enabled( 'order_attribution' ) ) {
		return;
	}

	// Register WPConsentAPI integration.
	$this->consent->register();

	add_action(
		'wp_enqueue_scripts',
		function() {
			$this->enqueue_scripts_and_styles();
		}
	);

	add_action(
		'admin_enqueue_scripts',
		function() {
			$this->enqueue_admin_scripts_and_styles();
		}
	);

	/**
	 * Filter set of actions used to stamp the unique checkout order attribution HTML container element.
	 *
	 * @since 9.0.0
	 *
	 * @param array $stamp_checkout_html_actions The set of actions used to stamp the unique checkout order attribution HTML container element.
	 */
	$stamp_checkout_html_actions = apply_filters(
		'wc_order_attribution_stamp_checkout_html_actions',
		array(
			'woocommerce_checkout_billing',
			'woocommerce_after_checkout_billing_form',
			'woocommerce_checkout_shipping',
			'woocommerce_after_order_notes',
			'woocommerce_checkout_after_customer_details',
		)
	);
	foreach ( $stamp_checkout_html_actions as $action ) {
		add_action( $action, array( $this, 'stamp_checkout_html_element_once' ) );
	}

	add_action( 'woocommerce_register_form', array( $this, 'stamp_html_element' ) );

	// Update order based on submitted fields.
	add_action(
		'woocommerce_checkout_order_created',
		function( $order ) {
			// Nonce check is handled by WooCommerce before woocommerce_checkout_order_created hook.
			// phpcs:ignore WordPress.Security.NonceVerification
			$params = $this->get_unprefixed_field_values( $_POST );
			/**
			 * Run an action to save order attribution data.
			 *
			 * @since 8.5.0
			 *
			 * @param WC_Order $order The order object.
			 * @param array    $params Unprefixed order attribution data.
			 */
			do_action( 'woocommerce_order_save_attribution_data', $order, $params );
		}
	);

	add_action(
		'woocommerce_order_save_attribution_data',
		function( $order, $data ) {
			$source_data = $this->get_source_values( $data );
			$this->send_order_tracks( $source_data, $order );
			$this->set_order_source_data( $source_data, $order );
		},
		10,
		2
	);

	add_action(
		'user_register',
		function( $customer_id ) {
			try {
				$customer = new WC_Customer( $customer_id );
				$this->set_customer_source_data( $customer );
			} catch ( Exception $e ) {
				$this->log( $e->getMessage(), __METHOD__, WC_Log_Levels::ERROR );
			}
		}
	);

	// Add origin data to the order table.
	add_action(
		'admin_init',
		function() {
			$this->register_order_origin_column();
		}
	);

	add_action(
		'woocommerce_new_order',
		function( $order_id, $order ) {
			$this->maybe_set_admin_source( $order );
		},
		2,
		10
	);
}