简体   繁体   English

WooCommerce自定义排序插件

[英]WooCommerce Custom Sort Plugin

I'm trying to set up a custom sort on my WooCommerce site, specifically I want to sort by an attribute - size - on all of my items. 我正在尝试在我的WooCommerce网站上设置自定义排序,特别是我希望按所有项目的属性 - 大小排序。 I found a tutorial to help with this - http://new.galalaly.me//2013/05/woocommerce-sort-by-custom-attributes/ - and I thought I'd followed it pretty well, but it seems like the code there might be out of date? 我找到了一个教程来帮助解决这个问题 - http://new.galalaly.me//2013/05/woocommerce-sort-by-custom-attributes/ - 而且我认为我已经很好地遵循了它,但它看起来像那里的代码可能已经过时了?

I can get the site to recognize my custom sort, but it doesn't actually sort things based on the size, it just defaults back to alphabetical order of the product name. 我可以让网站识别我的自定义排序,但它实际上并不根据大小排序,它只是默认返回产品名称的字母顺序。 However, it only recognizes items that have been added or updated since the addition of the code from the tutorial (which saves the attributes to meta data so we can sort by it). 但是,它只识别自从添加教程中的代码以来添加或更新的项目(将属性保存到元数据,以便我们可以按它排序)。 So if the items are older items then when I sort by size they don't even show up in the results. 因此,如果这些项目是较旧的项目,那么当我按大小排序时,它们甚至不会显示在结果中。 So clearly the code is working to some extent, I just can't seem to figure out why it's not actually sorting by size. 很明显代码在某种程度上起作用,我似乎无法弄清楚为什么它实际上并没有按大小排序。

I've checked that order_pa_size exists in the database and has things in the correct order, and it does. 我已经检查过order_pa_size是否存在于数据库中并且具有正确顺序的东西,并且确实如此。 I'm sure I'm just missing something but after trying everything I can think of I'm stumped. 我确定我只是错过了一些东西,但在尝试了一切之后我才能想到我很难过。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Here's my code - 这是我的代码 -

/************* 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 ***************************/

So for what it's worth, this is what I ended up doing... it's not an elegant solution but it works. 所以对于它的价值,这就是我最终做的......这不是一个优雅的解决方案,但它的工作原理。 And by not elegant I mean it's ugly, horrible, terrible and bad... but it works and I needed it to work so I'll take it. 并不优雅,我的意思是它的丑陋,可怕,可怕和糟糕......但它有效,我需要它才能工作,所以我会接受它。

/************* 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>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM