Skip to content

Instantly share code, notes, and snippets.

@acanza
Last active October 29, 2016 00:43
Show Gist options
  • Save acanza/5b2af2e21ad13d0837e1d66ef7cb8f85 to your computer and use it in GitHub Desktop.
Save acanza/5b2af2e21ad13d0837e1d66ef7cb8f85 to your computer and use it in GitHub Desktop.
// Crea el rol de usuario "Cliente VIP"
add_action( 'init', 'crea_rol_cliente_vip' );
function crea_rol_cliente_vip(){
$customer_role = get_role( 'customer' );
add_role( 'cliente_vip', __( 'Cliente VIP' ), $customer_role->capabilities );
}
// Aplica un precio especial dependiento del tipo de usuario
add_filter( 'woocommerce_get_price', 'aplica_precio_especial', 10, 2);
function aplica_precio_especial( $price, $product ) {
if (!is_user_logged_in()) return $price;
// Listado de productos con precio especial
$product_list = array( '14893', '14887', '14881' );
// % Descuento a aplicar
$discount = 10;
// Comprueba si el producto actual pertenece a la lista
if( in_array( $product->id, $product_list ) || empty( $product_list ) ) {
// Comprueba si el usuario tiene precio especial
if( usuario_con_precio_especial( 'cliente_vip' ) ){
$price = $price * ( 100 - $discount ) / 100;
}
}
return $price;
}
function usuario_con_precio_especial( $role = '',$user_id = null ){
if( is_numeric( $user_id ) ){
$user = get_user_by( 'id', $user_id );
}else{
$user = wp_get_current_user();
}
if( empty( $user ) ){
return false;
}
return in_array( $role, (array) $user->roles );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment