V
V
Vladislav Chernenko2021-02-05 23:54:48
WordPress
Vladislav Chernenko, 2021-02-05 23:54:48

Is it possible to sort variations ( get_available_variations() ) by price automatically?

I display in the product card (loop) a list of variations with a price:

if( $product->is_type('variable') ) {
  echo '<ul class="item-material-list">';
  foreach($product->get_available_variations() as $variation ){
    echo '<li class="item-material-item">';
    $attributes = array();
    foreach( $variation['attributes'] as $key => $value ){
      $taxonomy = str_replace('attribute_', '', $key );
      $term_name = get_term_by( 'slug', $value, $taxonomy )->name;
    }
    echo '<div class="item-material-name">'. $term_name .'</div>';
    $active_price = floatval($variation['display_price']);
    $regular_price = floatval($variation['display_regular_price']);
    if( $active_price != $regular_price ){
      $sale_price = $active_price;
    }
    echo '<div class="item-material-price">'. $active_price .'₽</div></li>';
  }
  echo '</ul>';
}


It seems that everything is fine, but it displays in the order specified through the admin panel ... there are a lot of goods, there is a terrible pun, the attributes were entered by hand in a different order. Is it possible to somehow sort by price (ASC / DESC)?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question