• Resolved graftedin

    (@graftedin)


    For some reason custom sorting of the product variations is not working. It seems that its just looking at the terms for the order and never looks back to the variations in posts.

    Here is a messy fix that seems to work for my instance.

    In wc-template-functions.php in the wc_dropdown_variation_attribute_options function at line 2069 add:

    // Make sure that we have visible children.
        if(isset($product->children['visible']) && is_array($product->children['visible']) && isset($attribute)){
          $options_by_menu_order = array();
          foreach($product->children['visible'] as $temp_option){
            $temp_meta = get_post_meta($temp_option, "attribute_" . $attribute, true);
            if(!in_array($temp_meta,$options_by_menu_order)){
              $options_by_menu_order[] = $temp_meta;
            }
          }
        }

    Then at line 2083 add:

    if(isset($options_by_menu_order) && is_array($options_by_menu_order) && $product && taxonomy_exists( $attribute )){
    				// Get terms if this is a taxonomy - ordered. We need the names too.
    				$terms = wc_get_product_terms( $product->id, $attribute, array( 'fields' => 'all' ) );
            for($i = 0; $i < count($options_by_menu_order); $i++){
      				foreach ( $terms as $term ) {
      					if ( in_array( $term->slug, $options ) && $term->slug == $options_by_menu_order[$i]) {
      						$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 ) ) . '</option>';
      					}
      				}
    
            }
    
          }

    and then make sure the next if is turned to an else if.

    I’m going on the assumption that the post_meta for the variation will match the stub for the term…..but I don’t know if that is true in all instances.

    Please consider adding a slightly cleaner version of this to your next version.

    Let me know if you have any questions.

    https://wordpress.org/plugins/woocommerce/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    We don’t sort variations on the frontend because variations can be made of multiple attributes.

    Reorder your terms – Products > Attributes – instead.

    Thread Starter graftedin

    (@graftedin)

    Then why provide away in Products > A Product > Product Data > Variations to adjust the order? Remove that ability and then provide a link to Attributes for sorting. At the very least provide that link.

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    The sorting is there for those that would like the variations to be in a logical order in the backend. There is no need to remove the ability to sort them.

    Thread Starter graftedin

    (@graftedin)

    I also said “at the very least provide that link.” There is a tooltip that shows when you mouse over the drag and drop feature that says “Drag and drop, or click to set admin variation order.” I don’t think there is a character limit so all you have to do is add “Change front end order by going to Products > Attributes….”

    But I think that you guys have made this more complicated than it needs to be by making us adjust this sort order twice……my code closes that loop.

    Plugin Contributor Mike Jolley

    (@mikejolley)

    The order of variations also determines the order they ‘match’ if you have overlap.

    There are discussions to merge the ordering systems into this same area. Nothing short term.

    Thread Starter graftedin

    (@graftedin)

    Yeah, I wouldn’t expect anything over night.

    Can you at least put something in that tooltip to tip people to where to order them for the front end. I know this now but others might still be fooled by this.

    This seems to fix it for me without any additional modifications.
    Joe

    function reorder_color_options( $array, $number,$taxonomy, $args ) { 
    if ($taxonomy == "pa_color" and is_super_admin()){ 
    //error_log(print_r($args,true));
    global $wpdb;
    $results = $wpdb->get_results( "select min(menu_order) menu_order, meta_value from wp_posts
               join wp_postmeta
               on wp_postmeta.post_id = wp_posts.ID and wp_postmeta.meta_key = 'attribute_pa_color'
               where wp_posts.post_parent = ".$number."
               group by meta_value
               order by min(menu_order)", OBJECT );
    //echo print_r($results,true);
    foreach ($results as $colororder){
    //	echo $colororder->meta_value. "<br>";
    	foreach ($array as $key=>$color){
    		if ($color->slug == $colororder->meta_value){
    //			echo "Found Match<br>";
    			$sortedarray[]=$color;
    			unset($array[$key]);
    		}
    	}
    }
    $array = array_merge($sortedarray, $array);
    }
    return $array; 
    };
    add_filter( 'woocommerce_get_product_terms', 'reorder_color_options', 10,4 );
    
    • This reply was modified 9 years, 2 months ago by rcojoe.
    • This reply was modified 9 years, 2 months ago by rcojoe. Reason: fixed formatting
    Thread Starter graftedin

    (@graftedin)

    Nice…… I’ll give this a shot next time.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Variations Not Using menu_order’ is closed to new replies.