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:
/** * Informacja o wartości zamówień na dany produkt. */ function get_sum_orders($product_id) { $args = http://www.php.net/array( 'product_id' => $product_id, ); $orders = wc_get_orders($args); if (http://www.php.net/empty($orders) || !http://www.php.net/is_array($orders)) { return false; } $total = http://www.php.net/array_reduce($orders, function ($carry, $order) { $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() { http://www.php.net/global $product; $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')) { http://www.php.net/echo '<p>' . http://www.php.net/sprintf( __( 'Sprzedano: %s sztuk', 'woocommerce' ), $total_sold ) . '</p>'; } if (($product->id == '24' && $sum_orders) && ($user->id == '22' || $user->id == '1')) { http://www.php.net/echo '<p>' . http://www.php.net/sprintf( __( 'Łączna wartość: %s zł', 'woocommerce' ), $sum_orders ) . '</p>'; } }
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(); http://www.php.net/global $product; $user = wp_get_current_user(); $total_items = absint( $wc_report->get_order_report_data( http://www.php.net/array( 'data' => http://www.php.net/array( '_qty' => http://www.php.net/array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'order_item_count', ), ), 'where_meta' => http://www.php.net/array( 'relation' => 'AND', http://www.php.net/array( 'type' => 'order_item_meta', 'meta_key' => http://www.php.net/array( '_product_id' ), 'meta_value' => '24', 'operator' => 'IN', ), ), 'query_type' => 'get_var', 'order_status' => http://www.php.net/array( 'completed' ), ) ) ); $sum_orders = $wc_report->get_order_report_data( http://www.php.net/array( 'data' => http://www.php.net/array( '_line_total' => http://www.php.net/array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'order_item_amount', ), ), 'where_meta' => http://www.php.net/array( 'relation' => 'AND', http://www.php.net/array( 'type' => 'order_item_meta', 'meta_key' => http://www.php.net/array( '_product_id' ), 'meta_value' => '24', 'operator' => 'IN', ), ), 'query_type' => 'get_results', 'order_status' => http://www.php.net/array( 'completed' ), ) ); if (($product->id == '24' && $total_items) && ($user->id == '1' || $user->id == '22')) { http://www.php.net/echo '<p>' . http://www.php.net/sprintf( __( 'Sprzedano: %s sztuk', 'woocommerce' ), $total_items ) . '</p>'; } if (($product->id == '24' && $sum_orders[0]->order_item_amount) && ($user->id == '1' || $user->id == '22')) { 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>'; } }
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)