Automattic\WooCommerce\Internal\Email

EmailLogger::get_object_contextprivateWC 1.0

Extract loggable context from the WooCommerce object attached to the email.

Returns a stable short type identifier rather than the raw class name so that log aggregation is not brittle across subclasses (e.g. WC_Order_Refund still returns type 'order').

Method of the class: EmailLogger{}

No Hooks.

Returns

Array{type:. string, id?: int}|array{} Type and (when resolvable) ID of the object, or empty when no object is set.

Usage

// private - for code of main (parent) class only
$result = $this->get_object_context( $wc_object ): array;
$wc_object(mixed) (required)
The email's related object (WC_Order, WC_Product, WP_User, etc.) or false/null.

EmailLogger::get_object_context() code WC 10.9.1

private function get_object_context( $wc_object ): array {
	if ( ! is_object( $wc_object ) ) {
		return array();
	}

	if ( $wc_object instanceof WC_Order ) {
		$type = 'order';
	} elseif ( $wc_object instanceof WC_Product ) {
		$type = 'product';
	} elseif ( $wc_object instanceof WP_User ) {
		$type = 'user';
	} else {
		$type = get_class( $wc_object );
	}

	$id = null;
	if ( $wc_object instanceof WC_Order || $wc_object instanceof WC_Product ) {
		// Both have an explicit get_id() — safe to call directly.
		$id = (int) $wc_object->get_id();
	} elseif ( $wc_object instanceof WP_User ) {
		// WP_User has no get_id() method; __call() returns false for unknown methods,
		// which casts to 0 and bypasses the ID-property fallback below.
		$id = (int) $wc_object->ID;
	} elseif ( method_exists( $wc_object, 'get_id' ) ) {
		try {
			$method = new \ReflectionMethod( $wc_object, 'get_id' );
			if ( 0 === $method->getNumberOfRequiredParameters() ) {
				$id = (int) $wc_object->get_id();
			}
		} catch ( \Throwable $e ) {
			$id = null;
		}
	}

	if ( null === $id ) {
		$public_props = get_object_vars( $wc_object );
		if ( array_key_exists( 'ID', $public_props ) ) {
			$id = (int) $public_props['ID'];
		}
	}

	if ( null === $id ) {
		return array( 'type' => $type );
	}

	return array(
		'type' => $type,
		'id'   => $id,
	);
}