Forked from qzya/sorting_in_variations_dropdown.php
Last active
May 27, 2024 10:25
-
-
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 | |
// Add a filter to modify the HTML of the dropdown for variation attribute options | |
add_filter('woocommerce_dropdown_variation_attribute_options_html', 'wc_dropdown_variation_attribute_options_sorted', 20, 2); | |
// Function to sort the variation attribute options | |
function wc_dropdown_variation_attribute_options_sorted( $html, $args ) { | |
// Parse the arguments with the default values | |
$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' ), | |
) | |
); | |
// If no selected option is set, use the default attribute of the product | |
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'] ); | |
} | |
// Extract the arguments into variables | |
$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' ); | |
// If no options are set, get the variation attributes of the product | |
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) { | |
$attributes = $product->get_variation_attributes(); | |
$options = $attributes[ $attribute ]; | |
} | |
// Start building the HTML for the dropdown | |
$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>'; | |
// If no options are available, return the HTML | |
if ( empty( $options ) ) { | |
return $html .= '</select>'; | |
} | |
// If the attribute is a taxonomy, get the terms and sort them | |
if ( $product && taxonomy_exists( $attribute ) ) { | |
$terms = wc_get_product_terms( | |
$product->get_id(), | |
$attribute, | |
array( | |
'fields' => 'all', | |
) | |
); | |
//sorting starts here | |
foreach($terms as $key => $term) { | |
$i = 0; | |
foreach($product->get_available_variations() as $variation) { | |
$i++; | |
if ($term->slug == $variation['attributes'][$name]) { | |
$key = $i - 1; | |
unset($terms[$key]); | |
$terms[$key] = $term; | |
} | |
} | |
} | |
ksort($terms); | |
// Separate terms into months, years, and other options | |
/*$months = []; | |
$years = []; | |
$other_options = []; | |
foreach ($terms as $term) { | |
if (strpos($term->name, 'mes') !== false) { // Mesi o mese: in questo caso l'ultima lettera la eludo per prendere sia singolare che plurale | |
$months[] = $term; | |
} elseif (strpos($term->name, 'ann') !== false) { // Anni o anno: in questo caso l'ultima lettera la eludo per prendere sia singolare che plurale | |
$years[] = $term; | |
} else { | |
$other_options[] = $term; | |
} | |
} | |
// Merge months, years, and other options | |
$sorted_terms = array_merge($months, $years, $other_options); | |
// Replace the original terms array with the sorted one | |
$terms = $sorted_terms; | |
// Sort the terms by key | |
ksort($terms);*/ | |
// Add each term as an option in the dropdown | |
foreach ( $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 { | |
// If the attribute is not a taxonomy, add each option in the dropdown | |
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>'; | |
} | |
} | |
// Return the final HTML for the dropdown | |
return $html .= '</select>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment