WooCommerce - 通过销售自定义计算订购相关产品
WooCommerce - Ordering related products by sales custom calculation
Woocommerce 相关产品模板位于:
/wp-content/plugins/woocommerce/templates/single-product/related.php
但它目前按随机排序。
我想通过 'total_sales' + 'sales_count'
订购(它们是 2 meta_keys 保存整数值和 'sales_count'
是一个额外的 自定义 字段)。
这里是查询:
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => 4,
'orderby' => 'total_sales',
'order' => 'DESC',
'post__in' => $related
) );
$products = new WP_Query( $args );
以上查询排序产品购买 total_sales 但我需要使用 total_sales 的总和对其进行排序和 sales_count?
有没有办法做到这一点?。
谢谢
I doesn't understand very well the purpose of $soldty = get_post_meta( $product->id, 'sales_count', true );
, as default 'total_sales'
for a product makes already the calculation of all items sold (the item quantity sold is added each time to the count).
无论如何,如果你真的需要这个计算,最好的选择是制作一个函数,create/update一个新的自定义字段,对于每个产品,计算:
add_action( 'wp_loaded', 'products_update_total_sales_calculation' );
function products_update_total_sales_calculation(){
$sales = array();
$all_products = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
) );
foreach($all_products as $item) {
$units_sold = get_post_meta( $item->ID, 'total_sales', true );
$soldty = get_post_meta( $item->ID, 'sales_count', true );
if(empty($soldty)) $soldty = 0;
$result = $units_sold + $soldty;
update_post_meta( $item->ID, 'global_sales_count', $result );
}
}
此函数计算产品销售额和create/update自定义字段'global_sales_count'
,其中包含计算值。
现在您可以根据这个新的自定义字段自定义查询 'orderby'
参数:
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => 4,
'meta_key' => 'global_sales_count', // <== <== the new custom field
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post__in' => $related
) );
$products = new WP_Query( $args );
如果您不需要计算,'meta_key'
值将更改为现有 'total_sales'
产品 meta_key
,这样:
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => 4,
'meta_key' => 'total_sales', // <== <== the existing meta_key
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post__in' => $related
) );
$products = new WP_Query( $args );
Woocommerce 相关产品模板位于:
/wp-content/plugins/woocommerce/templates/single-product/related.php
但它目前按随机排序。
我想通过 'total_sales' + 'sales_count'
订购(它们是 2 meta_keys 保存整数值和 'sales_count'
是一个额外的 自定义 字段)。
这里是查询:
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => 4,
'orderby' => 'total_sales',
'order' => 'DESC',
'post__in' => $related
) );
$products = new WP_Query( $args );
以上查询排序产品购买 total_sales 但我需要使用 total_sales 的总和对其进行排序和 sales_count?
有没有办法做到这一点?。
谢谢
I doesn't understand very well the purpose of
$soldty = get_post_meta( $product->id, 'sales_count', true );
, as default'total_sales'
for a product makes already the calculation of all items sold (the item quantity sold is added each time to the count).
无论如何,如果你真的需要这个计算,最好的选择是制作一个函数,create/update一个新的自定义字段,对于每个产品,计算:
add_action( 'wp_loaded', 'products_update_total_sales_calculation' );
function products_update_total_sales_calculation(){
$sales = array();
$all_products = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
) );
foreach($all_products as $item) {
$units_sold = get_post_meta( $item->ID, 'total_sales', true );
$soldty = get_post_meta( $item->ID, 'sales_count', true );
if(empty($soldty)) $soldty = 0;
$result = $units_sold + $soldty;
update_post_meta( $item->ID, 'global_sales_count', $result );
}
}
此函数计算产品销售额和create/update自定义字段'global_sales_count'
,其中包含计算值。
现在您可以根据这个新的自定义字段自定义查询 'orderby'
参数:
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => 4,
'meta_key' => 'global_sales_count', // <== <== the new custom field
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post__in' => $related
) );
$products = new WP_Query( $args );
如果您不需要计算,'meta_key'
值将更改为现有 'total_sales'
产品 meta_key
,这样:
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => 4,
'meta_key' => 'total_sales', // <== <== the existing meta_key
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post__in' => $related
) );
$products = new WP_Query( $args );