Code:
<?php
/**
* Soundbyte Social Club Membership Module
* This module integrates membership signup, management, and QR code generation into a Zen Cart store.
*/
// Ensure secure execution
define('IS_ADMIN_FLAG', true);
// Include necessary libraries
require_once(DIR_FS_CATALOG . '/vendor/phpqrcode/qrlib.php');
class MembershipModule {
/**
* Create membership as a product
*/
public static function addMembershipProduct() {
global $db;
$membershipProduct = [
'products_name' => 'Soundbyte Social Club Membership',
'products_description' => 'Become a member of the Soundbyte Social Club and enjoy exclusive benefits, including music events and trading card tournaments!',
'products_price' => 20.00,
'products_tax_class_id' => 1,
'products_status' => 1,
'products_quantity' => 9999,
'products_date_added' => 'NOW()',
];
// Insert product into database
zen_db_perform(TABLE_PRODUCTS, $membershipProduct);
// Assign product to a specific category (optional)
$productId = $db->Insert_ID();
zen_db_perform(TABLE_PRODUCTS_TO_CATEGORIES, [
'products_id' => $productId,
'categories_id' => 1 // Replace with actual category ID for memberships
]);
}
/**
* Create a new database table to store membership details
*/
public static function createMembershipTable() {
global $db;
$sql = "CREATE TABLE IF NOT EXISTS club_members (
member_id INT AUTO_INCREMENT PRIMARY KEY,
customer_id INT NOT NULL,
membership_start_date DATETIME NOT NULL,
membership_status ENUM('Pending', 'Active', 'Expired') DEFAULT 'Pending',
waiting_period_end_date DATETIME NOT NULL,
qr_code_path VARCHAR(255),
FOREIGN KEY (customer_id) REFERENCES customers(customers_id)
) ENGINE=InnoDB;";
$db->Execute($sql);
}
/**
* Handle membership purchase
*/
public static function handleMembershipPurchase($customerId) {
global $db;
$startDate = date('Y-m-d H:i:s');
$waitingPeriodEndDate = date('Y-m-d H:i:s', strtotime('+48 hours'));
// Generate QR Code
$qrData = "MemberID: $customerId | StartDate: $startDate";
$qrFileName = DIR_FS_CATALOG . "images/membership_qr_$customerId.png";
QRcode::png($qrData, $qrFileName, QR_ECLEVEL_H, 10);
// Insert membership details into the database
$db->Execute("INSERT INTO club_members (customer_id, membership_start_date, membership_status, waiting_period_end_date, qr_code_path)
VALUES ('$customerId', '$startDate', 'Pending', '$waitingPeriodEndDate', '$qrFileName')");
// Notify customer
self::sendMembershipEmail($customerId, $qrFileName, $waitingPeriodEndDate);
}
/**
* Send membership confirmation email
*/
private static function sendMembershipEmail($customerId, $qrFileName, $waitingPeriodEndDate) {
global $db;
// Fetch customer email
$customerQuery = $db->Execute("SELECT customers_email_address, customers_firstname FROM customers WHERE customers_id = '$customerId'");
$email = $customerQuery->fields['customers_email_address'];
$firstName = $customerQuery->fields['customers_firstname'];
// Email content
$subject = "Welcome to Soundbyte Social Club";
$message = "Dear $firstName,\n\nThank you for joining the Soundbyte Social Club!\n\n" .
"Your membership is now active. Please note that alcohol privileges will be available after $waitingPeriodEndDate.\n\n" .
"Attached is your digital membership card with a QR code for quick verification.\n\nEnjoy exclusive events, music, and more!\n\nBest regards,\nSoundbyte Social Club";
// Attach QR Code
$attachment = chunk_split(base64_encode(file_get_contents($qrFileName)));
$boundary = "==Boundary" . md5(time());
$headers = "From: [email protected]\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n\r\n" .
"--$boundary\r\n" .
"Content-Type: text/plain; charset=UTF-8\r\n\r\n" .
"$message\r\n\r\n" .
"--$boundary\r\n" .
"Content-Type: image/png; name=\"membership_qr_$customerId.png\"\r\n" .
"Content-Disposition: attachment; filename=\"membership_qr_$customerId.png\"\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n" .
"$attachment\r\n--$boundary--";
// Send email
mail($email, $subject, '', $headers);
}
/**
* Display membership QR code in customer account page
*/
public static function displayMembershipQRCode($customerId) {
global $db;
$query = $db->Execute("SELECT qr_code_path FROM club_members WHERE customer_id = '$customerId'");
if ($query->RecordCount() > 0) {
echo '<div><h3>Your Membership QR Code</h3>';
echo '<img src="' . DIR_WS_CATALOG . $query->fields['qr_code_path'] . '" alt="Membership QR Code">';
echo '</div>';
} else {
echo '<p>No active membership found. Join the Soundbyte Social Club today!</p>';
}
}
}
// Initialization
MembershipModule::createMembershipTable();
MembershipModule::addMembershipProduct();
I spotted the db query looks wrong and that is probably the least of the issues, usually takes a while to edit these into something that works