Skip to content

Instantly share code, notes, and snippets.

@mirite
Created April 10, 2022 00:28
Show Gist options
  • Select an option

  • Save mirite/66efbcc02ad19436f9a6d0760d859c15 to your computer and use it in GitHub Desktop.

Select an option

Save mirite/66efbcc02ad19436f9a6d0760d859c15 to your computer and use it in GitHub Desktop.
Restrict product visibility in WooCommerce to a specific user role
<?php
/**
* Limit Product Visibility
* @wordpress-plugin
* Plugin Name: Limit Product Visibility
* Description: A WooCommerce plugin to limit product visibility to administrators and a user role called Reps.
* Version: 1.0.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Jesse Conner
*
* @package Limit Product Availability
*/
/**
* Allows for products that can only be viewed by specific user roles.
*
* @since 1.0.0
*/
class LimitProductVisibility {
private const META_KEY = 'limit_product_visibility';
/**
* Instantiate the hooks and filters of our class
*
* @return void
* @since 1.0.0
*/
public function __construct() {
add_action( 'init', array( $this, 'create_role' ), 11 ); // Creates the required roles and capabilities.
add_action( 'woocommerce_product_options_general_product_data', array(
$this,
'product_settings'
) ); // Add the visibility settings to products.
add_action( 'woocommerce_process_product_meta', array(
$this,
'save_fields'
), 10, 2 ); // Save the product visibility settings.
add_filter( 'woocommerce_product_is_visible', array(
$this,
'product_invisible'
) ); // Hide product from listings if it is hidden from normal customers.
}
/**
* Creates the required roles and capabilities
*
* @return void
* @since 1.0.0
*/
public function create_role() {
add_role(
'rep',
'Rep',
array(
'read' => true,
'see_restricted' => true,
),
);
$role = get_role( 'administrator' );
// Add a new capability.
$role->add_cap( 'see_restricted' );
}
/**
* Add the visibility settings to products
*
* @return void
* @since 1.0.0
*/
public function product_settings() {
$limited = get_post_meta( get_the_ID(), self::META_KEY, true );
?>
<div class="option_group">
<?php
woocommerce_wp_select(
array(
'id' => self::META_KEY,
'label' => 'Restrict access to:',
'value' => $limited,
'options' => array(
'' => 'Not Restricted',
'Reps' => 'Reps',
),
'wrapper_class' => 'form-field-wide',
)
);
wp_nonce_field( 'restrict-access' );
?>
</div>
<?php
}
/**
* Save the product visibility settings
*
* @param int $id The id of the post to apply the meta to.
*
* @return void
* @since 1.0.0
*/
public function save_fields( $id ) {
if ( isset( $_POST[self::META_KEY] ) && check_admin_referer( 'restrict-access' ) ) {
$role = sanitize_meta( self::META_KEY, wp_unslash( $_POST[self::META_KEY] ), 'post', 'product' );
} else {
return;
}
update_post_meta( $id, self::META_KEY, esc_attr( $role ) );
}
/**
* Hide product from listings if it is hidden from normal customers
*
* @return boolean Whether to hide the product or not.
* @since 1.0.0
*/
public function product_invisible() {
$restricted = get_post_meta( get_the_ID(), self::META_KEY, true );
if ( ! $restricted ) {
return true;
}
return current_user_can( 'see_restricted' );
}
}
// Make sure WooCommerce is active before instantiating the plugin.
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) {
new LimitProductVisibility();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment