按属性值排序

时间:2017-01-30 14:33:59

标签: php wordpress woocommerce

我正在开发基于WooCommerce的网站,并试图找出如何通过属性值订购我的产品。我在网上看了,似乎找不到任何解决方案。我的属性具有基于数字的值,我希望能够从低到高排序。希望这有任何意义。

非常感谢您阅读本文并度过愉快的一天,

Sjoerd

2 个答案:

答案 0 :(得分:0)

//Save product attributes to post metadata when a product is saved.
add_action( 'save_post', 'wh_save_product_custom_meta', 10, 3 );
function wh_save_product_custom_meta($post_id, $post, $update)
{
    $post_type = get_post_type($post_id);
    // If this isn't a 'product' post, don't update it.
    if ($post_type != 'product')
        return;

    if (!empty($_POST['attribute_names']) && !empty($_POST['attribute_values']))
    {
        $attribute_names = $_POST['attribute_names'];
        $attribute_values = $_POST['attribute_values'];
        foreach ($attribute_names as $key => $attribute_name)
        {
            switch ($attribute_name)
            {
                //for length (int)
                case 'pa_length':
                    if (!empty($attribute_values[$key][0]))
                    {
                        update_post_meta($post_id, 'pa_length', $attribute_values[$key][0]);
                    }
                    break;
            }
        }
    }
}

//Main ordering logic for orderby attribute
add_filter('woocommerce_get_catalog_ordering_args', 'wh_catalog_ordering_args');

function wh_catalog_ordering_args($args)
{
    global $wp_query;
    if (isset($_GET['orderby']))
    {
        switch ($_GET['orderby'])
        {
            //for attribute/taxonomy=pa_length
            case 'pa-length-asc' :
                $args['order'] = 'ASC';
                $args['meta_key'] = 'pa_length';
                $args['orderby'] = 'meta_value_num'; //because length is an int
                break;
            case 'pa-length-desc' :
                $args['order'] = 'DESC';
                $args['meta_key'] = 'pa_length';
                $args['orderby'] = 'meta_value_num'; //because length is an int
                break;
        }
    }
    return $args;
}

//Lets add the created sorting order to the dropdown list.
add_filter('woocommerce_default_catalog_orderby_options', 'wh_catalog_orderby');
add_filter('woocommerce_catalog_orderby', 'wh_catalog_orderby');

function wh_catalog_orderby($sortby)
{
    $sortby['pa-length-asc'] = 'Sort by Length: Small - Large';
    $sortby['pa-length-desc'] = 'Sort by Length: Large - Small';
    return $sortby;
}

代码放在活动子主题(或主题)的function.php文件中。或者也可以在任何插件php文件中。
代码经过测试并有效。

请注意:由于您没有提及任何字段名称,所以我写了一个更通用的答案,如果您想要特定答案,请更新您的问题使用您的属性名称/ slug以及它将保留的样本值。

参考:Custom Order By Attributes

答案 1 :(得分:0)

//Save product attributes to post metadata when a product is saved.
add_action( 'save_post', 'wh_save_product_custom_meta', 10, 3 );
function wh_save_product_custom_meta($post_id, $post, $update)
{
    $post_type = get_post_type($post_id);
    // If this isn't a 'product' post, don't update it.
    if ($post_type != 'product')
        return;

    if (!empty($_POST['attribute_names']) && !empty($_POST['attribute_values']))
    {
        $attribute_names = $_POST['attribute_names'];
        $attribute_values = $_POST['attribute_values'];
        foreach ($attribute_names as $key => $attribute_name)
        {
            switch ($attribute_name)
            {
                //for koppel (int)
                case 'pa_nominaal-koppel-nm':
                    if (!empty($attribute_values[$key][0]))
                    {
                        update_post_meta($post_id, 'pa_nominaal-koppel-nm', $attribute_values[$key][0]);
                    }
                    break;
            }
        }
    }
}

//Main ordering logic for orderby attribute
add_filter('woocommerce_get_catalog_ordering_args', 'wh_catalog_ordering_args');

function wh_catalog_ordering_args($args)
{
    global $wp_query;
    if (isset($_GET['orderby']))
    {
        switch ($_GET['orderby'])
        {
            //for attribute/taxonomy=pa_nominaal-koppel-nm
            case 'pa-nominaal-koppel-nm-asc' :
                $args['order'] = 'ASC';
                $args['meta_key'] = 'pa_nominaal-koppel-nm';
                $args['orderby'] = 'meta_value_num'; //because length is an int
                break;
            case 'pa-nominaal-koppel-nm-desc' :
                $args['order'] = 'DESC';
                $args['meta_key'] = 'pa_nominaal-koppel-nm';
                $args['orderby'] = 'meta_value_num'; //because length is an int
                break;
        }
    }
    return $args;
}

//Lets add the created sorting order to the dropdown list.
add_filter('woocommerce_default_catalog_orderby_options', 'wh_catalog_orderby');
add_filter('woocommerce_catalog_orderby', 'wh_catalog_orderby');

function wh_catalog_orderby($sortby)
{
    $sortby['pa-nominaal-koppel-nm-asc'] = 'Sorteer op koppel: Small - Large';
    $sortby['pa-nominaal-koppel-nm-desc'] = 'Sorteer op koppel: Large - Small';
    return $sortby;
}

所以我这样做了,“nominaal-koppel-nm”是应该分类的分类法。我重新添加了一堆产品进行测试,但它做了一些排序,但不确定它的排序。因为该属性至少没有按值排序。也许我正在做一些非常错误的事情,但我似乎无法解决这个问题。