如何根据计算价值对产品进行排序?(自定义发布元、价格、选项)

时间:2020-07-25 作者:kostikovmu

我有选择:wz\\u currency\\u dollar,wz\\u currency\\u euro。并发布元货币。在以美元或欧元定义的产品管理面板价格中,选择相应的post meta。在根据下一个公式重新计算html的前端价格中:

if (get_post_meta($product->get_id(), \'_wz_currency\', true) == \'DOLL\') {

    $price = ( (int) $product->get_price() * (int) get_option(\'wz_currency_dollar\') ) . get_woocommerce_currency_symbol();

} else if (get_post_meta($product->get_id(), \'_wz_currency\', true) == \'EUR\') {

    $price = ( (int) $product->get_price() * (int) get_option(\'wz_currency_euro\') ) . get_woocommerce_currency_symbol();
}
如果我按价格对产品进行分类,结果会出乎意料。举个简单的例子:

product_1 200 (price=10 * wz_currency_euro=20)
product_2 400 (price=20 * wz_currency_euro=20)
product_3 180 (price=30 * wz_currency_dollar=6)
但我想:

product_3 180 (price=30 * wz_currency_dollar=6)
product_1 200 (price=10 * wz_currency_euro=20)
product_2 400 (price=20 * wz_currency_euro=20)
如何更改排序?

1 个回复
最合适的回答,由SO网友:kostikovmu 整理而成

首先,我在查询中添加了表posmeta

function add_join_clause_to_view_sql( $join, $wpq ) {
  global $wpdb;
  if( $wpq->query[\'orderby\'] === \'price\' || $wpq->query[\'orderby\'] === \'price-desc\') {
    $post_meta_table_name = $wpdb->postmeta;
    $post_table_name = $wpdb->posts;

    $join .= "
                LEFT JOIN
                    $post_meta_table_name
                      ON $post_meta_table_name.post_id = $post_table_name.id
            ";
  }

  return $join;
}

add_filter(\'posts_join\', \'add_join_clause_to_view_sql\', 10, 2);
其次,我在WHERE语句中添加了meta\\u键

function add_where_clause_to_view_sql( $where, $wpq ) {
  global $wpdb;
  if( $wpq->query[\'orderby\'] === \'price\' || $wpq->query[\'orderby\'] === \'price-desc\') {
    $post_meta_table_name = $wpdb->postmeta;
    $where .= "
                AND
                    $post_meta_table_name.meta_key = \'_wz_currency\'
            ";
  }
  return $where;
}

add_filter( \'posts_where\' , \'add_where_clause_to_view_sql\', 10, 2 );
第三,我添加了一个带条件的计算字段

function filter_function_name_4359( $fields, $wpq ){

  global $wpdb;
  if($wpq->query[\'orderby\'] === \'price\' || $wpq->query[\'orderby\'] === \'price-desc\') {
    $post_meta_table_name = $wpdb->postmeta;
    $wc_product_meta_lookup = \'wc_product_meta_lookup\';
    $wz_currency_dollar = (int) get_option(\'wz_currency_dollar\');
    $wz_currency_euro = (int) get_option(\'wz_currency_euro\');

    $fields .= ", CAST(
CASE 
   WHEN $post_meta_table_name.meta_value = \'DOLL\' THEN $wc_product_meta_lookup.min_price * $wz_currency_dollar
   WHEN $post_meta_table_name.meta_value = \'EUR\' THEN $wc_product_meta_lookup.min_price * $wz_currency_euro
   WHEN $post_meta_table_name.meta_value = \'RUB\' THEN $wc_product_meta_lookup.min_price END AS decimal(15,2)
) AS CalculatedPrice";
  }
  return $fields;
}

add_filter( \'posts_fields\', \'filter_function_name_4359\', 10, 2 );
最后,我添加了orderby语句和calculate字段

function orderByPriceTimesCurrency($orderby, $wpq) {
  if( $wpq->query[\'orderby\'] === \'price\') {

    $orderby = \'
      CalculatedPrice ASC
    \';
  }
  if( $wpq->query[\'orderby\'] === \'price-desc\') {

    $orderby = \'
      CalculatedPrice DESC
    \';
  }


  return $orderby;
}

add_filter(\'posts_orderby_request\', \'orderByPriceTimesCurrency\', 10, 2);

相关推荐

从商店页面的前面部分删除Storefront-Sorting div

我已经发现,我可以使用以下方法从商店页面中删除排序下拉菜单和分页:function delay_remove() { remove_action( \'woocommerce_after_shop_loop\', \'woocommerce_catalog_ordering\', 10 ); remove_action( \'woocommerce_before_shop_loop\', \'woocommerce_catalog_ordering\', 10 ); remove_