Automattic\WooCommerce\Admin\API

Notes::activate_promo_note()publicWC 1.0

Activate a promo note, create if not exist.

Метод класса: Notes{}

No Hooks.

Return

WP_REST_Request|WP_Error.

Usage

$Notes = new Notes();
$Notes->activate_promo_note( $request );
$request(WP_REST_Request) (required)
Request object.

Код Notes::activate_promo_note() WC 7.1.0

public function activate_promo_note( $request ) {
	$promo_note_name = $request->get_param( 'promo_note_name' );

	if ( ! in_array( $promo_note_name, $this->allowed_promo_notes, true ) ) {
		return new \WP_Error(
			'woocommerce_note_invalid_promo_note_name',
			__( 'Please provide a valid promo note name.', 'woocommerce' ),
			array( 'status' => 422 )
		);
	}

	$data_store = NotesRepository::load_data_store();
	$note_ids   = $data_store->get_notes_with_name( $promo_note_name );

	if ( empty( $note_ids ) ) {
		// Promo note doesn't exist, this could happen in cases where
		// user might have disabled RemoteInboxNotications via disabling
		// marketing suggestions. Thus we'd have to manually add the note.
		$note = new Note();
		$note->set_name( $promo_note_name );
		$note->set_status( Note::E_WC_ADMIN_NOTE_ACTIONED );
		$data_store->create( $note );
	} else {
		$note = NotesRepository::get_note( $note_ids[0] );
		NotesRepository::update_note(
			$note,
			[
				'status' => Note::E_WC_ADMIN_NOTE_ACTIONED,
			]
		);
	}

	return rest_ensure_response(
		array(
			'success' => true,
		)
	);
}