私は非常によく$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);
私は2つのカスタムフィールドの合計で注文する必要がある情報を追加しました。その他のパラメータは通常のものです –
カスタムフィールドとして注文した場合、wocommerceによるtotal_sales値の増分 sales_countカスタムフィールドいくつかの整数を保持していますが、sales_countの使用法は、sales_count + total_salesの商品ページに表示するために使用しています。製品に実際に売り上げがないので、sales_countフィールドを使っていくつかの売り上げが表示されます –
$ units_sold = get_post_meta($ product-> id、 'total_sales'、true); $ soldty = get_post_meta($ product-> id、 'sales_count'、true); echo 'Sales:'。($ units_sold + $ soldty); –