get_meta('_usedkit_sold_at')) { $product->update_meta_data('_usedkit_sold_at', time()); $product->save_meta_data(); } elseif ($status === 'instock' && $product->get_meta('_usedkit_sold_at')) { $product->delete_meta_data('_usedkit_sold_at'); // back on sale $product->delete_meta_data('_usedkit_sold_price'); $product->save_meta_data(); } }, 10, 3); /* What it actually sold for, from the order: the asking price is often not it (a * discount code, an accepted offer). Including tax, as the customer paid it. */ add_action('woocommerce_order_status_changed', function ($order_id, $from, $to, $order) { if (!in_array($to, array('processing', 'completed'), true) || !$order) { return; } foreach ($order->get_items() as $item) { $product = $item->get_product(); $qty = max(1, (int) $item->get_quantity()); if ($product) { $product->update_meta_data('_usedkit_sold_price', round(((float) $item->get_total() + (float) $item->get_total_tax()) / $qty, 2)); $product->save_meta_data(); } } do_action('usedkit_sold_price_recorded'); }, 10, 4); add_action('admin_menu', function () { add_submenu_page('woocommerce', __('Sold items', 'usedkit'), __('Sold items', 'usedkit'), 'manage_woocommerce', 'usedkit-sold', 'usedkit_sold_page'); }, 20); function usedkit_sold_page() { if (!current_user_can('manage_woocommerce')) { return; } $days = absint(usedkit_view_arg('sold', 'days')) ?: 90; $since = time() - $days * DAY_IN_SECONDS; $products = wc_get_products(array('limit' => 500, 'stock_status' => 'outofstock', 'meta_key' => '_usedkit_sold_at', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_compare' => '>=', 'meta_value' => $since)); $rows = array(); $spans = array(); $margin_total = 0.0; $priced = 0; foreach ($products as $p) { $sold = (int) $p->get_meta('_usedkit_sold_at'); $listed = $p->get_date_created() ? $p->get_date_created()->getTimestamp() : null; $span = $listed ? max(0, round(($sold - $listed) / DAY_IN_SECONDS)) : null; if ($span !== null) { $spans[] = $span; } $price = function_exists('usedkit_sold_price') ? usedkit_sold_price($p) : (float) $p->get_price(); $cost = function_exists('usedkit_cost') ? usedkit_cost($p) : null; if ($cost !== null) { $margin_total += $price - $cost; $priced++; } $rows[] = array($p, $sold, $span, $price, $cost); } sort($spans); $median = $spans ? $spans[(int) floor((count($spans) - 1) / 2)] : null; ?>

get_name()); ?>