Untitled
unknown
plain_text
a year ago
3.3 kB
7
Indexable
// Add column headers
add_action('salesking_customers_custom_columns_header', function(){
?>
<th class="nk-tb-col tb-col-md"><span class="sub-text"><?php esc_html_e('Last Order Date', 'salesking'); ?></span></th>
<th class="nk-tb-col tb-col-md"><span class="sub-text"><?php esc_html_e('Customer Note', 'salesking'); ?></span></th>
<?php
});
// Add column content
add_action('salesking_customers_custom_columns_content', function($customer){
// Last Order Date
$last_order = wc_get_customer_last_order($customer->get_id());
$last_order_date = $last_order ? $last_order->get_date_created()->date('Y-m-d') : '-';
// Customer Note
$customer_note = get_user_meta(get_current_user_id(), 'sales_rep_customer_note_'.$customer->get_id(), true);
?>
<td class="nk-tb-col tb-col-md" data-order="<?php echo esc_attr($last_order_date); ?>">
<div>
<span class="tb-amount"><?php echo esc_html($last_order_date); ?></span>
</div>
</td>
<td class="nk-tb-col tb-col-md">
<div>
<textarea class="customer-note" data-customer-id="<?php echo esc_attr($customer->get_id()); ?>"><?php echo esc_textarea($customer_note); ?></textarea>
</div>
</td>
<?php
}, 10, 1);
// Add scripts to the dashboard head
add_action('salesking_dashboard_head', function() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.customer-note').on('change', function() {
var customerId = $(this).data('customer-id');
var note = $(this).val();
var $textarea = $(this);
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: {
action: 'save_customer_note',
nonce: '<?php echo wp_create_nonce('customer-note-nonce'); ?>',
customer_id: customerId,
note: note
},
success: function(response) {
if (response.success) {
$textarea.css('border-color', 'green');
setTimeout(function() {
$textarea.css('border-color', '');
}, 2000);
} else {
$textarea.css('border-color', 'red');
setTimeout(function() {
$textarea.css('border-color', '');
}, 2000);
}
}
});
});
});
</script>
<?php
});
// AJAX handler to save customer note
add_action('wp_ajax_save_customer_note', function() {
check_ajax_referer('customer-note-nonce', 'nonce');
$customer_id = isset($_POST['customer_id']) ? intval($_POST['customer_id']) : 0;
$note = isset($_POST['note']) ? sanitize_textarea_field($_POST['note']) : '';
$current_user_id = get_current_user_id();
if ($customer_id) {
update_user_meta($current_user_id, 'sales_rep_customer_note_'.$customer_id, $note);
wp_send_json_success('Note saved successfully');
} else {
wp_send_json_error('Error saving note');
}
});
Editor is loading...
Leave a Comment