2016-09-11 4 views
1

Woocommerce 관련 제품 템플릿에 의해 관련 제품을 주문은에 있습니다 :WooCommerce 판매 사용자 정의 계산

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

그러나 임의하여 현재 순서.

나는 'total_sales' + 'sales_count'으로 (사람들이 2 meta_keys 정수 값을 보유하고 'sales_count'하는 추가 사용자 정의 필드) 것을 주문하고 싶습니다. 여기

쿼리입니다 : 위의 쿼리 정렬 제품 TOTAL_SALES을 구입

$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_SALESsales_count의 합계를 사용하여 해당 정렬 할 필요가?

이 방법이 있습니까? 이미 판매 된 모든 항목의 계산을 만드는 제품에 대한 기본 'total_sales'으로 내가 $soldty = get_post_meta($product->id, 'sales_count', true);의 잘 목적을 이해하지 못하는

감사

+0

죄송합니다. 두 개의 사용자 정의 필드의 합계로 주문해야하는 정보가 더 필요합니다. 다른 매개 변수는 정상입니다. –

+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); 'Sales :'. ($ units_sold + $ soldty); –

답변

1

는 (판매 품목 수량이 각각 추가된다 카운트 시간). 당신이 정말로이 계산을해야하는 경우

어쨌든, 최선의 선택은 계산으로, 각 제품에 대한 새로운 사용자 정의 필드를 생성/업데이트하는 기능을하는 것입니다 :

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' 계산 값이 포함되어 있습니다.


당신이 당신의 계산이 필요하지 않은 경우

$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' 기존에 변경됩니다 :

이제이 새로운 사용자 정의 필드를 기반으로 쿼리 'orderby' 인수를 사용자 정의 할 수 있습니다 제품 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); 
관련 문제