Skip to content

Instantly share code, notes, and snippets.

@ara303
Last active September 6, 2024 01:26
Show Gist options
  • Save ara303/0d0924e67ae47ee06b104ccdbf31ad67 to your computer and use it in GitHub Desktop.
Save ara303/0d0924e67ae47ee06b104ccdbf31ad67 to your computer and use it in GitHub Desktop.
Super simple WooCommerce ticket system (may or may not work)
<?php
/**
* this is wrong. needs to be custom. isn't. idk. will fix later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
do_action( 'woocommerce_email_header', $email_heading, $email );
echo '<p>Thank you for your purchase! Here are your ticket details:</p>';
$additional_info = get_post_meta( $order->get_id(), '_additional_info', true );
if ( $additional_info ) {
foreach ( $additional_info as $index => $info ) {
echo '<p>Ticket ' . ( $index + 1 ) . ': ' . esc_html( $info ) . '</p>';
}
}
do_action( 'woocommerce_email_footer', $email );
?>
<?php
/**
* Code does in functions.php or a functionality plugin.
*/
add_action('template_redirect', 'custom_add_to_cart_redirect_with_quantity');
function custom_add_to_cart_redirect_with_quantity() {
if (is_cart() && !empty($_POST)) {
// Assuming the ticket product ID is 456 (replace with your actual ID)
$ticket_product_id = 456;
foreach (WC()->cart->get_cart() as $cart_item) {
if ($cart_item['product_id'] == $ticket_product_id) {
$_SESSION['ticket_quantity'] = $cart_item['quantity'];
break;
}
}
wp_redirect(get_permalink(123)); // Replace '123' with your custom page ID
exit;
}
}
// Handle form submission on custom page and redirect to checkout
add_action('template_redirect', 'custom_form_submission_handler');
function custom_form_submission_handler() {
if (is_page(123) && !empty($_POST['custom_form_fields'])) {
// Store data for each ticket
$_SESSION['additional_info'] = array();
foreach ($_POST['custom_form_fields'] as $info) {
$_SESSION['additional_info'][] = sanitize_text_field($info);
}
wp_redirect(wc_get_checkout_url());
exit;
}
}
// Custom form for additional information page
function display_custom_form($content) {
if (is_page(123) && !is_admin() && !defined('DOING_AJAX')) {
$quantity = isset($_SESSION['ticket_quantity']) ? $_SESSION['ticket_quantity'] : 1;
ob_start();
?>
<form method="POST" action="">
<?php for ($i = 0; $i < $quantity; $i++): ?>
<label for="additional_info_<?php echo $i; ?>">Enter Additional Information for Ticket <?php echo $i + 1; ?>:</label>
<textarea id="additional_info_<?php echo $i; ?>" name="custom_form_fields[]" required></textarea>
<?php endfor; ?>
<input type="submit" value="Continue to Checkout">
</form>
<?php
return ob_get_clean();
}
return $content;
}
add_filter('the_content', 'display_custom_form');
// Add hidden fields to checkout to include stored data
add_action('woocommerce_before_checkout_form', 'add_custom_info_to_checkout');
function add_custom_info_to_checkout() {
if (isset($_SESSION['additional_info'])) {
foreach ($_SESSION['additional_info'] as $index => $info) {
woocommerce_form_field('additional_info_' . $index, array(
'type' => 'hidden',
'value' => $info,
));
}
}
}
// Save custom fields to order meta
add_action('woocommerce_checkout_update_order_meta', 'save_custom_info_to_order_meta');
function save_custom_info_to_order_meta($order_id) {
$additional_info = array();
foreach ($_SESSION['additional_info'] as $index => $info) {
if (!empty($_POST['additional_info_' . $index])) {
$additional_info[] = sanitize_text_field($_POST['additional_info_' . $index]);
}
}
if (!empty($additional_info)) {
update_post_meta($order_id, '_additional_info', $additional_info);
unset($_SESSION['additional_info']); // Clear session data
}
}
// Display custom field in order admin panel
add_action('woocommerce_admin_order_data_after_billing_address', 'display_custom_info_in_order_admin', 10, 1);
function display_custom_info_in_order_admin($order){
$additional_info = get_post_meta($order->get_id(), '_additional_info', true);
if (!empty($additional_info)) {
echo '<p><strong>' . __('Additional Information') . ':</strong></p>';
foreach ($additional_info as $index => $info) {
echo '<p>Ticket ' . ($index + 1) . ': ' . esc_html($info) . '</p>';
}
}
}
// Hook into WooCommerce order status 'completed'
add_action('woocommerce_order_status_completed', 'send_custom_order_emails', 10, 1);
function send_custom_order_emails($order_id) {
if (!$order_id) {
return;
}
$order = wc_get_order($order_id);
$additional_info = get_post_meta($order_id, '_additional_info', true);
// Send email to customer
send_custom_email_to_customer($order, $additional_info);
// Send email to admin
send_custom_email_to_admin($order, $additional_info);
}
// Send custom email to customer
function send_custom_email_to_customer($order, $additional_info) {
$customer_email = $order->get_billing_email();
$subject = 'Your Ticket Purchase Receipt';
$headers = array('Content-Type: text/html; charset=UTF-8');
$body = '<p>Thank you for your purchase! Here are your ticket details:</p>';
foreach ($additional_info as $index => $info) {
$body .= '<p>Ticket ' . ($index + 1) . ': ' . esc_html($info) . '</p>';
}
wp_mail($customer_email, $subject, $body, $headers);
}
// Send custom email to admin
function send_custom_email_to_admin($order, $additional_info) {
$admin_email = get_option('admin_email');
$subject = 'New Ticket Sale';
$headers = array('Content-Type: text/html; charset=UTF-8');
$body = '<p>A new ticket sale has been completed. Here are the details:</p>';
$body .= '<p>Order ID: ' . $order->get_order_number() . '</p>';
$body .= '<p>Customer: ' . $order->get_formatted_billing_full_name() . '</p>';
$body .= '<p>Email: ' . $order->get_billing_email() . '</p>';
$body .= '<p>Tickets purchased: ' . count($additional_info) . '</p>';
foreach ($additional_info as $index => $info) {
$body .= '<p>Ticket ' . ($index + 1) . ': ' . esc_html($info) . '</p>';
}
wp_mail($admin_email, $subject, $body, $headers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment