Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Wordpress][WooCommerce] Zliczanie zakupionych produktów po id w pliku functions.php, Zawyżone wyniki w porównianiu do WC Reports
swiezak
post 23.03.2020, 18:32:16
Post #1





Grupa: Zarejestrowani
Postów: 159
Pomógł: 0
Dołączył: 21.08.2011

Ostrzeżenie: (0%)
-----


Witajcie.
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:
  1. /**
  2. * Informacja o wartości zamówień na dany produkt.
  3. */
  4. function get_sum_orders($product_id) {
  5. $args = array(
  6. 'product_id' => $product_id,
  7. );
  8. $orders = wc_get_orders($args);
  9.  
  10. if (empty($orders) || !is_array($orders)) {
  11. return false;
  12. }
  13.  
  14. $total = array_reduce($orders, function ($carry, $order) {
  15. $carry += (float)$order->get_total();
  16.  
  17. return $carry;
  18. }, 0.0);
  19.  
  20. return $total;
  21. }
  22.  
  23. /**
  24. * Informacja o liczbie sprzedanych sztuk na karcie produktu.
  25. */
  26. add_action( 'woocommerce_single_product_summary', 'wc_product_sold_count', 21 );
  27. function wc_product_sold_count() {
  28. global $product;
  29. $user = wp_get_current_user();
  30.  
  31. $total_sold = $product->get_total_sales();
  32. $sum_orders = get_sum_orders('24');
  33.  
  34. if (($product->id == '24' && $total_sold) && ($user->id == '22' || $user->id == '1')) {
  35. echo '<p>' . sprintf( __( 'Sprzedano: %s sztuk', 'woocommerce' ), $total_sold ) . '</p>';
  36. }
  37. if (($product->id == '24' && $sum_orders) && ($user->id == '22' || $user->id == '1')) {
  38. echo '<p>' . sprintf( __( 'Łączna wartość: %s zł', 'woocommerce' ), $sum_orders ) . '</p>';
  39. }
  40. }


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.

  1. add_action( 'woocommerce_single_product_summary', 'wc_product_sold_count', 21 );
  2. function wc_product_sold_count() {
  3. include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php');
  4. $wc_report = new WC_Admin_Report();
  5. global $product;
  6. $user = wp_get_current_user();
  7.  
  8. $total_items = absint(
  9. $wc_report->get_order_report_data(
  10. 'data' => array(
  11. '_qty' => array(
  12. 'type' => 'order_item_meta',
  13. 'order_item_type' => 'line_item',
  14. 'function' => 'SUM',
  15. 'name' => 'order_item_count',
  16. ),
  17. ),
  18. 'where_meta' => array(
  19. 'relation' => 'AND',
  20. 'type' => 'order_item_meta',
  21. 'meta_key' => array( '_product_id' ),
  22. 'meta_value' => '24',
  23. 'operator' => 'IN',
  24. ),
  25. ),
  26. 'query_type' => 'get_var',
  27. 'order_status' => array( 'completed' ),
  28. )
  29. )
  30. );
  31.  
  32. $sum_orders = $wc_report->get_order_report_data(
  33. 'data' => array(
  34. '_line_total' => array(
  35. 'type' => 'order_item_meta',
  36. 'order_item_type' => 'line_item',
  37. 'function' => 'SUM',
  38. 'name' => 'order_item_amount',
  39. ),
  40. ),
  41. 'where_meta' => array(
  42. 'relation' => 'AND',
  43. 'type' => 'order_item_meta',
  44. 'meta_key' => array( '_product_id' ),
  45. 'meta_value' => '24',
  46. 'operator' => 'IN',
  47. ),
  48. ),
  49. 'query_type' => 'get_results',
  50. 'order_status' => array( 'completed' ),
  51. )
  52. );
  53.  
  54. if (($product->id == '24' && $total_items) && ($user->id == '1' || $user->id == '22')) {
  55. echo '<p>' . sprintf( __( 'Sprzedano: %s sztuk', 'woocommerce' ), $total_items ) . '</p>';
  56. }
  57. if (($product->id == '24' && $sum_orders[0]->order_item_amount) && ($user->id == '1' || $user->id == '22')) {
  58. echo '<p>' . sprintf( __( 'Łączna wartość netto: %s', 'woocommerce' ), wc_price( $sum_orders[0]->order_item_amount ) ) . '</p>';
  59. }
  60. }


Ten post edytował swiezak 22.03.2020, 01:34:48
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 22:05