Forked from RadGH/filter-wc-shipping-methods-and-payment-gateways.php
Created
June 14, 2018 06:02
-
-
Save manidip/daa499df6cb6783636caf143527cdf2e to your computer and use it in GitHub Desktop.
Filter woocommerce shipping methods and payment gateway by user role.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Homestead Apothecary - Role Based Shipping and Checkout | |
| Plugin URI: http://alchemyandaim.com/ | |
| Description: Customizes the availability of shipping methods and checkout gateways based on user role. No settings are available for this plugin, customizations must be made to the plugin directly. | |
| Author: Alchemy + Aim | |
| Author URI: https://alchemyandaim.com/ | |
| */ | |
| define( 'HA_RBSC_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) ); | |
| define( 'HA_RBSC_PATH', dirname( __FILE__ ) ); | |
| function ha_rcbs_shipping_methods() { | |
| // Note: This only restricts methods. It will not force one of these to become active. These methods/gateways must be active in WooCommerce already. | |
| return array( | |
| 'administrator' => true, // Allow all | |
| 'shop_manager' => true, | |
| 'wholesaler' => array( 'wholesale_shipping' ), // Custom method, see theme functions.php. | |
| 'default' => array( 'enda_bundle_rate' ) // Bundle rates plugin. | |
| ); | |
| } | |
| function ha_rcbs_gateways() { | |
| // Note: This only restricts methods. It will not force one of these to become active. These methods/gateways must be active in WooCommerce already. | |
| return array( | |
| 'administrator' => true, // Allow all | |
| 'shop_manager' => true, | |
| 'wholesaler' => array( 'cheque' ), // Only check. | |
| 'default' => array( 'paypal', 'eh_stripe_pay' ) // Paypal and Stripe. | |
| ); | |
| } | |
| /* | |
| * Returns the current user's role as a string. Only supports the primary user role. All roles as of 4/6/2018: | |
| * | |
| * administrator | |
| * editor | |
| * author | |
| * contributor | |
| * subscriber | |
| * customer | |
| * shop_manager | |
| * wholesaler | |
| * ee_events_administrator | |
| * wpseo_manager | |
| * wpseo_editor | |
| */ | |
| function ha_rcbs_get_user_role() { | |
| $user = wp_get_current_user(); | |
| if ( !$user || empty( $user->roles ) ) return false; | |
| return $user->roles[0]; | |
| } | |
| /** | |
| * Disables unallowed shipping methods based on your user role. | |
| * | |
| * @param $shipping_methods | |
| * @param $package | |
| * | |
| * @return mixed | |
| */ | |
| function ha_rbcs_limit_shipping_methods( $shipping_methods ) { | |
| if ( is_admin() ) return $shipping_methods; // Disable in the dashboard | |
| $user_role = ha_rcbs_get_user_role(); | |
| $shipping_config = ha_rcbs_shipping_methods(); | |
| // If user role isn't in the config, fall back to default settings. | |
| if ( isset($shipping_config[$user_role]) ) { | |
| $allowed_methods = $shipping_config[$user_role]; | |
| }else{ | |
| $allowed_methods = $shipping_config['default']; | |
| } | |
| // True gives access to all methods | |
| if ( $allowed_methods === true ) return $shipping_methods; | |
| // Loop through all gateways, remove those that aren't allowed. | |
| foreach( $shipping_methods as $k => $v ) { | |
| // Key is suffixed with a number, eg "free_shipping:5" which indicates zone. We just want to check the method_id. | |
| if ( !in_array( $v->__get('method_id'), $allowed_methods ) ) unset($shipping_methods[$k]); | |
| } | |
| return $shipping_methods; | |
| } | |
| add_filter( 'woocommerce_package_rates', 'ha_rbcs_limit_shipping_methods', 50, 1 ); | |
| /** | |
| * Disable unallowed payment gateways based on your user role. | |
| * | |
| * @param $gateways | |
| * | |
| * @return mixed | |
| */ | |
| function ha_rcbs_limit_payment_gateways( $gateways ) { | |
| if ( is_admin() ) return $gateways; // Disable in the dashboard | |
| $user_role = ha_rcbs_get_user_role(); | |
| $gateway_config = ha_rcbs_gateways(); | |
| // If user role isn't in the config, fall back to default settings. | |
| if ( isset($gateway_config[$user_role]) ) { | |
| $allowed_gateways = $gateway_config[$user_role]; | |
| }else{ | |
| $allowed_gateways = $gateway_config['default']; | |
| } | |
| // True gives access to all gateways | |
| if ( $allowed_gateways === true ) return $gateways; | |
| // Loop through all gateways, remove those that aren't allowed. | |
| foreach( $gateways as $k => $v ) { | |
| if ( !in_array( $k, $allowed_gateways ) ) unset($gateways[$k]); | |
| } | |
| return $gateways; | |
| } | |
| add_filter('woocommerce_available_payment_gateways', 'ha_rcbs_limit_payment_gateways', 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment