Suneth Kalhara
Suneth Kalhara

Reputation: 1208

WooCommerce - Ordering related products by sales custom calculation

Woocommerce related products template is located on:

/wp-content/plugins/woocommerce/templates/single-product/related.php

But it currently order by random.

I would like to order that by 'total_sales' + 'sales_count' (those are 2 meta_keys hold integer values and 'sales_count' is an additional custom field).

Here is the query:

 $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 );

The above query sort products buy total_sales but I need to sort that using sum of total_sales and sales_count?

Is there a way to do this?.

Thanks

Upvotes: 3

Views: 1459

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253804

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).

Anyway, If you really need this calculation, the best option will be to make a function that will create/update a new custom field, for each product, with that calculation:

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 );
    }
}

This function make the calculation for product sales and create/update a custom field 'global_sales_count' with the calculation value in it.

Now you can customized your query 'orderby' argument based on this new custom field:

$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 );

If you don't need your calculation, the 'meta_key' value will be changed to existing 'total_sales' product meta_key, this way:

$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 );

Upvotes: 3

Related Questions