Last active
March 30, 2025 06:06
-
-
Save goranefbl/a3fec140894d638fe0cefc852278ae2a to your computer and use it in GitHub Desktop.
WPGens Points and Rewards - manual refund points per order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add meta box to order page | |
add_action('add_meta_boxes', 'add_points_refund_box'); | |
function add_points_refund_box() { | |
if (class_exists('Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController')) { | |
$controller = wc_get_container()->get('Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController'); | |
$screen = $controller->custom_orders_table_usage_is_enabled() | |
? wc_get_page_screen_id('shop-order') | |
: 'shop_order'; | |
} else { | |
$screen = 'shop_order'; | |
} | |
add_meta_box( | |
'points_refund_box', | |
'Refund Points', | |
'render_points_refund_box', | |
$screen, | |
'side', | |
'default' | |
); | |
} | |
function render_points_refund_box($post) { | |
$order = wc_get_order($post->ID); | |
?> | |
<div class="points-refund-box"> | |
<p> | |
<label>Points to Refund:</label> | |
<input type="number" id="points_refund_amount" step="1" min="1" /> | |
</p> | |
<p> | |
<button type="button" class="button button-primary" id="refund_points_button"> | |
Refund Points | |
</button> | |
</p> | |
</div> | |
<script> | |
jQuery(document).ready(function($) { | |
$('#refund_points_button').on('click', function() { | |
if (!confirm('Are you sure you want to refund these points?')) { | |
return; | |
} | |
$.ajax({ | |
url: ajaxurl, | |
type: 'POST', | |
data: { | |
action: 'manual_points_refund', | |
order_id: '<?php echo esc_js($post->ID); ?>', | |
points: $('#points_refund_amount').val(), | |
nonce: '<?php echo wp_create_nonce('wc-admin-nonce'); ?>' | |
}, | |
success: function(response) { | |
if (response.success) { | |
alert('Points refunded successfully'); | |
window.location.reload(); | |
} else { | |
alert(response.data.message); | |
} | |
} | |
}); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
// AJAX handler | |
add_action('wp_ajax_manual_points_refund', 'handle_manual_points_refund'); | |
function handle_manual_points_refund() { | |
check_ajax_referer('wc-admin-nonce', 'nonce'); | |
if (!current_user_can('edit_shop_orders')) { | |
wp_send_json_error(['message' => 'Insufficient permissions']); | |
} | |
$order_id = isset($_POST['order_id']) ? absint($_POST['order_id']) : 0; | |
$points = isset($_POST['points']) ? absint($_POST['points']) : 0; | |
$order = wc_get_order($order_id); | |
if (!$order || !$points) { | |
wp_send_json_error(['message' => 'Invalid order or points amount']); | |
} | |
$user_id = $order->get_user_id(); | |
if (!$user_id) { | |
wp_send_json_error(['message' => 'Order has no associated user']); | |
} | |
do_action( | |
'wpgens_loyalty_update_points', | |
$user_id, | |
$points, | |
WPGL_Points_Activity_Type::EARN, | |
WPGL_Points_Source_Type::ORDER_REFUNDED, | |
$order_id, | |
'Manual points refund' | |
); | |
wp_send_json_success(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment