diff --git a/b2bpayments.php b/b2bpayments.php index 9bde0f8..fcd26d4 100644 --- a/b2bpayments.php +++ b/b2bpayments.php @@ -45,8 +45,6 @@ class B2BPayments extends PaymentModule implements PrestaShop\PrestaShop\Core\Mo if ( !parent::install() || !$this->registerHook('paymentOptions') - // || !$this->registerHook('displayCustomerAccount') - // || !$this->registerHook('displayBeforeShoppingCartBlock') || !$this->registerHook('DisplayOrderConfirmation') ) { return false; @@ -228,7 +226,18 @@ class B2BPayments extends PaymentModule implements PrestaShop\PrestaShop\Core\Mo 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. @@ -279,10 +288,27 @@ class B2BPayments extends PaymentModule implements PrestaShop\PrestaShop\Core\Mo */ 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)); if ($hookName == 'breadcrumb') { return $this->fetch('module:' . $this->name . '/views/templates/hook/breadcrumb.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'); } + + + 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; + } } diff --git a/config_hr.xml b/config_hr.xml new file mode 100644 index 0000000..48623dd --- /dev/null +++ b/config_hr.xml @@ -0,0 +1,12 @@ + + + b2bpayments + + + + + + + 1 + 0 + \ No newline at end of file diff --git a/views/templates/hook/breadcrumb_price_visibility_switch.tpl b/views/templates/hook/breadcrumb_price_visibility_switch.tpl new file mode 100644 index 0000000..a503d8d --- /dev/null +++ b/views/templates/hook/breadcrumb_price_visibility_switch.tpl @@ -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. +*} +
+
+ +
+
+ +{literal} + +{/literal} \ No newline at end of file diff --git a/views/templates/hook/mpc.tpl b/views/templates/hook/mpc.tpl new file mode 100644 index 0000000..a27916f --- /dev/null +++ b/views/templates/hook/mpc.tpl @@ -0,0 +1,5 @@ +{if $mpc} + +{/if} \ No newline at end of file