Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rafiahmedd/3fb5e76d11c4bebadd28e57d08ef8d33 to your computer and use it in GitHub Desktop.
Save rafiahmedd/3fb5e76d11c4bebadd28e57d08ef8d33 to your computer and use it in GitHub Desktop.
Block-WpPayForm-submission-if-maximum-quantity-limit-reached.php
<?php
add_filter('wppayform/validate_data_on_submission_item_quantity',function( $error, $elementId, $element, $form_data){
global $wpdb;
$formId = 46; //Please Add Your Form ID here
$transactions = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpf_order_transactions WHERE form_id = {$formId}" );
$orders = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wpf_order_items WHERE form_id = {$formId}" );
$orderQuantity = [];
$limit = 10; //Please Add Your Total Limit Here
for ($i = 0; $i < count($transactions); $i++) {
$transId = $transactions[$i]->submission_id;
$paymentStatus = $transactions[$i]->status;
for ($j = 0; $j < count($orders); $j++) {
if ($transId == $orders[$j]->submission_id && $paymentStatus == 'paid') {
$orderQuantity[] = $orders[$j]->quantity;
$remain = $limit - array_sum($orderQuantity);
$temp = $remain;
if ($temp < $form_data['item_quantity']) {
if($temp<=0){
return $error = "No tickets left.";
}else{
return $error = "A fewer tickets left.";
}
}
}
}
}
},10,4);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment