Automattic\WooCommerce\Internal\POS
Capabilities::set_pos_preset
Assign or clear the POS preset for a user.
Touches only caps + meta, never WP roles: granting access to an existing user leaves their role intact, and clearing it never leaves them roleless. Every woocommerce_pos_* cap the user holds directly (via add_cap — the only way this class grants them) is stripped first, so a preset change (Manager to Cashier) drops the caps the new preset omits, and a clear (null) removes them along with the preset meta. Role-granted woocommerce_pos_* caps, if any, are out of scope: this class never adds caps to roles.
Method of the class: Capabilities{}
No Hooks.
Returns
bool. True on success (including clears); false if the user does not exist or the preset value is not assignable.
Usage
$result = Capabilities::set_pos_preset( $user_id, ?string $preset ): bool;
- $user_id(int) (required)
- Target user.
- ?string $preset(required)
- .
Changelog
| Since 11.0.0 | Introduced. |
Capabilities::set_pos_preset() Capabilities::set pos preset code WC 11.1.2
public static function set_pos_preset( int $user_id, ?string $preset ): bool {
$user = get_user_by( 'id', $user_id );
if ( ! $user instanceof WP_User ) {
return false;
}
// Validate before mutating any state.
if ( null !== $preset && ! in_array( $preset, POSPreset::get_all(), true ) ) {
return false;
}
// Strip the user's directly-held woocommerce_pos_* caps so a preset change (or clear)
// starts clean. remove_cap() is a no-op for caps the user does not hold and strips a cap
// whether it was granted (true) or explicitly denied (false). This class only grants caps
// per-user via add_cap(), so role-granted caps are out of scope.
foreach ( self::all_pos_capabilities() as $cap ) {
$user->remove_cap( $cap );
}
if ( null === $preset ) {
Users::delete_site_user_meta( $user_id, self::POS_PRESET_META_KEY );
return true;
}
// Store the preset per-site so the bookkeeping stays aligned with the blog-scoped
// POS capabilities on multisite (Users::update_site_user_meta suffixes the blog prefix,
// so the key still matches the woocommerce_% uninstall sweep).
Users::update_site_user_meta( $user_id, self::POS_PRESET_META_KEY, $preset );
foreach ( array_keys( self::capabilities_for_preset( $preset ) ) as $cap ) {
$user->add_cap( $cap );
}
return true;
}