added MPC VPC switch

This commit is contained in:
2025-04-24 19:16:45 +03:00
parent f17d62c9e5
commit 38f5152b16
4 changed files with 183 additions and 2 deletions

View File

@@ -45,8 +45,6 @@ class B2BPayments extends PaymentModule implements PrestaShop\PrestaShop\Core\Mo
if ( if (
!parent::install() || !parent::install() ||
!$this->registerHook('paymentOptions') !$this->registerHook('paymentOptions')
// || !$this->registerHook('displayCustomerAccount')
// || !$this->registerHook('displayBeforeShoppingCartBlock')
|| !$this->registerHook('DisplayOrderConfirmation') || !$this->registerHook('DisplayOrderConfirmation')
) { ) {
return false; return false;
@@ -228,7 +226,18 @@ class B2BPayments extends PaymentModule implements PrestaShop\PrestaShop\Core\Mo
return $payment_options; return $payment_options;
} }
/**
* Check if a customer is in a B2B group.
*/
public function isCustomerB2B(?int $customer_id): bool
{
if (!$customer_id && !($customer_id = $this->context->customer->id)) {
return false;
}
return (bool) count(array_intersect([Configuration::get('B2BPAYMENTS_POSTPAID_GROUP'), Configuration::get('B2BPAYMENTS_PREPAID_GROUP')], Customer::getGroupsStatic($customer_id)));
}
/** /**
* Check if a customer is in a specific group. * Check if a customer is in a specific group.
@@ -279,10 +288,27 @@ class B2BPayments extends PaymentModule implements PrestaShop\PrestaShop\Core\Mo
*/ */
public function renderWidget($hookName, array $configuration) public function renderWidget($hookName, array $configuration)
{ {
/** call it in .tpl like {widget name='b2bpayments' hook='breadcrumb_price_visibility_switch'} */
if ($hookName == 'breadcrumb_price_visibility_switch') {
if ($this->isCustomerB2B(null)) {
return $this->fetch('module:' . $this->name . '/views/templates/hook/breadcrumb_price_visibility_switch.tpl');
}
return '';
}
/** call it in .tpl like {widget name='b2bpayments' hook='get_mpc' product=$product} */
if ($hookName == 'get_mpc') {
if ($this->isCustomerB2B(null) && isset($configuration['product']->embedded_attributes)) {
return $this->renderMPC($configuration['product']->embedded_attributes);
}
return '';
}
$this->smarty->assign($this->getWidgetVariables($hookName, $configuration)); $this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
if ($hookName == 'breadcrumb') { if ($hookName == 'breadcrumb') {
return $this->fetch('module:' . $this->name . '/views/templates/hook/breadcrumb.tpl'); return $this->fetch('module:' . $this->name . '/views/templates/hook/breadcrumb.tpl');
} }
return $this->fetch('module:' . $this->name . '/views/templates/hook/b2b_switch.tpl'); return $this->fetch('module:' . $this->name . '/views/templates/hook/b2b_switch.tpl');
} }
@@ -357,4 +383,29 @@ class B2BPayments extends PaymentModule implements PrestaShop\PrestaShop\Core\Mo
return $this->fetch('module:b2bpayments/views/templates/hook/displayOrderConfirmation.tpl'); return $this->fetch('module:b2bpayments/views/templates/hook/displayOrderConfirmation.tpl');
} }
public function renderMPC($embeddedProductAttributes): string
{
$id_product_attribute = (int)$embeddedProductAttributes['id_product_attribute'] ?? null;
$id_product = (int)$embeddedProductAttributes['id_product'];
$this->smarty->assign(['mpc' => $this->context->getCurrentLocale()->formatPrice(
$this->calculateMPC($id_product, $id_product_attribute),
$this->context->currency->iso_code
)]);
return $this->fetch('module:b2bpayments/views/templates/hook/mpc.tpl');
}
public function calculateMPC(int $id_product, ?int $id_product_attribute): float
{
$originalCustomer = Context::getContext()->customer;
$guestContext = Context::getContext();
$guestContext->customer = new Customer();
$specific_price_output = null;
$regularPrice = Product::getPriceStatic($id_product, true, $id_product_attribute, 2, null, false, true, 1, false, null, null, null, $specific_price_output, true, true, $guestContext);
Context::getContext()->customer = $originalCustomer;
return $regularPrice;
}
} }

