Drukowana wersja tematu

Kliknij tu, aby zobaczyć temat w orginalnym formacie

Forum PHP.pl _ Systemy portalowe i CMS'y _ [Wordpress][WooCommerce] Zliczanie zakupionych produktów po id w pliku functions.php

Napisany przez: swiezak 23.03.2020, 18:32:16

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 = http://www.php.net/array(
  6. 'product_id' => $product_id,
  7. );
  8. $orders = wc_get_orders($args);
  9.  
  10. if (http://www.php.net/empty($orders) || !http://www.php.net/is_array($orders)) {
  11. return false;
  12. }
  13.  
  14. $total = http://www.php.net/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. http://www.php.net/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. http://www.php.net/echo '<p>' . http://www.php.net/sprintf( __( 'Sprzedano: %s sztuk', 'woocommerce' ), $total_sold ) . '</p>';
  36. }
  37. if (($product->id == '24' && $sum_orders) && ($user->id == '22' || $user->id == '1')) {
  38. http://www.php.net/echo '<p>' . http://www.php.net/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. http://www.php.net/global $product;
  6. $user = wp_get_current_user();
  7.  
  8. $total_items = absint(
  9. $wc_report->get_order_report_data(
  10. http://www.php.net/array(
  11. 'data' => http://www.php.net/array(
  12. '_qty' => http://www.php.net/array(
  13. 'type' => 'order_item_meta',
  14. 'order_item_type' => 'line_item',
  15. 'function' => 'SUM',
  16. 'name' => 'order_item_count',
  17. ),
  18. ),
  19. 'where_meta' => http://www.php.net/array(
  20. 'relation' => 'AND',
  21. http://www.php.net/array(
  22. 'type' => 'order_item_meta',
  23. 'meta_key' => http://www.php.net/array( '_product_id' ),
  24. 'meta_value' => '24',
  25. 'operator' => 'IN',
  26. ),
  27. ),
  28. 'query_type' => 'get_var',
  29. 'order_status' => http://www.php.net/array( 'completed' ),
  30. )
  31. )
  32. );
  33.  
  34. $sum_orders = $wc_report->get_order_report_data(
  35. http://www.php.net/array(
  36. 'data' => http://www.php.net/array(
  37. '_line_total' => http://www.php.net/array(
  38. 'type' => 'order_item_meta',
  39. 'order_item_type' => 'line_item',
  40. 'function' => 'SUM',
  41. 'name' => 'order_item_amount',
  42. ),
  43. ),
  44. 'where_meta' => http://www.php.net/array(
  45. 'relation' => 'AND',
  46. http://www.php.net/array(
  47. 'type' => 'order_item_meta',
  48. 'meta_key' => http://www.php.net/array( '_product_id' ),
  49. 'meta_value' => '24',
  50. 'operator' => 'IN',
  51. ),
  52. ),
  53. 'query_type' => 'get_results',
  54. 'order_status' => http://www.php.net/array( 'completed' ),
  55. )
  56. );
  57.  
  58. if (($product->id == '24' && $total_items) && ($user->id == '1' || $user->id == '22')) {
  59. http://www.php.net/echo '<p>' . http://www.php.net/sprintf( __( 'Sprzedano: %s sztuk', 'woocommerce' ), $total_items ) . '</p>';
  60. }
  61. if (($product->id == '24' && $sum_orders[0]->order_item_amount) && ($user->id == '1' || $user->id == '22')) {
  62. http://www.php.net/echo '<p>' . http://www.php.net/sprintf( __( 'Łączna wartość netto: %s', 'woocommerce' ), wc_price( $sum_orders[0]->order_item_amount ) ) . '</p>';
  63. }
  64. }

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)