Php WooCommerce自定义排序插件
我正试图在我的WooCommerce网站上设置一个自定义排序,特别是我想在我的所有项目上按属性大小排序。我找到了一个教程来帮助我——我认为我已经很好地遵循了它,但是那里的代码似乎已经过时了 我可以让网站识别我的自定义排序,但它实际上并没有根据大小进行排序,它只是默认回到产品名称的字母顺序。但是,它只识别自添加教程中的代码后添加或更新的项目(将属性保存到元数据中,以便我们可以按其排序)。因此,如果项目是较旧的项目,那么当我按大小排序时,它们甚至不会显示在结果中。很明显,代码在某种程度上是有效的,我只是不明白为什么它实际上没有按大小排序 我已经检查了数据库中是否存在order_pa_size,并且其顺序正确,确实如此。我确信我只是错过了一些东西,但在尝试了我能想到的一切之后,我被难住了。任何帮助都将不胜感激。这是我的密码-Php WooCommerce自定义排序插件,php,wordpress,sorting,woocommerce,Php,Wordpress,Sorting,Woocommerce,我正试图在我的WooCommerce网站上设置一个自定义排序,特别是我想在我的所有项目上按属性大小排序。我找到了一个教程来帮助我——我认为我已经很好地遵循了它,但是那里的代码似乎已经过时了 我可以让网站识别我的自定义排序,但它实际上并没有根据大小进行排序,它只是默认回到产品名称的字母顺序。但是,它只识别自添加教程中的代码后添加或更新的项目(将属性保存到元数据中,以便我们可以按其排序)。因此,如果项目是较旧的项目,那么当我按大小排序时,它们甚至不会显示在结果中。很明显,代码在某种程度上是有效的,我
/************* 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 ***************************/
所以不管怎样,这就是我最后做的。。。这不是一个优雅的解决方案,但它是有效的。我说的不优雅是指它丑陋、可怕、可怕和糟糕。。。但它是有效的,我需要它来发挥作用,所以我接受它
/************* 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>";
}
/**********添加按属性排序**************/
/**
*使用以下方法中定义的选项定义排序标准
*/
添加过滤器('woocommerce\u get\u catalog\u ordering\u args','custom\u woocommerce\u get\u catalog\u ordering\u args');
函数自定义\商业\获取\目录\排序\参数($args){
全局$wp_查询;
//将$\u会话更改为$\u GET
if(isset($\u GET['orderby'])){
开关($\u GET['orderby']):
“尺寸描述”案例:
$args['order']='DESC';
$args['meta_key']='pa_size';
$args['orderby']='meta_value';
打破
“尺寸”案例:
$args['order']='ASC';
$args['meta_key']='pa_size';
$args['orderby']='meta_value';
打破
终端开关;
}
返回$args;
}
/**
*将排序选项添加到下拉列表中。。逻辑/标准采用上述方法
*/
添加过滤器('woocommerce\u catalog\u orderby'、'custom\uWooCommerce\u catalog\u orderby');
功能自定义\商业\目录\订购人($sortby){
未设置($sortby['popularity']);
未设置($sortby[“评级]);
未结算($sortby[“价格]);
未设置($sortby['price-desc']);
未结算($sortby['date']);
$sortby['size_desc']=“按大小排序:从大到小”;
$sortby['size_asc']=“按大小排序:从最小到最大”;
返回$sortby;
}
/**
*将自定义属性保存为post的元数据,以便我们可以在排序和搜索中使用
*/
添加操作('save_post'、'save_商业'u attr_to_meta');
功能保存到元($post\u id){
//获取属性名称。对于每个元素,获取属性的索引和名称
//然后使用索引从属性值数组中获取相应的提交值。
登记册分类法('pa_size',
应用过滤器('woocommerce\u taxonomy\u objects'.'pa\u size',数组('product')),
应用\u过滤器('woocommerce\u taxonomy\u args.'pa\u size',数组(
“分层”=>正确,
'show_ui'=>false,
'query_var'=>true,
“重写”=>false,
) )
);
$size='';
if($\u请求['attribute\u names']){
$attributes=$\u请求['attribute\u名称];
}否则{
$product=新WC\U product\U Simple($post\U id);
$attributes=$product->get_attributes();
foreach($attributes作为$attribute):
if($attribute['is_taxonomy'])){
全球$wp_分类法;
$array=wc_get_attribute_taxonomies();
$values=wc_get_product_terms($product->id,$attribute['name'],数组('fields'=>'slugs'));
$size=$values[0];
}
endforeach;
}
交换机(strtolower($size)):
案例“2”:
$new_order=00;
打破
案例'34':
$new_order=01;
打破
案件'56':
$new_order=02;
打破
案例“7”:
$new_order=03;
打破
案例“810”:
$new_order=04;
打破
“1214”案:
$new_订单=05;
打破
案例“tween”:
$new_order=06;
打破
案例“os”:
$new_order=07;
打破
案例“tc”:
$new_order=08;
打破
案例“xxs”:
$new_order=09;
打破
案例“xs”:
$new_订单=10;
打破
案例s:
$new_订单=11;
打破
案例“m”:
$new_订单=12;
打破
案例“l”:
$new_订单=13;
打破
案例“xl”:
$new_订单=14;
打破
案例“2xl”:
$new_订单=15;
打破
案例“3xl”:
$new_订单=16;
打破
终端开关;
更新发布元($post\u id,$pa\u size',$new\u order);
}
全球美元固定汇率;
$sizes\U fixed=错误;
函数sizeFixer(){
全球美元固定汇率;
$resave=get_posts(数组('post_type'=>product','posts_per_page'=>500));
法罗群岛