12
config_hr.xml Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>b2bpayments</name>
<displayName><![CDATA[B2B Payments]]></displayName>
<version><![CDATA[1.0.0]]></version>
<description><![CDATA[Provides B2B payment options based on customer group.]]></description>
<author><![CDATA[panariga]]></author>
<tab><![CDATA[payments_gateways]]></tab>
<confirmUninstall><![CDATA[Are you sure you want to uninstall?]]></confirmUninstall>
<is_configurable>1</is_configurable>
<need_instance>0</need_instance>
</module>

View File

@@ -0,0 +1,113 @@
{*
* Price Display Toggle Template
*
* Module: B2bpayments
* Author: Panariga
*
* Provides a select element to switch between showing VPC, MPC, or both prices.
* Saves the user's preference in localStorage.
*}
<div class="col-md-6 col-lg-3">
<div id="price-display-switcher" class="payments-selection">
<select id="priceDisplayToggle" name="price_display_preference" class="custom-select">
{* Default option, selected if nothing is stored or if 'both' is stored *}
<option value="both">{l s='Show VPC & MPC' d='Modules.B2bpayments.ShopBreadcrumb'}</option>
<option value="vpc">{l s='Show VPC only (B2B)' d='Modules.B2bpayments.ShopBreadcrumb'}</option>
<option value="mpc">{l s='Show MPC only (B2C)' d='Modules.B2bpayments.ShopBreadcrumb'}</option>
</select>
</div>
</div>
{literal}
<script>
document.addEventListener('DOMContentLoaded', function() {
const priceToggleSelect = document.getElementById('priceDisplayToggle');
const vpcElements = document.querySelectorAll('.vpcrender');
const mpcElements = document.querySelectorAll('.mpcrender');
const storageKey = 'priceDisplayPreference'; // Key for localStorage
// Function to apply visibility based on preference
function applyPriceVisibility(preference) {
console.log('Applying preference:', preference); // For debugging
console.log('Found VPC elements:', vpcElements.length); // For debugging
console.log('Found MPC elements:', mpcElements.length); // For debugging
switch (preference) {
case 'vpc':
// Show VPC, Hide MPC
vpcElements.forEach(el => el.style.display = ''); // Reset to default display (usually block or inline)
mpcElements.forEach(el => el.style.display = 'none');
break;
case 'mpc':
// Hide VPC, Show MPC
vpcElements.forEach(el => el.style.display = 'none');
mpcElements.forEach(el => el.style.display = ''); // Reset to default display
break;
case 'both':
default:
// Show Both
vpcElements.forEach(el => el.style.display = ''); // Reset to default display
mpcElements.forEach(el => el.style.display = ''); // Reset to default display
break;
}
}
// Function to save preference to localStorage
function savePreference(preference) {
try {
localStorage.setItem(storageKey, preference);
console.log('Saved preference:', preference); // For debugging
} catch (e) {
console.error('Failed to save preference to localStorage:', e);
// LocalStorage might be disabled or full
}
}
// Function to load preference from localStorage
function loadPreference() {
try {
return localStorage.getItem(storageKey);
} catch (e) {
console.error('Failed to load preference from localStorage:', e);
return null; // Return null if localStorage is inaccessible
}
}
// --- Initialization ---
if (priceToggleSelect) {
// 1. Load saved preference
const savedPreference = loadPreference();
// 2. Set the select element's value and apply visibility
if (savedPreference && ['both', 'vpc', 'mpc'].includes(savedPreference)) {
priceToggleSelect.value = savedPreference;
applyPriceVisibility(savedPreference);
} else {
// No valid saved preference, use the default value from the select element
const initialPreference = priceToggleSelect.value || 'both'; // Fallback to 'both'
applyPriceVisibility(initialPreference);
// Optionally save the default if nothing was stored
// savePreference(initialPreference);
}
// 3. Add event listener for changes
priceToggleSelect.addEventListener('change', function(event) {
const selectedPreference = event.target.value;
applyPriceVisibility(selectedPreference);
savePreference(selectedPreference);
});
} else {
console.warn('Price display toggle select element (#priceDisplayToggle) not found.');
}
// Initial check in case elements are added dynamically later (less common for prices)
// If you expect prices to load via AJAX *after* DOMContentLoaded,
// you might need a MutationObserver or trigger applyPriceVisibility again.
// For standard page loads, the above should be sufficient.
});
</script>
{/literal}

View File

@@ -0,0 +1,5 @@
{if $mpc}
<div class="mpcrender" style="display: none;">
<b> {$mpc}</b>
</div>
{/if}