【问题标题】:WooCommerce Custom Sort PluginWooCommerce 自定义排序插件
【发布时间】:2016-05-29 04:49:36
【问题描述】:

我正在尝试在我的 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 ***************************/

【问题讨论】:

    标签: php wordpress sorting woocommerce


    【解决方案1】:

    因此,就其价值而言,这就是我最终要做的......这不是一个优雅的解决方案,但它确实有效。所谓不优雅,我的意思是它丑陋、可怕、可怕和糟糕……但它有效,我需要它工作,所以我会接受它。

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

    【讨论】:

    • 你能帮帮我吗?请。我需要使用名称为:pa_ano 并且有一个数值(是西班牙语中的年份)的属性。我不能让它工作
    猜你喜欢
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    相关资源
    最近更新 更多