Automattic\WooCommerce\Internal\Admin\Settings

PaymentProviders::attach_extension_suggestionpublicWC 1.0

Attach a payment extension suggestion.

Attachment is a broad concept that can mean different things depending on the suggestion. Currently, we use it to record the extension installation. This is why we expect to receive instructions to record attachment when the extension is installed.

Method of the class: PaymentProviders{}

No Hooks.

Returns

true|false. True if the suggestion was successfully marked as attached, false otherwise.

Usage

$PaymentProviders = new PaymentProviders();
$PaymentProviders->attach_extension_suggestion( $id ): bool;
$id(string) (required)
The ID of the payment extension suggestion to attach.

PaymentProviders::attach_extension_suggestion() code WC 9.9.4

public function attach_extension_suggestion( string $id ): bool {
	// We may receive a suggestion ID that is actually an order map ID used in the settings page providers list.
	// Extract the suggestion ID from the order map ID.
	if ( $this->is_suggestion_order_map_id( $id ) ) {
		$id = $this->get_suggestion_id_from_order_map_id( $id );
	}

	$suggestion = $this->get_extension_suggestion_by_id( $id );
	if ( is_null( $suggestion ) ) {
		throw new Exception( esc_html__( 'Invalid suggestion ID.', 'woocommerce' ) );
	}

	$payments_nox_profile = get_option( Payments::PAYMENTS_NOX_PROFILE_KEY, array() );
	if ( empty( $payments_nox_profile ) ) {
		$payments_nox_profile = array();
	} else {
		$payments_nox_profile = maybe_unserialize( $payments_nox_profile );
	}

	// Check if it is already marked as attached.
	if ( ! empty( $payments_nox_profile['suggestions'][ $id ]['attached']['timestamp'] ) ) {
		return true;
	}

	// Mark the suggestion as attached.
	if ( empty( $payments_nox_profile['suggestions'] ) ) {
		$payments_nox_profile['suggestions'] = array();
	}
	if ( empty( $payments_nox_profile['suggestions'][ $id ] ) ) {
		$payments_nox_profile['suggestions'][ $id ] = array();
	}
	if ( empty( $payments_nox_profile['suggestions'][ $id ]['attached'] ) ) {
		$payments_nox_profile['suggestions'][ $id ]['attached'] = array();
	}
	$payments_nox_profile['suggestions'][ $id ]['attached']['timestamp'] = time();

	// Store the modified profile data.
	$result = update_option( Payments::PAYMENTS_NOX_PROFILE_KEY, $payments_nox_profile, false );
	// Since we already check if the suggestion is already attached, we should not get a false result
	// for trying to update with the same value.
	// False means the update failed and the suggestion is not marked as attached.
	if ( false === $result ) {
		return false;
	}

	// Handle custom attachment logic per-provider.
	switch ( $id ) {
		case ExtensionSuggestions::PAYPAL_FULL_STACK:
		case ExtensionSuggestions::PAYPAL_WALLET:
			// Set an option to inform the extension.
			update_option( 'woocommerce_paypal_branded', 'payments_settings', false );
			break;
		default:
			break;
	}

	return true;
}