更改Woocommerce商店中产品页面属性/变体的字母排序


Change alphabetical sorting of attributes / variations on product page in Woocommerce shop

我有一个Woocommerce商店,当你点击产品页面上的"选择"时,不同的属性/变化按字母顺序排序。我目前正试图弄清楚如何根据后端顺序对它们进行排序。

具体来说:目前Small被放在Large后面,感觉不太直观。

任何帮助都非常受欢迎。我已经用谷歌搜索了一个小时了,似乎还是找不到解决办法。

我当前模板的HTML/PHP如下:

<?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>';
            }
        }
    }
?>

你现在可能已经找到了解决方案,因为它很简单(尽管我浪费了5-6个小时才知道),但如果你还没有,这里是。

  1. 在管理面板中选择"Products>> Attributes"。
  2. 在这里,编辑Size属性&将默认排序顺序更改为"自定义排序"&保存它。
  3. 返回属性页面&点击大小属性的"配置项目"。现在,在值表上,按您希望在前端显示的任何顺序向上或向下拖动项。

例如,在您的例子中,将Small拖到表的顶部&大号放到底部,就这样。没有保存选项,所以不要混淆它,无论你在列表中拖动任何项目,它都会粘住并保存。

我有一个解决你问题的办法。登录FTP,然后进入/wp-content/plugins/woocommerce/includes/admin/post-types/meta-boxes/class-wc-meta-box-product-data.php

文件夹

第885行用以下数组替换

$args = array(
    'post_type'=>'product_variation',
    'post_status' => array( 'private', 'publish' ),
    'posts_per_page' => -1,
    'meta_key' => 'attribute_pa_woo-color', //rename with your attributes
    'post_parent' => $post->ID,
    'meta_query' => array(
        array(
            'key' => 'attribute_pa_woo-color' //rename with your attributes
        ),
        array(
            'key' => 'attribute_pa_woo-size' //rename with your attributes
        )
    )
);

目前会手动排序属性