Alexey Khodakov Posted December 24, 2023 Share Posted December 24, 2023 Hello. I'm trying to develop a custom module, that helps restrict some payment method if some promocode is applied to cart. Module is registered by two hooks: displayPaymentTop and paymentOptions and also have two public functions hookPaymentOptions and hookDisplayPaymentTop. Function hookDisplayPaymentTop is triggered and I can see the return of it, but hookPaymentOptions isn't triggered and I can't change payment options list. What I do wrong? There is code: <?php if (!defined('_PS_VERSION_')) { exit; } class PaymentPromo extends Module { public function __construct() { $this->name = 'paymentpromo'; $this->tab = 'payments_gateways'; $this->version = '1.0.0'; $this->author = 'Aleksey Khodakov'; $this->need_instance = 0; $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_); $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('Payment Promo'); $this->description = $this->l('Restriction of payment options by promocode'); $this->confirmUninstall = $this->l('Do you really want to delete module?'); } public function install() { if (Shop::isFeatureActive()) { Shop::setContext(Shop::CONTEXT_ALL); } PrestaShopLogger::addLog('Payment promo installation process...', 1); return parent::install() && $this->registerHook('displayPaymentTop') && $this->registerHook('paymentOptions') ; } public function uninstall() { return parent::uninstall(); } public function hookPaymentOptions($params) { PrestaShopLogger::addLog('hookPaymentOptions is truggered', 0); if (!$this->active) { return; } // Getting Cart $cart = $params['cart']; // Check if there is any promocode if ($cart->id) { $cartRules = $cart->getCartRules(); foreach ($cartRules as $cartRule) { if ($this->isPaymentRestrictionNeeded($cartRule['code'])) { // Ограничиваем способы оплаты PrestaShopLogger::addLog('Payment restriction applied for promo code: ' . $cartRule['code'], 1, null, 'Cart', $cart->id, true); return $this->getLimitedPaymentOptions(); } } } // Return All payment opetions if there is no specifiq promocode return $this->getStandardPaymentOptions(); } public function hookDisplayPaymentTop($params) { PrestaShopLogger::addLog('hookDisplayPaymentTop is triggered', 0); if (!$this->active) { return; } //ECHO "EXT1" ; // Получаем корзину $cart = $params['cart']; // Проверяем, есть ли промокоды if ($cart->id) { $cartRules = $cart->getCartRules(); foreach ($cartRules as $cartRule) { if ($this->isPaymentRestrictionNeeded($cartRule['code'])) { // Ограничиваем способы оплаты PrestaShopLogger::addLog('Payment text applied for promo code: ' . $cartRule['code'], 1, null, 'Cart', $cart->id, true); //$this->getLimitedPaymentOptions(); return '<p>There is a limitation to payment</p>'; } } } } private function isPaymentRestrictionNeeded($promoCode) { // There is a logic to check promocode return $promoCode === 'ONLINE'; } private function getLimitedPaymentOptions() { $paymentOptions = []; // Getting all installed payment Modules $modules = PaymentModule::getInstalledPaymentModules(); foreach ($modules as $module) { if ($module['name'] == 'codfee') { $moduleInstance = Module::getInstanceById($module['id_module']); if ($moduleInstance && $moduleInstance->active) { $paymentOptions[] = $this->createPaymentOption($moduleInstance); } } } return $paymentOptions; } private function getStandardPaymentOptions() { // Return all payment options $paymentOptions = []; // Getting all available payment modules $modules = PaymentModule::getInstalledPaymentModules(); foreach ($modules as $module) { $moduleInstance = Module::getInstanceById($module['id_module']); if ($moduleInstance && $moduleInstance->active) { $paymentOptions[] = $this->createPaymentOption($moduleInstance); } } return $paymentOptions; } private function createPaymentOption($moduleInstance) { $paymentOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption(); $paymentOption->setModuleName($moduleInstance->name) ->setCallToActionText($moduleInstance->displayName) ->setAction($moduleInstance->getFormLink()) ->setAdditionalInformation($moduleInstance->getDescription()); return $paymentOption; } } Link to comment Share on other sites More sharing options...
AddWeb Solution Posted February 2 Share Posted February 2 Hi, Clear the cache and check once. Also Make sure that your module is properly installed and activated in the Prestashop back office. Verify that the module is enabled in the Payment section. Double-check the hook registration in the install method.check if both displayPaymentTop and paymentOptions are registered correctly. Thanks! Link to comment Share on other sites More sharing options...
Alexey Khodakov Posted February 6 Author Share Posted February 6 On 2/2/2024 at 3:50 PM, AddWeb Solution said: Verify that the module is enabled in the Payment section. Could you write how to do it? I mean how to register module in Payment section? Link to comment Share on other sites More sharing options...
AddWeb Solution Posted February 6 Share Posted February 6 3 minutes ago, Alexey Khodakov said: Could you write how to do it? I mean how to register module in Payment section? In Modules section, Click on your module to access its configuration page. Verify that your module is configured to appear in the Payment section. Make sure that your module is associated with the necessary hooks related to payments. In your case, it is to associated with the paymentOptions hook. After making any adjustments, save the changes and clear the cache. Then Navigate to the "Payment" section in the back office to verify that your module appears among the available payment methods. If your module is not yet activated, activate it. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now