mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-05-02 12:02:25 +08:00
https://stackoverflow.com/questions/72302559/enable-shipping-method-for-specific-user-role-only-in-woocommerce
18 lines
643 B
Text
18 lines
643 B
Text
function filter_woocommerce_package_rates( $rates, $package ) {
|
|
// Set the rate IDs in the array
|
|
$rate_ids = array( 'local_pickup:1', 'free_shipping:2' );
|
|
|
|
// NOT the required user role, remove shipping method(s)
|
|
if ( ! current_user_can( 'administrator' ) ) {
|
|
// Loop trough
|
|
foreach ( $rates as $rate_id => $rate ) {
|
|
// Checks if a value exists in an array
|
|
if ( in_array( $rate_id, $rate_ids ) ) {
|
|
unset( $rates[$rate_id] );
|
|
}
|
|
}
|
|
}
|
|
|
|
return $rates;
|
|
}
|
|
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
|