Mam pewien problem, z którym nie wiem za bardzo jak sobie poradzić.
Chcę w widoku wybranego produktu wtyczki WooCommerce wyświetlić informację nt. ile sztuk się sprzedało i jaka jest ich łączna wartość.
W pliku functions.php mam taki oto fragment:
/** * Informacja o wartości zamówień na dany produkt. */ function get_sum_orders($product_id) { 'product_id' => $product_id, ); $orders = wc_get_orders($args); return false; } $carry += (float)$order->get_total(); return $carry; }, 0.0); return $total; } /** * Informacja o liczbie sprzedanych sztuk na karcie produktu. */ add_action( 'woocommerce_single_product_summary', 'wc_product_sold_count', 21 ); function wc_product_sold_count() { $user = wp_get_current_user(); $total_sold = $product->get_total_sales(); $sum_orders = get_sum_orders('24'); if (($product->id == '24' && $total_sold) && ($user->id == '22' || $user->id == '1')) { } if (($product->id == '24' && $sum_orders) && ($user->id == '22' || $user->id == '1')) { } }
Funkcje zliczają, ale... Nie ma to potwierdzenia w WC Reports.
Wyniki się nie pokrywają - w widoku pojedynczego produktu są one zawyżone.
Czy ktoś jest w stanie mi podpowiedzieć, gdzie popełniłem błąd? Będę wdzięczny za pomoc.
Dla potomnych.
Poradziłem sobie, wykorzystując klasę WC Reports w pliku functions.php motywu. Temat do zamknięcia.
add_action( 'woocommerce_single_product_summary', 'wc_product_sold_count', 21 ); function wc_product_sold_count() { include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php'); $wc_report = new WC_Admin_Report(); $user = wp_get_current_user(); $total_items = absint( $wc_report->get_order_report_data( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'order_item_count', ), ), 'relation' => 'AND', 'type' => 'order_item_meta', 'meta_value' => '24', 'operator' => 'IN', ), ), 'query_type' => 'get_var', ) ) ); $sum_orders = $wc_report->get_order_report_data( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'order_item_amount', ), ), 'relation' => 'AND', 'type' => 'order_item_meta', 'meta_value' => '24', 'operator' => 'IN', ), ), 'query_type' => 'get_results', ) ); if (($product->id == '24' && $total_items) && ($user->id == '1' || $user->id == '22')) { } if (($product->id == '24' && $sum_orders[0]->order_item_amount) && ($user->id == '1' || $user->id == '22')) { } }