【问题标题】:Wordpress (Woocommerce) order by Product Dimensions (Length, Width, and then Height)按产品尺寸(长度、宽度和高度)排序的 Wordpress (Woocommerce)
【发布时间】:2019-11-27 21:21:29
【问题描述】:

我正在尝试按长度和宽度订购我的产品,它首先按长度排序,如果两个产品具有相同的值,它将按宽度排序(依此类推)。如果我能先让它工作,我想增加高度。

到目前为止,我遇到的问题是它按长度和仅按长度订购产品(加上一个错误,即使长度也不正确,即产品订单是 2.1、2 和 2.5)。

这是我从查询中得到的:

function woocommerce_catalog_orderby( $args ) {
    $args['meta_query'] = array(
        'relation' => 'AND',
        'length_clause' => array(
            'key'     => '_length',
            'type' => 'DECIMAL',
            'orderby' => 'meta_value_num',
        ),
        'width_clause' => array(
            'key'     => '_width',
            'type' => 'DECIMAL',
            'orderby' => 'meta_value_num',
        ),
    );

    $args['orderby'] = array(
        'length_clause' => 'ASC',
        'width_clause' => 'ASC',
    );

    return $args;
}
add_filter('woocommerce_get_catalog_ordering_args', 'woocommerce_catalog_orderby');

我确信我错过了一些我目前看不到的明显东西。非常感谢任何帮助!

【问题讨论】:

  • 此搜索的另一个死胡同。为什么按两个不同的元键排序如此困难?
  • @freethebees 从我读过的所有内容来看,使用 Wordpress 的过滤器是不可能的。我还没有开始走这条路(我一直在推迟,tbh),但我认为下一步就是使用修改后的 SQL 查询:codex.wordpress.org/Class_Reference/…。 ://

标签: wordpress woocommerce


【解决方案1】:

在您的情况下,您必须在为 woocommerce 添加过滤器时添加优先级。 这是按元值排序的工作代码:

/**

'''
 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 color (string)
                    case 'pa_color':
                        //it may have multiple color (eg. black, brown, maroon, white) but we'll take only the first color.
                        if (!empty($attribute_values[$key][0])) {
                            update_post_meta($post_id, 'pa_color', $attribute_values[$key][0]);
                        }
                        break;
                    //for lenght (int)
                    case 'pa_length':
                        if (!empty($attribute_values[$key][0])) {
                            update_post_meta($post_id, 'pa_length', $attribute_values[$key][0]);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
    
    add_action( 'save_post', 'wh_save_product_custom_meta', 10, 3);
'''   

【讨论】:

    【解决方案2】:

    可以添加自定义排序:请按照步骤操作并替换您的元值

    https://docs.woocommerce.com/document/custom-sorting-options-ascdesc/

    【讨论】:

      猜你喜欢
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      相关资源
      最近更新 更多