Woocommerce通过正确的产品排序更改销售价格



帮助我更改wooccommerce中的产品销售价格/我使用挂钩:

add_filter('woocommerce_product_get_price', [$this, 'productGetPrice'], 10, 2);
add_filter('woocommerce_product_get_sale_price', [$this, 'productGetSalePrice'], 10, 2);
add_filter('woocommerce_product_variation_get_sale_price', [$this, 'productGetSalePrice'], 10, 2);
add_filter('woocommerce_product_get_date_on_sale_from', [$this, 'productGetSalePriceFrom'], 10, 2);
add_filter('woocommerce_product_get_date_on_sale_to', [$this, 'productGetSalePriceTo'], 10, 2);

它起作用https://i.stack.imgur.com/DkRkU.png.但在wooccommercewc_product_meta_lookup表中,我看到旧的价格和排序产品是不正确的。我是否应该为我想要更改售价的每个产品更改meta_price

在主题/函数中添加代码。php

在此代码中:选择按价格排序(从低到高/从高到低(后,排序结果为按销售价格排序。

add_filter( 'relevanssi_orderby', 'woocommerce_relevanssi_orderby' );
function woocommerce_relevanssi_orderby( $orderby ) {
if ( in_array( $orderby, array( 'price', 'price-desc' ), true ) ) {
global $wp_query;
$orderby = 'meta_value_num';
$wp_query->query_vars['meta_key'] = '_sale_price';
}
if ( 'date ID' === $orderby ) {
global $wp_query;
$orderby = 'post_date';
}
if ( 'popularity' === $orderby ) {
global $wp_query, $rlv_wc_order;
$orderby = 'meta_value_num';
$rlv_wc_order = 'desc';
$wp_query->query_vars['meta_key'] = 'total_sales';
}
return $orderby;
}
add_filter( 'relevanssi_order', 'woocommerce_relevanssi_order' );
function woocommerce_relevanssi_order( $order ) {
global $rlv_wc_order;
if ( $rlv_wc_order ) {
$order = $rlv_wc_order;
}
return $order;
}

最新更新