Skip to content

Instantly share code, notes, and snippets.

@acanza
Created August 15, 2015 11:51
Show Gist options
  • Save acanza/202b5ab6f1c22a309189 to your computer and use it in GitHub Desktop.
Save acanza/202b5ab6f1c22a309189 to your computer and use it in GitHub Desktop.
//* Habilita cupón sólo usuarios registrados
/* Añade checkbox para habilitar opción */
add_action( 'woocommerce_coupon_options_usage_restriction', 'add_custom_field_meta_box_coupon' );
function add_custom_field_meta_box_coupon(){
echo '<div class="option_group">';
woocommerce_wp_checkbox( array( 'id' => 'register_users_use', 'label' => __( 'Solo usuarios registrados', 'woocommerce' ), 'description' => __( 'Marca esta opción si quieres permitir el uso de este cupón solo a usuarios registrados.', 'woocommerce' ) ) );
echo '</div>';
}
/* Guarda opción */
add_action( 'woocommerce_coupon_options_save', 'save_custom_field_meta_box_coupon' );
function save_custom_field_meta_box_coupon( $post_id ){
$register_users_use = isset( $_POST['register_users_use'] ) ? 'yes' : 'no';
update_post_meta( $post_id, 'register_users_use', $register_users_use );
}
/* Aplica el cupón si el usuario está registrado */
add_filter( 'woocommerce_coupon_is_valid', 'apply_coupon_registered_users_only', 10, 2 );
function apply_coupon_registered_users_only( $availability, $coupon ){
$coupon_data = get_post_meta( $coupon->id, 'register_users_use', true );
if( ( $coupon_data == 'yes' ) && !is_user_logged_in() ){
$availability = false;
}
return $availability;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment