I have a Woocommerce shop where the different attributes / variations are ordered alphabetically when you click the 'select' on a product page. I'm currently trying to figure out how I can sort them based on the ordening in the backend.
我有一家Woocommerce商店,在那里,当你点击产品页面上的“选择”时,不同的属性/变体按字母顺序排列。我目前正试图找出如何根据后端军械进行排序。
Specifically: Currently Small is placed after Large, which not feels all that intuitive.
具体来说:当前小的被放在大的后面,这感觉不那么直观。
Any help would be more than welcome. I've been googling for an hour now and can't seem to find the solution.
任何帮助都是不受欢迎的。我在谷歌上搜索了一个小时,似乎找不到解决办法。
The HTML / PHP of my current template is as follows:
我当前模板的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>';
}
}
}
?>
2 个解决方案
#1
9
You might have figured out the solution by now as it is quite simple( although I got to know after wasting 5-6 hrs), but if you haven't, here it is.
你可能已经找到了解决方案,因为它非常简单(尽管我在浪费了5-6个小时之后才知道),但是如果你还没有找到的话,就在这里。
- From the admin panel, select Products >> Attributes.
- 在管理面板中,选择Products >>属性。
- There, edit the Size attribute & change the Default sort order to "Custom Ordering" & save it.
- 在那里,编辑Size属性并将默认的排序顺序更改为“自定义排序”并保存它。
- Go back to the Attributes page & click on "Configure items" for Size attribute. Now on the table of values, drag the items up or down in any order you want to display on the front-end.
- 回到属性页面,单击“为Size属性配置项”。现在,在值表上,按您希望在前端显示的任何顺序,将项目向上或向下拖动。
For ex. in your case, drag Small to the top of the table & Large to the bottom, that's it. There is no save option, so don't get confused with it, wherever you drag any item on the list, it will stick and get saved.
对于ex.在您的情况下,将小的拖到表的顶部&大到底部,就是这样。没有save选项,所以不要对它感到困惑,无论您将列表中的任何项目拖到哪里,它都会粘住并保存。
#2
-3
I´ve got a solution for your question. Just login to FTP, then go to the folder /wp-content/plugins/woocommerce/includes/admin/post-types/meta-boxes/class-wc-meta-box-product-data.php
你的问题我´已经有一个解决方案。只需登录到FTP,然后转到文件夹/wp-content/plugins/woocommerce/包含/管理/后类型/元框/class-wc-meta-box- data.php。
In that line no 885 replace with following array:
在第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
)
)
);
Currently it will sort attributes manually.
目前,它将手动排序属性。
#1
9
You might have figured out the solution by now as it is quite simple( although I got to know after wasting 5-6 hrs), but if you haven't, here it is.
你可能已经找到了解决方案,因为它非常简单(尽管我在浪费了5-6个小时之后才知道),但是如果你还没有找到的话,就在这里。
- From the admin panel, select Products >> Attributes.
- 在管理面板中,选择Products >>属性。
- There, edit the Size attribute & change the Default sort order to "Custom Ordering" & save it.
- 在那里,编辑Size属性并将默认的排序顺序更改为“自定义排序”并保存它。
- Go back to the Attributes page & click on "Configure items" for Size attribute. Now on the table of values, drag the items up or down in any order you want to display on the front-end.
- 回到属性页面,单击“为Size属性配置项”。现在,在值表上,按您希望在前端显示的任何顺序,将项目向上或向下拖动。
For ex. in your case, drag Small to the top of the table & Large to the bottom, that's it. There is no save option, so don't get confused with it, wherever you drag any item on the list, it will stick and get saved.
对于ex.在您的情况下,将小的拖到表的顶部&大到底部,就是这样。没有save选项,所以不要对它感到困惑,无论您将列表中的任何项目拖到哪里,它都会粘住并保存。
#2
-3
I´ve got a solution for your question. Just login to FTP, then go to the folder /wp-content/plugins/woocommerce/includes/admin/post-types/meta-boxes/class-wc-meta-box-product-data.php
你的问题我´已经有一个解决方案。只需登录到FTP,然后转到文件夹/wp-content/plugins/woocommerce/包含/管理/后类型/元框/class-wc-meta-box- data.php。
In that line no 885 replace with following array:
在第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
)
)
);
Currently it will sort attributes manually.
目前,它将手动排序属性。