הוספת עמודה למספר מוצרים שנמכרו בדשבורד של וורפדרס תחת עמודת מוצרים
PHP
// Add Sold column to Products table
add_filter('manage_edit-product_columns', 'add_sold_column_to_product_table');
function add_sold_column_to_product_table($columns) {
$columns['product_sold'] = __('מוצרים שנמכרו', 'text-domain');
return $columns;
}
// Make Sold column sortable
add_filter('manage_edit-product_sortable_columns', 'make_sold_column_sortable_in_product_table');
function make_sold_column_sortable_in_product_table($columns) {
$columns['product_sold'] = 'product_sold';
return $columns;
}
// Populate Sold column with number of units sold
add_action('manage_product_posts_custom_column', 'populate_sold_column_in_product_table', 10, 2);
function populate_sold_column_in_product_table($column, $post_id) {
if ($column === 'product_sold') {
$product = wc_get_product($post_id);
if ($product && $product->is_type('simple')) { // Check if product exists and is a simple product
$units_sold = get_post_meta($post_id, 'total_sales', true);
echo $units_sold;
} else {
echo '-';
}
}
}
// Define custom sorting for Sold column
add_action('pre_get_posts', 'custom_sorting_for_sold_column');
function custom_sorting_for_sold_column($query) {
if (!is_admin()) { // Check if not in admin area
return;
}
$screen = get_current_screen();
if ($screen->id !== 'edit-product' || $query->get('orderby') !== 'product_sold') { // Check if on Products screen and sorting by Sold column
return;
}
$query->set('meta_key', 'total_sales');
$query->set('orderby', 'meta_value_num');
}