For instance I've size attribute and I add XL before L or M it shows up in that sequence in add to cart form.

What I would love to have is it being sorted by the order set in Product Attributes so it will be S, M, L, XL and so on.

Simple code for form_alter (this is only workaround and should go into the form class itself where sorting is done. But it should be configurable.

// Sort the add to cart form attributes by it's weight.
  $attribute_storage = \Drupal::entityTypeManager()->getStorage('commerce_product_attribute_value');
  foreach (Element::children($form['purchased_entity']['widget']) as $widget_index) {
    $widget = &$form['purchased_entity']['widget'][$widget_index];
    foreach (Element::children($widget['attributes']) as $attribute_code) {
      $attribute = &$widget['attributes'][$attribute_code];
      if (count($attribute['#options']) <= 1) {
        continue;
      }

      $attributes = $attribute_storage->loadMultiple(array_keys($attribute['#options']));
      uksort($attribute['#options'], function ($a, $b) use ($attributes) {
        return $attributes[$a]->getWeight() > $attributes[$b]->getWeight() ? 1 : -1;
      });
    }

Comments

nikunjkotecha created an issue.

ginovski’s picture

+1, this should be implemented

abdhomsi’s picture

+1

jon pollard’s picture

My project really needs to be able to do this - product sizing not being in the correct order is not a great option!

It would be great to have a patch for this - or some indication of where/how to use the code that you have posted. Any help from anybody would be really, really appreciated!

:o)

jon pollard’s picture

Investigating this further, it appears that product attributes are displayed on the add to cart form in the order that they are placed in the product variations page. So, these are set on a per product basis, rather than by a rule. That seems a bit mad to me - although it does mean I can fix products where these are not in the right order.

flyke’s picture

I had to resort to something like this:

<?php

/**
 * @file
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductAttribute;
use Drupal\commerce_product\Entity\ProductAttributeValue;

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Set stores when creating a new product.
 */
function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {

    // Validate if its a add to cart form.
    if (str_starts_with($form_id, 'commerce_order_item_add_to_cart_form_commerce_product_')) {
        // Validate if the form has a select size element.
        if (isset($form['purchased_entity']['widget'][0]['attributes']['attribute_size']['#options'])) {
            // Get the size options.
            $old_options = $form['purchased_entity']['widget'][0]['attributes']['attribute_size']['#options'];
            // Do nothing if there are no sizes.
            if (empty($old_options)) {
                return;
            }

            // Sort the size select options by the attributes' weight.
            $product_attribute = ProductAttribute::load('size');
            if (!$product_attribute) {
                return;
            }
            $product_attribute_values = $product_attribute->getValues();
            if (empty($product_attribute_values)) {
                return;
            }
            $new_options = [];
            foreach ($product_attribute_values as $key => $product_attribute_value) {
                if (array_key_exists($key, $old_options)) {
                    $new_options[$key] = $product_attribute_value->name->value;
                }
            }
            $form['purchased_entity']['widget'][0]['attributes']['attribute_size']['#options'] = $new_options;
        }
        
    }
}
jsacksick’s picture

hm... I get the request, however this would essentially mean that the variations order is no longer respected if we change this... Currently variations can be reordered from the product variations tab, if we change this, this would essentially make the variations ordering "useless".