Untitled
webwizards
plain_text
a year ago
2.9 kB
20
Indexable
add_filter('manage_edit-shop_order_columns', 'add_custom_order_columns', 10, 1);
add_action('manage_shop_order_posts_custom_column', 'add_custom_order_columns_content', 10, 2);
add_filter('manage_woocommerce_page_wc-orders_columns', 'add_custom_order_columns', 10, 1);
add_action('manage_woocommerce_page_wc-orders_custom_column', 'add_custom_order_columns_content', 10, 2);
// Add custom columns to the orders list
function add_custom_order_columns($columns) {
$columns['nearest_branch'] = __('Nearest Branch', 'woocommerce');
$columns['sales_rep'] = __('Sales Rep', 'woocommerce');
return $columns;
}
// Populate the custom columns with data
function add_custom_order_columns_content($column, $order_id = 0) {
global $woocommerce, $post;
// Handle order ID for both legacy and HPOS
if ($order_id === 0) {
if (isset($post)) {
if (isset($post->ID)) {
$order_id = $post->ID;
}
}
}
if ($order_id !== 0) {
$order = wc_get_order($order_id);
$customer_id = $order->get_customer_id();
if ('nearest_branch' === $column) {
if ($customer_id) {
$nearest_branch = get_user_meta($customer_id, 'b2bking_custom_field_7736', true);
echo !empty($nearest_branch) ? esc_html($nearest_branch) : '—';
} else {
echo '—';
}
}
if ('sales_rep' === $column) {
if ($customer_id) {
$sales_rep = get_user_meta($customer_id, 'b2bking_custom_field_7735', true);
echo !empty($sales_rep) ? esc_html($sales_rep) : '—';
} else {
echo '—';
}
}
}
}
// Add customer info to Order Details page
add_action('woocommerce_admin_order_data_after_billing_address', 'add_custom_order_details_info');
function add_custom_order_details_info($order) {
$customer_id = $order->get_customer_id();
if ($customer_id) {
$nearest_branch = get_user_meta($customer_id, 'b2bking_custom_field_7736', true);
$sales_rep = get_user_meta($customer_id, 'b2bking_custom_field_7735', true);
if (!empty($nearest_branch) || !empty($sales_rep)) {
echo '<div class="order_data_column" style="width: 100%; clear: both; margin-top: 15px;">';
echo '<h3>' . __('Customer Information', 'woocommerce') . '</h3>';
if (!empty($nearest_branch)) {
echo '<p><strong>' . __('Nearest Branch:', 'woocommerce') . '</strong> ' . esc_html($nearest_branch) . '</p>';
}
if (!empty($sales_rep)) {
echo '<p><strong>' . __('Sales Rep:', 'woocommerce') . '</strong> ' . esc_html($sales_rep) . '</p>';
}
echo '</div>';
}
}
}Editor is loading...
Leave a Comment