Skip to content

Instantly share code, notes, and snippets.

@kilbot
Created December 9, 2024 22:32
Show Gist options
  • Save kilbot/58c7c0dada338b9ba9e403e027da06ae to your computer and use it in GitHub Desktop.
Save kilbot/58c7c0dada338b9ba9e403e027da06ae to your computer and use it in GitHub Desktop.
Custom payment.php template with integration for PW Gift Cards
<?php
/**
* POS Order Pay template.
*
* This template can be overridden by copying it to yourtheme/woocommerce-pos/payment.php.
* HOWEVER, this is not recommended , don't be surprised if your POS breaks
*/
defined( 'ABSPATH' ) || exit;
// Handle gift card submission
if ( $_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['pwgc_code'] ) && isset( $_POST['pwgc_nonce'] ) ) {
if ( wp_verify_nonce( $_POST['pwgc_nonce'], 'apply_pwgc' ) ) {
$card_number = sanitize_text_field( $_POST['pwgc_code'] );
$order = $this->order;
if ( ! is_a( $order, 'WC_Order' ) ) {
wc_add_notice( 'Invalid order.', 'error' );
} else {
$gift_card = new PW_Gift_Card( $card_number );
if ( ! $gift_card->get_id() ) {
wc_add_notice( $gift_card->get_error_message(), 'error' );
} elseif ( ! $gift_card->get_active() ) {
wc_add_notice( 'Card is inactive.', 'error' );
} else {
$balance = $gift_card->get_balance();
if ( $balance <= 0 ) {
wc_add_notice( 'This gift card has a zero balance.', 'notice' );
} else {
// Add gift card as an order item
$item = new WC_Order_Item_PW_Gift_Card();
$item->set_props(
array(
'card_number' => $gift_card->get_number(),
'amount' => $balance,
)
);
$order->add_item( $item );
// Calculate original order total
$cart_total = 0;
$fees_total = 0;
foreach ( $order->get_items() as $o_item ) {
$cart_total += $o_item->get_total();
}
foreach ( $order->get_fees() as $fee_item ) {
$fees_total += $fee_item->get_total();
}
$original_total = round( $cart_total + $fees_total + $order->get_shipping_total() + $order->get_cart_tax() + $order->get_shipping_tax(), wc_get_price_decimals() );
// Apply gift card and update totals
$new_total = max( 0, $original_total - $balance );
$order->set_total( $new_total );
$order->save();
wc_add_notice( 'Gift card applied successfully.', 'success' );
}
}
}
} else {
wc_add_notice( 'Invalid form submission.', 'error' );
}
}
?>
<!doctype html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<?php
if ( ! $this->disable_wp_head ) {
wp_head();
} else {
// Manually call necessary functions when wp_head is disabled.
if ( function_exists( 'wp_enqueue_block_template_skip_link' ) ) {
wp_enqueue_block_template_skip_link();
}
}
?>
<style>
html, body, ul, li, fieldset, address {
font-family: sans-serif;
font-size: 14px;
margin: 0 !important;
padding: 0 !important;
color: #000000 !important;
background-color: #ffffff !important;
}
h1, h2, h3, h4, h5, h6 {
margin: 0;
padding: 0;
font-weight: 600;
line-height: 1.3;
}
h1 {
font-size: 18px;
margin-bottom: 15px;
}
h2 {
font-size: 16px;
margin-bottom: 12px;
}
h3 {
font-size: 14px;
margin-bottom: 10px;
}
h4 {
font-size: 14px;
margin-bottom: 8px;
}
h5 {
font-size: 14px;
margin-bottom: 6px;
}
h6 {
font-size: 12px;
margin-bottom: 4px;
}
.woocommerce {
color: #000000 !important;
background-color: #ffffff !important;
}
.woocommerce-pos-troubleshooting h3 {
margin: 0.5em 0;
padding: 0;
font-size: 1em;
font-weight: 600;
}
.woocommerce-pos-troubleshooting input[type="checkbox"] {
margin-right: 5px;
}
.woocommerce-pos-troubleshooting button {
background: #007cba;
border: none;
color: #fff;
padding: 10px 15px;
border-radius: 3px;
cursor: pointer;
}
.woocommerce-pos-troubleshooting button:hover {
background: #005a87;
}
.troubleshooting-modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1000; /* Sit on top */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
.troubleshooting-modal-content {
background-color: #fefefe;
margin: 2% auto; /* Slight margin from the top */
padding: 20px;
border: 1px solid #888;
width: 90%; /* Full width with slight margin */
max-height: 90%; /* Full height with slight margin */
overflow: auto; /* Enable scroll for content */
position: relative;
}
.close-troubleshooting-modal {
position: absolute;
top: -5px;
right: 10px;
color: #aaa;
font-size: 28px;
font-weight: bold;
}
.close-troubleshooting-modal:hover,
.close-troubleshooting-modal:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
.woocommerce-error {
background-color: #f8d7da;
color: #721c24;
border-color: #f5c6cb;
padding: 5px;
margin-bottom: 20px;
}
.woocommerce-info {
background-color: #d1ecf1;
color: #0c5460;
border-color: #bee5eb;
padding: 5px;
margin-bottom: 20px;
}
.cashier {
padding: 5px;
}
.cashier .cashier-name {
font-size: 14px;
font-weight: bold;
}
.current-user {
padding: 5px;
margin-bottom: 20px;
}
.current-user .user-name {
font-size: 14px;
font-weight: bold;
cursor: pointer;
color: #007acc; /* Change this color to your preferred link color */
text-decoration: underline;
}
.coupons {
text-align: right;
}
.coupons h3 {
margin: 10px 0;
}
/* Style for Customer Details */
.woocommerce-customer-details {
margin-bottom: 20px;
}
.woocommerce-columns {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
}
.woocommerce-column {
flex: 0 0 calc(50% - 10px);
padding: 5px;
}
.woocommerce-column__title {
font-size: 14px;
font-weight: bold;
margin-bottom: 5px;
}
address {
font-size: 12px;
line-height: 1.4;
}
address p {
margin-bottom: 3px;
}
.woocommerce-customer-details--phone,
.woocommerce-customer-details--email {
font-size: 12px;
margin-bottom: 3px;
}
/* Style for Order Details Table */
table.shop_table {
border-collapse: collapse !important;
width: 100% !important;
margin-bottom: 20px;
}
table.shop_table thead tr th,
table.shop_table tbody tr td,
table.shop_table tfoot tr td {
border: none;
padding: 5px;
text-align: left;
}
table.shop_table thead tr th {
font-weight: bold;
border-bottom: 2px solid #ddd;
}
table.shop_table tbody tr td {
border-bottom: 1px solid #ddd;
}
table.shop_table tfoot tr th {
padding: 5px;
text-align: right;
}
table.shop_table ul.wc-item-meta {
padding: 0;
list-style: none;
margin-top: 5px !important;
}
table.shop_table ul.wc-item-meta li {
font-size: 12px;
}
table.shop_table ul.wc-item-meta p {
display: inline-block;
margin: 0;
}
/* Style for Payment List Accordion */
.wc_payment_methods ul {
padding: 0;
margin: 0;
}
.wc_payment_methods li {
border-top: 1px solid #ddd;
margin-bottom: 10px;
list-style: none;
}
.wc_payment_methods li label {
display: inline-block;
padding: 10px;
cursor: pointer;
margin-bottom: 0;
}
.wc_payment_methods li div.payment_box {
display: none;
padding: 10px;
}
.wc_payment_methods li input[type="radio"] {
display: inline-block;
margin-right: 10px;
vertical-align: middle;
}
.wc_payment_methods li input[type="radio"] + label {
display: inline-block;
vertical-align: middle;
}
.wc_payment_methods li input[type="radio"]:checked + label {
font-weight: bold;
}
.wc_payment_methods li input[type="radio"]:checked + label + div.payment_box {
display: block;
padding-left: 42px;
background-color: #f7f7f7;
}
.wc_payment_methods li fieldset {
border: none;
}
.woocommerce-page #payment button#place_order {
display: none;
}
</style>
<script>
window.addEventListener("message", ({data}) => {
if (data.action && data.action === "wcpos-process-payment") {
// submit checkout form
const button = document.getElementById('place_order');
if (button) {
const event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
button.dispatchEvent(event);
}
}
}, false);
</script>
</head>
<body <?php body_class(); ?>>
<div class="woocommerce">
<?php
// Troubleshooting form section
echo $this->get_troubleshooting_form_html();
?>
<?php woocommerce_output_all_notices(); ?>
<?php
// Cashier details section
echo $this->get_cashier_details_html();
// Paying customer details section
echo $this->get_paying_customer_details_html();
// Coupon form section
echo $this->get_coupon_form_html();
?>
<!-- Gift Card Form -->
<div style="text-align:right">
<form method="post" style="margin-bottom:20px;">
<label for="pwgc_code">Gift Card Code:</label>
<input type="text" name="pwgc_code" id="pwgc_code" />
<?php wp_nonce_field( 'apply_pwgc', 'pwgc_nonce' ); ?>
<button type="submit">Apply Gift Card</button>
</form>
</div>
<?php
// WooCommerce payment form
wc_get_template(
'checkout/form-pay.php',
array(
'order' => $this->order,
'available_gateways' => $available_gateways,
'order_button_text' => $order_button_text,
)
);
?>
</div>
<?php
if ( ! $this->disable_wp_footer ) {
wp_footer();
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment