Files
b2bpayments/views/templates/hook/breadcrumb_payment_switcher.tpl

69 lines
2.2 KiB
Smarty

{if $show_switch}
<div class="payment-switcher">
<div class="payments-selection">
{* The select dropdown *}
<select id="b2b-group-select" name="b2b_group_type" class="custom-select" onchange="switchB2Bpayment();">
{* Option for Prepaid *}
<option value="prepaid" {if !$current_group_is_postpaid}selected{/if}>
{l s='Prepaid' d='Modules.B2bpayments.ShopBreadcrumb'}
</option>
{* Option for Postpaid
* show only if customer is in group "B2B Odgoda placanja" *}
{if in_array(5, $customer_groups)}
<option value="postpaid" {if $current_group_is_postpaid}selected{/if}>
{l s='Postpaid' d='Modules.B2bpayments.ShopBreadcrumb'}
</option>
{/if}
</select>
</div>
</div>
<script type="text/javascript">
function switchB2Bpayment() {
const xhr = new XMLHttpRequest();
xhr.open('POST', '{$switch_url|escape:'javascript'}');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); // Required for POST requests
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300) {
try {
const response = JSON.parse(xhr.responseText);
if (response.success) {
// Success: Reload the page to reflect the change
location.reload();
} else {
alert('{$error_message|escape:'javascript'}');
}
} catch (e) {
console.error('JSON Parsing Error:', e, xhr.responseText);
alert('An error occurred while processing the response. Please try again.');
}
} else {
// HTTP error: Show generic error
console.error('HTTP Error:', xhr.status, xhr.statusText);
alert('An error occurred while switching groups (HTTP ' + xhr.status + '). Please try again.');
}
};
xhr.onerror = function() {
// Network or other request error
console.error('Request failed');
alert(
'An error occurred while switching groups (Network Error). Please check your connection and try again.'
);
};
const params = 'ajax=true&action=switchGroup'; // Build the query string
xhr.send(params);
};
</script>
{/if}