Change the alphabetical sorting of attributes / variations on a product page in a Woocommerce store

I have a Woocommerce store where various attributes / options are sorted alphabetically when you click “select” on the product page. I am currently trying to figure out how I can sort them based on backend ordering.

In particular: Small is currently located after Large, which does not seem intuitive.

Any help would be more than welcome. I’ve been online for over an hour and can’t find a solution.

The HTML / PHP of my current template is as follows:

<?php
    if ( is_array( $options ) ) {

        if ( isset( $_REQUEST[ 'attribute_' . sanitize_title( $name ) ] ) ) {
            $selected_value = $_REQUEST[ 'attribute_' . sanitize_title( $name ) ];
        } elseif ( isset( $selected_attributes[ sanitize_title( $name ) ] ) ) {
            $selected_value = $selected_attributes[ sanitize_title( $name ) ];
        } else {
            $selected_value = '';
        }

        // Get terms if this is a taxonomy - ordered
        if ( taxonomy_exists( $name ) ) {

            $orderby = wc_attribute_orderby( $name );

            switch ( $orderby ) {
                case 'name' :
                    $args = array( 'orderby' => 'name', 'hide_empty' => false, 'menu_order' => false );
                break;
                case 'id' :
                    $args = array( 'orderby' => 'id', 'order' => 'ASC', 'menu_order' => false, 'hide_empty' => false );
                break;
                case 'menu_order' :
                    $args = array( 'menu_order' => 'ASC', 'hide_empty' => false );
                break;
            }

            $terms = get_terms( $name, $args );

            foreach ( $terms as $term ) {
                if ( ! in_array( $term->slug, $options ) )
                    continue;

                echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $term->slug ), false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>';
            }
        } else {

            foreach ( $options as $option ) {
                echo '<option value="' . esc_attr( sanitize_title( $option ) ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $option ), false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
            }

        }
    }
?>
+4
source share
2 answers

, , ( , 5-6 ), , .

  • → .
  • "" " " .
  • "" " " "". , .

, , . , , , - , .

+8

. FTP, /wp-content/plugins/woocommerce/includes/admin/post-types/meta-boxes/class-wc-meta-box-product-data.php

no 885 :

$args = array(
    'post_type'=>'product_variation',
    'post_status' => array( 'private', 'publish' ),
    'posts_per_page' => -1,
    'meta_key' => 'attribute_pa_woo-color', //rename with your attributes
    'post_parent' => $post->ID,
    'meta_query' => array(
        array(
            'key' => 'attribute_pa_woo-color' //rename with your attributes
        ),
        array(
            'key' => 'attribute_pa_woo-size' //rename with your attributes
        )
    )
);

.

-3

All Articles