I have a Woocommerce shop where the different attributes / variations are ordered alphabetically when you click the 'select' on a product page. I'm currently trying to figure out how I can sort them based on the ordening in the backend.
Specifically: Currently Small is placed after Large, which not feels all that intuitive.
Any help would be more than welcome. I've been googling for an hour now and can't seem to find the 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>';
}
}
}
?>
In the Customizer, go to WooCommerce then Product catalog change the “Default product sorting” to “Default sorting (custom ordering + name)” and we'll get going from there. (See a gif of how to access this). This setting will automatically sort products alphabetically in your store.
In the left-hand menu, click Products and then click Attributes. On the WooCommerce Attributes page, hover over the attribute you want to change (in this example Size) and click Edit. In the Edit Attribute page, open the Default sort order dropdown menu. There are four different options here.
Change the Product Sorting in WooCommerce Settings If you have WooCommerce 3.2. 6 or below, you can go to Settings > Products > Display tab and change the default sorting there.
Go to: Products > Add Product (or edit an existing one). Select the Attributes tab in the Product Data. There you can choose any of the attributes that you've created in the dropdown menu.
You might have figured out the solution by now as it is quite simple( although I got to know after wasting 5-6 hrs), but if you haven't, here it is.
For ex. in your case, drag Small to the top of the table & Large to the bottom, that's it. There is no save option, so don't get confused with it, wherever you drag any item on the list, it will stick and get saved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With