Please submit a private ticket if you need to share sensitive information, such as license details and admin credentials.

Okay
  Public Ticket #3355782
Order Options By Attribute Values (e.g. By Color)
Closed

Comments

  •  3
    Jan started the conversation

    Hi WPCLEVER team,

    in your plugin "WPC Composite Products for WooCommerce" you can sort options by name, by price, … .

    Is it possible to sort the options by attribute values?

    Example:

    3 shirts in the colors "blue", "green" and "red" are listed like this:

    Shirt A Blue
    Shirt A Green
    Shirt A Red
    Shirt B Blue
    Shirt B Green
    Shirt B Red
    Shirt C Blue
    Shirt C Green
    Shirt C Red

    I would like to sort the output by the color attribute:

    Shirt A Blue
    Shirt B Blue
    Shirt C Blue
    Shirt A Green
    Shirt B Green
    Shirt C Green
    Shirt A Red
    Shirt B Red
    Shirt C Red

    The code could look something like this:

    function wooco_options_order_by_color ( $orderby )
    {
        $orderby = "
            CASE 
                WHEN {$wpdb->term_taxonomy}.taxonomy  = 'pa_color' THEN '1'
            ELSE
                2
            END ASC,
            {$wpdb->terms}.term_order ASC";
        return $orderby;
    }
    add_filter( 'wooco_options_order', 'wooco_options_order_by_color' );

    If this is to much work, can you tell me the filter, that would be the right one for this kind of customization?

    Thank you very much,
    Jan

  •  767
    Janilyn replied

    Hi Jan,

    Thank you for contacting WPClever Support Forum. 

    We’ve received your ticket and assigned one of our developers to help you with that. 

    He’ll be looking into your question and responding with specific instructions as soon as possible. 

    Please stay patient as we're having very heavy workloads, it might take some time for our developers to reach back.

    Best regards.

    Janilyn T. - WPClever Support Agent

  •  1,226
    Dustin replied

    Hi Jan,

    You can use two filter hooks 'wooco_product_data' and 'wooco_get_products':

    add_filter( 'wooco_product_data', function ( $data, $product ) {
        if ( is_a( $product, 'WC_Product_Variation' ) ) {
            $data['color'] = $product->get_attribute( 'pa_color' );
        } else {
            $data['color'] = '';
        }
        return $data;
    }, 99, 2 );
    add_filter( 'wooco_get_products', function ( $products ) {
        array_multisort( array_column( $products, 'color' ), SORT_ASC, $products );
        return $products;
    }, 99 );

    And the result https://app.screencast.com/ck7oEn4rP5sR4

    Best regards,
    Dustin

  •  3
    Jan replied

    Hello Dustin,

    thank you very much for your helpful reply!

    I did some modifications, to sort the values by the custom order in the WooCommerce attribute settings in the backend.

    If anyone needs the same:

    add_filter( 'wooco_product_data', function ( $data, $product ) {
        $data['order'] = 999;
        if ( is_a( $product, 'WC_Product_Variation' ) )
        {
            global $wpdb;
            $product_id = $product->get_id();
            $sql = $wpdb->prepare( "SELECT IFNULL(tm.`meta_value`, 0) AS `order` FROM `{$wpdb->prefix}postmeta` AS pm LEFT JOIN `{$wpdb->prefix}terms` AS t ON ( t.`slug` = pm.`meta_value` ) LEFT JOIN `{$wpdb->prefix}termmeta` AS tm ON ( tm.`term_id` = t.`term_id` AND tm.`meta_key` = 'order' ) WHERE pm.`post_id` LIKE %d AND pm.`meta_key` = 'attribute_pa_color' LIMIT 1", $product_id );
            $data['order'] = $wpdb->get_results( $sql )[0]->order;
        }
        return $data;
    }, 99, 2 );
    add_filter( 'wooco_get_products', function ( $products ) {
        $column_1 = array_column( $products, 'order' );
        $column_2 = array_column( $products, 'name' );
        
        array_multisort( $column_1, SORT_ASC, $column_2, SORT_ASC, $products );
        return $products;
    }, 99 );
    

    Have a great day. :-)

    Jan

  •  1,226
    Dustin replied

    Thank you so much for the code!

    I'll give it to those who have the same requirements as you.

    Best regards,
    Dustin

  •  1
    chrischacha replied

    If you have a list of items that have attributes such as color, size, or other values, and you want to order them by attribute values, there are several ways to do this. you can import the list of items into a spreadsheet such as Excel or Google Sheets and sort the list scrap metal prices by the attribute column. To do this, select the column that contains the attribute values and click on the sort function to sort the list by that column. if you have a large dataset or need to perform more complex sorting operations, you can use a database or programming language such as SQL or Python to sort the list by attribute values. You can create a database table with columns for the items and their attributes, and then use SQL queries to sort the list by attribute values. 

  • [deleted] replied

    To order options by attribute values, such as color, you can follow these steps:

    1. Define the attribute you want to order the options by. For example, let's use "color."
    2. Assign each option a value for the attribute you defined. For example, you could assign the values "red," "green," and "blue" to different options based on their color. Scaler Academy Review
    3. Create a list of options, where each option is represented by an object or a data structure that contains the attribute value. For example:

      Option 1: { color: "red", ... } Option 2: { color: "green", ... } Option 3: { color: "blue", ... }

    4. Use a sorting algorithm or function to sort the options based on their attribute values. In this case, you would sort the options based on the "color" attribute. The specific sorting algorithm or function you use will depend on the programming language or framework you're working with.
    5. After sorting, the options will be ordered based on their attribute values. In this example, the options would be ordered as follows:

      Option 1: { color: "blue", ... } Option 2: { color: "green", ... } Option 3: { color: "red", ... }

    By following these steps, you can order options based on any attribute value, not just color. Simply replace "color" with the desired attribute, and assign appropriate values to each option based on that attribute.


  • mateomateohudso replied

    Determine the specific criteria for sorting the options. For example, you might want to sort them according to lesturf alphabetically by color name or follow a specific color hierarchy (e.g., red, blue, green, etc.). Depending on the programming language or framework you're using, you can utilize built-in sorting functions or implement custom sorting algorithms. Most programming languages provide sorting functions that allow you to define custom comparison criteria.