I figured it out, sort of, realised i didnt have stripe installed on my testing sandbox so its got a TODO comment and hardcoded publishable key
the code is dynamically pulled from stripe and injected into the following div...
Code:
<div id="payment-method-messaging-element"></div>
it loads and does the calculations as expected, the TODO is self explanatory. This particular edit covers the "tpl_product_info.php" in includes/templates/your_template_folder/templates/ but should also be presented in the basket with the price set as the total basket price and i think i have covered the issue then?
i'm not happy with the location of the div, the page is a bit funky from playing around with it yesterday trying to improve its layout... BUT, it works :)
Code:
* @version $Id: Steve 2021 Jun 14 Modified in v1.5.8-alpha $
*/
//require(DIR_WS_MODULES . '/debug_blocks/product_info_prices.php');
//require_once(DIR_WS_LANGUAGES . $_SESSION['language'] . '/modules/payment/stripe.php');
// TODO test without hardcoding credentials double check language definition url and uncomment above require statement
define('MODULE_PAYMENT_STRIPE_PUBLISHABLE_KEY', 'pk_live_add_your_pk_live_here');
?>
<script src="https://js.stripe.com/v3/"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
// Set your publishable key. Replace with your live key in production.
const stripe = Stripe('<?php echo MODULE_PAYMENT_STRIPE_PUBLISHABLE_KEY; ?>', {
locale: 'en-GB' // Sets the locale to British English
});
// Get the product price dynamically from the span with id "productPrices"
const priceElement = document.querySelector('#productPrices');
if (!priceElement) {
console.error('Price element not found. Ensure the id "productPrices" exists.');
return;
}
// Extract and convert the price to pence
const productPrice = parseFloat(priceElement.innerText.replace(/[^\d.]/g, '')) * 100;
// Check if price was extracted correctly
if (isNaN(productPrice)) {
console.error('Failed to extract the product price. Check the price format.');
return;
}
// Create an instance of Stripe Elements
const elements = stripe.elements();
// Payment Method Messaging Element options
const options = {
amount: productPrice, // Use dynamically fetched product price
currency: 'GBP', // Set to British Pounds
countryCode: 'GB', // Country code for the UK
};
// Create the Payment Method Messaging Element
const paymentMessageElement = elements.create('paymentMethodMessaging', options);
// Mount the element to a container with the specified ID
paymentMessageElement.mount('#payment-method-messaging-element');
});
</script>
<div class="centerColumn product-info" id="productGeneral">
<!--bof Form start-->
<?php echo zen_draw_form('cart_quantity', zen_href_link(zen_get_info_page($_GET['products_id']), zen_get_all_get_params(array('action')) . 'action=add_product', $request_type), 'post', 'enctype="multipart/form-data" id="addToCartForm"') . "\n"; ?>
<!--eof Form start-->
<?php if ($messageStack->size('product_info') > 0) echo $messageStack->output('product_info'); ?>
<!--bof Category Icon -->
<?php if ($module_show_categories != 0) { ?>
<?php
/**
* display the category icons
*/
//require($template->get_template_dir('/tpl_modules_category_icon_display.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_category_icon_display.php'); ?>
<?php } ?>
<!--eof Category Icon -->
<!--bof Prev/Next top position -->
<?php if (PRODUCT_INFO_PREVIOUS_NEXT == 1 or PRODUCT_INFO_PREVIOUS_NEXT == 3) { ?>
<?php
/**
* display the product previous/next helper
*/
require($template->get_template_dir('/tpl_products_next_previous.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_products_next_previous.php'); ?>
<?php } ?>
<!--eof Prev/Next top position-->
<div id="prod-info-top" class="container-fluid-mobile <?php echo ( $elevatezoom_style == 'pro' ) ? 'container-mobile-airSticky' : ''; ?>">
<div class="row <?php echo ( $elevatezoom_style == 'pro' ) ? 'airSticky_stop-block' : ''; ?>">
<div id="pinfo-left" class="<?php echo $prod_info_img_class; ?> hidden-xs group">
<!--bof Main Product Image -->
<?php if (!empty($products_image)) { ?>
<?php require($template->get_template_dir('/tpl_modules_additional_images.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_additional_images.php'); ?>
<?php } ?>
</div>
<div class="tt-mobile-product-layout col-12 visible-xs">
<div class="tt-mobile-product-slider arrow-location-center slick-animated-show-js">
<div><?php echo wt_image(addslashes($products_image_large), addslashes($products_name), MEDIUM_IMAGE_WIDTH, MEDIUM_IMAGE_HEIGHT, 'data-zoom-image="' . addslashes($products_image_large) . '"'); ?></div>
<?php
if ( is_array( $list_box_contents ) > 0 ) {
for ( $row = 0; $row < sizeof( $list_box_contents ); $row++ ) {
for ( $col = 0; $col < sizeof( $list_box_contents[$row] ); $col++ ) {
if ( isset( $list_box_contents[$row][$col]['text']['large'] ) ) {
echo '<div ' . wt_stringify_atts( $list_box_contents[$row][$col]['params'] ) . '>' . $list_box_contents[$row][$col]['text']['large'] . '</div>';
}
}
}
}
?>
</div>
</div>
<h1 id="productName" class="tt-title productGeneral"><?php echo $products_name; ?></h1>
<div class="tt-price">
<span id="productPrices" class="productGeneral new-price">
<?php
// base price
if ($show_onetime_charges_description == 'true') {
$one_time = '<span >' . TEXT_ONETIME_CHARGE_SYMBOL . TEXT_ONETIME_CHARGE_DESCRIPTION . '</span><br>';
} else {
$one_time = '';
}
echo $one_time . ((zen_has_product_attributes_values((int)$_GET['products_id']) and $flag_show_product_info_starting_at == 1) ? TEXT_BASE_PRICE : '') . zen_get_products_display_price((int)$_GET['products_id']);
?>
</span>
</div>
<div id="payment-method-messaging-element"></div>
if anyone decides to go ahead and implement this you need the div id and to locate it where you please. alot of this code wont match up to what you may have, the important pieces are....
Code:
//require_once(DIR_WS_LANGUAGES . $_SESSION['language'] . '/modules/payment/stripe.php');
// TODO test without hardcoding credentials double check language definition url and uncomment above require statement
define('MODULE_PAYMENT_STRIPE_PUBLISHABLE_KEY', 'pk_live_add_your_pk_live_here');
?>
<script src="https://js.stripe.com/v3/"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
// Set your publishable key. Replace with your live key in production.
const stripe = Stripe('<?php echo MODULE_PAYMENT_STRIPE_PUBLISHABLE_KEY; ?>', {
locale: 'en-GB' // Sets the locale to British English
});
// Get the product price dynamically from the span with id "productPrices"
const priceElement = document.querySelector('#productPrices');
if (!priceElement) {
console.error('Price element not found. Ensure the id "productPrices" exists.');
return;
}
// Extract and convert the price to pence
const productPrice = parseFloat(priceElement.innerText.replace(/[^\d.]/g, '')) * 100;
// Check if price was extracted correctly
if (isNaN(productPrice)) {
console.error('Failed to extract the product price. Check the price format.');
return;
}
// Create an instance of Stripe Elements
const elements = stripe.elements();
// Payment Method Messaging Element options
const options = {
amount: productPrice, // Use dynamically fetched product price
currency: 'GBP', // Set to British Pounds
countryCode: 'GB', // Country code for the UK
};
// Create the Payment Method Messaging Element
const paymentMessageElement = elements.create('paymentMethodMessaging', options);
// Mount the element to a container with the specified ID
paymentMessageElement.mount('#payment-method-messaging-element');
});
</script>
and your div....
<div id="payment-method-messaging-element"></div>
which might take some experimenting with until you locate it where you like... do be careful and backup your tpl_display_product_info.php before making any changes
SIDENOTE: if you are not in the uk you need to edit the currency and country code :)
Bookmarks