Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancoStino/57e34304f48bd57f760802c8ae17b3f5 to your computer and use it in GitHub Desktop.
Save FrancoStino/57e34304f48bd57f760802c8ae17b3f5 to your computer and use it in GitHub Desktop.
Show only first variation dropdown product variable by id variation if user logged out - Woocommerce - Single Product Page
<?php
/*
* Show only first variation dropdown product variable if user logged out - Woocommerce - Single Product Page
*/
add_filter('woocommerce_after_add_to_cart_button','sei_partita_iva', 10, 1);
function sei_partita_iva(){
if ( !is_user_logged_in() ) {
echo '<div style="margin: 20px 0 20px 0;"><h4>Hai una Partita IVA? <a href="https://www.tittex.com/mio-account/?action=register" style="color:#dd3333; text-decoration: underline;" >Registrati</a> per visualizzare i prezzi riservati per te!</h4></div>';
}
}
add_filter('woocommerce_dropdown_variation_attribute_options_args','fun_select_default_option', 1, 1);
function fun_select_default_option( $args ){
if ( !is_user_logged_in() || current_user_can('privato') ){
foreach( $args['options'] as $key => $option ){
if( $option === "1" ) {
$args['selected'] = $args['options'][$key];
}
}
}
return $args;
}
/* --- */
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'custom_woocommerce_dropdown_variation_attribute_options_html', 1, 2 );
function custom_woocommerce_dropdown_variation_attribute_options_html( $html, $args ){
if ( !is_user_logged_in() || current_user_can('privato') ) {
$product = $args[ 'product' ];
$attribute = $args[ 'attribute' ];
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
$options = $args[ 'options' ];
if ( empty( $options ) && !empty( $product ) && !empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
foreach ( $terms as $key => $term ) {
if ( in_array( $term->slug, $options ) && $term->slug != '1') {
//if (in_array($options, '1')){
$html = str_replace( '<option value="' . esc_attr( $term->slug ) . '" ', '<option hidden value ="' . esc_attr( $term->slug ) . '" ', $html );
//}
}
echo "<pre>";
print_r($term);
echo "</pre>";
}
}
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment