Forked from qzya/sorting_in_variations_dropdown.php
Last active
September 21, 2024 10:03
-
-
Save FrancoStino/3240eab0fbf3b3851d8c8ebb79da6437 to your computer and use it in GitHub Desktop.
[Sort product attributes in variations dropdown on single page by backend variations order ] #wp #woocommerce #products #product_attributes #sorting #terms #variations
This file contains 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 | |
// Aggiungi un filtro per modificare l'HTML del menu a tendina per le opzioni di variazione degli attributi | |
add_filter('woocommerce_dropdown_variation_attribute_options_html', 'wc_dropdown_variation_attribute_options_sorted', 20, 2); | |
// Funzione per ordinare le opzioni di variazione degli attributi | |
function wc_dropdown_variation_attribute_options_sorted( $html, $args ) { | |
// Analizza gli argomenti con i valori predefiniti | |
$args = wp_parse_args( | |
apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ), | |
array( | |
'options' => false, | |
'attribute' => false, | |
'product' => false, | |
'selected' => false, | |
'name' => '', | |
'id' => '', | |
'class' => '', | |
'show_option_none' => __( 'Choose an option', 'woocommerce' ), | |
) | |
); | |
// Se non è impostata un'opzione selezionata, utilizza l'attributo predefinito del prodotto | |
if ( false === $args['selected'] && $args['attribute'] && $args['product'] instanceof WC_Product ) { | |
$selected_key = 'attribute_' . sanitize_title( $args['attribute'] ); | |
$args['selected'] = isset( $_REQUEST[ $selected_key ] ) ? wc_clean( wp_unslash( $_REQUEST[ $selected_key ] ) ) : $args['product']->get_variation_default_attribute( $args['attribute'] ); | |
} | |
// Estrai gli argomenti in variabili | |
$options = $args['options']; | |
$product = $args['product']; | |
$attribute = $args['attribute']; | |
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute ); | |
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute ); | |
$class = $args['class']; | |
$show_option_none = (bool) $args['show_option_none']; | |
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' ); | |
// Se non ci sono opzioni impostate, ottieni gli attributi di variazione del prodotto | |
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) { | |
$attributes = $product->get_variation_attributes(); | |
$options = $attributes[ $attribute ]; | |
} | |
// Inizia a costruire l'HTML per il menu a tendina | |
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">'; | |
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>'; | |
// Se non ci sono opzioni disponibili, restituisci l'HTML | |
if ( empty( $options ) ) { | |
return $html .= '</select>'; | |
} | |
// Se l'attributo è una tassonomia, ottieni i termini e ordina | |
if ( $product && taxonomy_exists( $attribute ) ) { | |
$terms = wc_get_product_terms( | |
$product->get_id(), | |
$attribute, | |
array( | |
'fields' => 'all', | |
) | |
); | |
$special_sizes = []; | |
$double_zero = null; | |
$triple_zero = null; | |
$months = []; | |
$years = []; | |
$other_options = []; | |
foreach ( $terms as $term ) { | |
if ( $term->name === '00' ) { | |
$double_zero = $term; | |
} elseif ( $term->name === '000' ) { | |
$triple_zero = $term; | |
} elseif ( preg_match( '/^Taglia\s+0+/i', $term->name ) ) { | |
$special_sizes[] = ['term' => $term, 'number' => substr_count($term->name, '0')]; | |
} elseif ( preg_match( '/(\d+)\s*(mes|mesi)/i', $term->name, $matches ) ) { | |
$months[] = ['term' => $term, 'number' => (int) $matches[1]]; | |
} elseif ( preg_match( '/(\d+)\s*(ann|anni)/i', $term->name, $matches ) ) { | |
$years[] = ['term' => $term, 'number' => (int) $matches[1]]; | |
} else { | |
$other_options[] = $term; | |
} | |
} | |
// Ordina le taglie speciali in base al numero di zeri (decrescente) | |
usort( $special_sizes, function( $a, $b ) { | |
return $b['number'] - $a['number']; | |
}); | |
// Ordina i mesi e gli anni numericamente | |
usort( $months, function( $a, $b ) { | |
return $a['number'] - $b['number']; | |
}); | |
usort( $years, function( $a, $b ) { | |
return $a['number'] - $b['number']; | |
}); | |
// Unisci tutte le categorie nell'ordine desiderato | |
$sorted_terms = array_merge( | |
$triple_zero ? [$triple_zero] : [], | |
$double_zero ? [$double_zero] : [], | |
array_column( $special_sizes, 'term' ), | |
array_column( $months, 'term' ), | |
array_column( $years, 'term' ), | |
$other_options | |
); | |
// Aggiungi ogni termine come opzione nel menu a tendina | |
foreach ( $sorted_terms as $term ) { | |
if ( in_array( $term->slug, $options, true ) ) { | |
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name, $term, $attribute, $product ) ) . '</option>'; | |
} | |
} | |
} else { | |
// Se l'attributo non è una tassonomia, aggiungi ogni opzione nel menu a tendina e formatta i nomi | |
foreach ( $options as $option ) { | |
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false ); | |
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option, null, $attribute, $product ) ) . '</option>'; | |
} | |
} | |
// Restituisci l'HTML finale per il menu a tendina | |
return $html .= '</select>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment