2016-09-11 9 views
1

Woocommerce関連製品テンプレートが上に配置されている:WooCommerce販売カスタム計算によって関連製品を発注

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

しかし、それランダムによって現在オーダー。

Iは、(それらが2 meta_keysが整数値を保持していると'sales_count'追加カスタムフィールドである)'total_sales' + '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_SALESsales_countの合計を使用していることをソートする必要がありますか?

これを行う方法はありますか。

おかげ

+0

私は2つのカスタムフィールドの合計で注文する必要がある情報を追加しました。その他のパラメータは通常のものです –

+0

カスタムフィールドとして注文した場合、wocommerceによるtotal_sales値の増分 sales_countカスタムフィールドいくつかの整数を保持していますが、sales_countの使用法は、sales_count + total_salesの商品ページに表示するために使用しています。製品に実際に売り上げがないので、sales_countフィールドを使っていくつかの売り上げが表示されます –

+0

$ units_sold = get_post_meta($ product-> id、 'total_sales'、true); $ soldty = get_post_meta($ product-> id、 'sales_count'、true); echo 'Sales:'。($ units_sold + $ soldty); –

答えて

1

私は非常によく$soldty = get_post_meta($product->id, 'sales_count', true);の目的を理解していない、製品のデフォルト'total_sales'が既に販売されたすべての項目の計算を行いとして(販売数量は、それぞれ追加されますカウントまでの時間)。あなたが本当にこの計算を必要とする場合

とにかく、最良のオプションは、その計算で、各製品のために、新しいカスタムフィールドを作成/更新する機能を作ることになります。

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

この関数は、製品販売の計算を行い、カスタムフィールドを登録/更新します。'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); 
関連する問題