Skip to content

Commit

Permalink
Complete Reservation Section Tamplate, WooCommerce and Transient
Browse files Browse the repository at this point in the history
  • Loading branch information
ringkurahman committed Aug 10, 2021
1 parent a79a621 commit d1a8242
Show file tree
Hide file tree
Showing 6 changed files with 817 additions and 660 deletions.
8 changes: 4 additions & 4 deletions assets/js/reservation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
$("#reservenow").on("click", function () {
$.post( mealurl.ajaxurl, {

action: "reservation",
action: "reservation", // Custom Post Id
name: $("#name").val(),
email: $("#email").val(),
phone: $("#phone").val(),
persons: $("#persons").val(),
date: $("#date").val(),
time: $("#time").val(),
rn: $("#rn").val(),
rn: $("#rn").val(), // Nonce
},
function (data) {
console.log(data)
Expand All @@ -19,8 +19,8 @@
"You have already placed a request for this reservation. No need to submit again"
)
} else {
$("#paynow").attr("href", data)
$("#reservenow").hide()
$("#paynow").attr("href", data) // Target Link ID
$("#reservenow").hide() // Input Button ID
$("#paynow").show()
}
}
Expand Down
142 changes: 142 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,67 @@ function meal_process_reservation() {
);
//print_r( $data );

$reservation_arguments = array(
'post_type' => 'reservation',
'post_author' => 1,
'post_date' => date( 'Y-m-d H:i:s' ),
'post_status' => 'publish',
'post_title' => sprintf( '%s - Reservation for %s persons on %s - %s', $name, $persons, $date . " : " . $time, $email ),
'meta_input' => $data
);

$reservations = new WP_Query( array(
'post_type' => 'reservation',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
'email_check' => array(
'key' => 'email',
'value' => $email
),
'date_check' => array(
'key' => 'date',
'value' => $date
),
'time_check' => array(
'key' => 'time',
'value' => $time
),
)
) );
if ( $reservations->found_posts > 0 ) {
echo 'Duplicate';
} else {
$wp_error = '';
$reservation_id = wp_insert_post( $reservation_arguments, $wp_error );

//transient check
$reservation_count = get_transient( 'res_count' ) ? get_transient( 'res_count' ) : 0;
//transient check end

if ( ! $wp_error ) {

$reservation_count ++;
set_transient( 'res_count', $reservation_count, 0 );

$_name = explode( " ", $name );
$order_data = array(
'first_name' => $_name[0],
'last_name' => isset( $_name[1] ) ? $_name[1] : '',
'email' => $email,
'phone' => $phone,
);
$order = wc_create_order();
$order->set_address( $order_data );
$order->add_product( wc_get_product( 42 ), 1 ); // Booking Product ID 42
$order->set_customer_note( $reservation_id );
$order->calculate_totals();

add_post_meta( $reservation_id, 'order_id', $order->get_id() );

echo $order->get_checkout_payment_url();
}
}

} else {
echo 'Not allowed';
Expand All @@ -200,3 +261,84 @@ function meal_process_reservation() {

add_action( 'wp_ajax_reservation', 'meal_process_reservation' );
add_action( 'wp_ajax_nopriv_reservation', 'meal_process_reservation' );




// Remove WooCommerce Plugin Fields
function meal_checkout_fields( $fields ) {

// remove billing fields
unset( $fields['billing']['billing_company'] );
unset( $fields['billing']['billing_address_1'] );
unset( $fields['billing']['billing_address_2'] );
unset( $fields['billing']['billing_city'] );
unset( $fields['billing']['billing_postcode'] );
unset( $fields['billing']['billing_country'] );
unset( $fields['billing']['billing_state'] );

// remove shipping fields
unset( $fields['shipping']['shipping_first_name'] );
unset( $fields['shipping']['shipping_last_name'] );
unset( $fields['shipping']['shipping_company'] );
unset( $fields['shipping']['shipping_address_1'] );
unset( $fields['shipping']['shipping_address_2'] );
unset( $fields['shipping']['shipping_city'] );
unset( $fields['shipping']['shipping_postcode'] );
unset( $fields['shipping']['shipping_country'] );
unset( $fields['shipping']['shipping_state'] );

// remove order comment fields
unset( $fields['order']['order_comments'] );

return $fields;
}

add_filter( 'woocommerce_checkout_fields', 'meal_checkout_fields' );




// Get Order status
function meal_order_status_processing( $order_id ) {
$order = wc_get_order( $order_id );
$reservation_id = $order->get_customer_note();
if ( $reservation_id ) {
$reservation = get_post( $reservation_id );
wp_update_post( array(
'ID' => $reservation_id,
'post_title' => "[Paid] - " . $reservation->post_title
) );

add_post_meta( $reservation_id, 'paid', 1 );
}
}

add_filter( 'woocommerce_order_status_processing', 'meal_order_status_processing' );




// Show Transient Count in Admin Reservation Menu
function meal_change_menu( $menu ) {
$reservation_count = get_transient( 'res_count' ) ? get_transient( 'res_count' ) : 0;
if ( $reservation_count > 0 ) {
$menu[4][0] = "Reservation <span class='awaiting-mod'>{$reservation_count}</span> "; // Menu Position 5[0]
}

return $menu;
}

add_filter( 'add_menu_classes', 'meal_change_menu' );



// Delete Transient After Visit
function meal_admin_scripts( $screen ) {
$_screen = get_current_screen();
if ( 'edit.php' == $screen && 'reservation' == $_screen->post_type ) { // Custom Post ID = reservation
delete_transient( 'res_count' );
}
}

add_action( 'admin_enqueue_scripts', 'meal_admin_scripts' );
Loading

0 comments on commit d1a8242

Please sign in to comment.