我正在尝试在我的WooCommerce网站上设置自定义排序,特别是我希望按所有项目的属性 - 大小排序。我找到了一个教程来帮助解决这个问题 - http://new.galalaly.me//2013/05/woocommerce-sort-by-custom-attributes/ - 而且我认为我已经很好地遵循了它,但似乎代码可能已经过时了?
我可以让网站识别我的自定义排序,但它实际上并不根据大小排序,它只是默认返回产品名称的字母顺序。但是,它只识别自从添加教程中的代码以来添加或更新的项目(将属性保存到元数据,以便我们可以按它排序)。因此,如果这些项目是较旧的项目,那么当我按大小排序时,它们甚至不会显示在结果中。很明显代码在某种程度上起作用,我似乎无法弄清楚为什么它实际上并没有按大小排序。
我已经检查过order_pa_size是否存在于数据库中并且输入的顺序正确,而且确实如此。我确定我只是错过了一些东西,但在尝试了一切之后我才能想到我很难过。任何帮助将不胜感激。这是我的代码 -
/************* Add sorting by attributes **************/
// Code from http://new.galalaly.me//2013/05/woocommerce-sort-by-custom-attributes/
/**
* Defines the criteria for sorting with options defined in the method below
*/
add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args');
function custom_woocommerce_get_catalog_ordering_args( $args ) {
global $wp_query;
// Changed the $_SESSION to $_GET
if (isset($_GET['orderby'])) {
switch ($_GET['orderby']) :
case 'pa_size' :
$args['order'] = 'ASC';
$args['meta_key'] = 'pa_size';
$args['orderby'] = 'order_pa_size';
break;
endswitch;
}
return $args;
}
/**
* Adds the sorting options to dropdown list .. The logic/criteria is in the method above
*/
add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');
function custom_woocommerce_catalog_orderby( $sortby ) {
unset($sortby['popularity']);
unset($sortby['rating']);
unset($sortby['price']);
unset($sortby['price-desc']);
$sortby['pa_size'] = 'Sort by Size - Small to Large';
return $sortby;
}
/**
* Save custom attributes as post's meta data as well so that we can use in sorting and searching
*/
add_action( 'save_post', 'save_woocommerce_attr_to_meta' );
function save_woocommerce_attr_to_meta( $post_id ) {
// Get the attribute_names .. For each element get the index and the name of the attribute
// Then use the index to get the corresponding submitted value from the attribute_values array.
if(isset($_REQUEST['attribute_names'])){
foreach( $_REQUEST['attribute_names'] as $index => $value ) {
update_post_meta( $post_id, $value, $_REQUEST['attribute_values'][$index] );
}
}
}
/************ End of Sorting ***************************/
答案 0 :(得分:0)
因此,对于它的价值,这就是我最终做的......它不是一个优雅的解决方案,但它的工作原理。并不优雅,我的意思是它的丑陋,可怕,可怕和糟糕......但它有效,我需要它才能工作,所以我会接受它。
/************* Add sorting by attributes **************/
/**
* Defines the criteria for sorting with options defined in the method below
*/
add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args');
function custom_woocommerce_get_catalog_ordering_args( $args ) {
global $wp_query;
// Changed the $_SESSION to $_GET
if (isset($_GET['orderby'])) {
switch ($_GET['orderby']) :
case 'size_desc' :
$args['order'] = 'DESC';
$args['meta_key'] = 'pa_size';
$args['orderby'] = 'meta_value';
break;
case 'size_asc' :
$args['order'] = 'ASC';
$args['meta_key'] = 'pa_size';
$args['orderby'] = 'meta_value';
break;
endswitch;
}
return $args;
}
/**
* Adds the sorting options to dropdown list .. The logic/criteria is in the method above
*/
add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');
function custom_woocommerce_catalog_orderby( $sortby ) {
unset($sortby['popularity']);
unset($sortby['rating']);
unset($sortby['price']);
unset($sortby['price-desc']);
unset($sortby['date']);
$sortby['size_desc'] = 'Sort by Size: Largest to Smallest';
$sortby['size_asc'] = 'Sort by Size: Smallest to Largest';
return $sortby;
}
/**
* Save custom attributes as post's meta data as well so that we can use in sorting and searching
*/
add_action( 'save_post', 'save_woocommerce_attr_to_meta' );
function save_woocommerce_attr_to_meta( $post_id ) {
// Get the attribute_names .. For each element get the index and the name of the attribute
// Then use the index to get the corresponding submitted value from the attribute_values array.
register_taxonomy( 'pa_size',
apply_filters( 'woocommerce_taxonomy_objects_' . 'pa_size', array('product') ),
apply_filters( 'woocommerce_taxonomy_args_' . 'pa_size', array(
'hierarchical' => true,
'show_ui' => false,
'query_var' => true,
'rewrite' => false,
) )
);
$size = '';
if($_REQUEST['attribute_names']){
$attributes = $_REQUEST['attribute_names'];
} else {
$product = new WC_Product_Simple($post_id);
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute ) :
if ( $attribute['is_taxonomy'] ) {
global $wp_taxonomies;
$array = wc_get_attribute_taxonomies();
$values = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'slugs' ) );
$size = $values[0];
}
endforeach;
}
switch(strtolower($size)):
case '2':
$new_order = 00;
break;
case '34':
$new_order = 01;
break;
case '56':
$new_order = 02;
break;
case '7':
$new_order = 03;
break;
case '810':
$new_order = 04;
break;
case '1214':
$new_order = 05;
break;
case 'tween':
$new_order = 06;
break;
case 'os':
$new_order = 07;
break;
case 'tc':
$new_order = 08;
break;
case 'xxs':
$new_order = 09;
break;
case 'xs':
$new_order = 10;
break;
case 's':
$new_order = 11;
break;
case 'm':
$new_order = 12;
break;
case 'l':
$new_order = 13;
break;
case 'xl':
$new_order = 14;
break;
case '2xl':
$new_order = 15;
break;
case '3xl':
$new_order = 16;
break;
endswitch;
update_post_meta( $post_id, 'pa_size', $new_order);
}
global $sizes_fixed;
$sizes_fixed = false;
function sizeFixer(){
global $sizes_fixed;
$resave = get_posts(array('post_type'=>'product', 'posts_per_page' => 500));
foreach($resave as $index=>$value){
save_woocommerce_attr_to_meta($resave[$index]->ID);
}
$sizes_fixed = true;
}
if($_REQUEST['size_fixer']){
sizeFixer();
}
// Function that outputs the contents of the dashboard widget
function dashboard_widget_function( $post, $callback_args ) {
global $sizes_fixed;
if($sizes_fixed){
echo('<p class="success">Sizes have been fixed</p>');
}
echo "<p>Having troubles with products sorting correctly? Click the link below to reset the size order :)</p><p><a href='/wp-admin/index.php?size_fixer=true'>Fix Size Ordering</a></p>";
}