Reading list Switch to dark mode

    Remove payment method based on product into the cart

    Updated 27 March 2023

    Remove  payment method based on specific product if exist into the cart, To give appropriate payment methods to the customer by which they can proceed their cart.

    In this post, i will tell you how you can stop the payment methods not be available when customer comes to proceed their cart by choosing the appropriate payment methods.

    Many times admin want to remove payment methods like bankwire/cheque or paypal payment even they want these method will only be visible for a specific product not for all products, so we can do this task by writing few line of code.

    Hook.php – Be careful when writing anything in this file, this is the file where all your prestashop modules pass. So we will override this class file to remove payment methods.

    Path for this class – classes/Hook.php

    Start your headless eCommerce
    now.
    Find out More

    Now jump to function  getHookModuleExecList() in Hook.php, we have to override this function to perform the restriction on payment methods based on products.

    Sample code – How to write code to remove payment module not to be available when customer comes to pay.

    class Hook extends HookCore
    {
    	/*
        * module: preorder
        * date: 2016-11-02 19:17:19
        * version: 2.0.0
        */
        public static function getHookModuleExecList($hook_name = null)
    	{
    		$list = parent::getHookModuleExecList($hook_name);
    		if ($hook_name == 'displayPayment') {
    			foreach ($list as $key => $payment) {
    				if ($payment['id_module'] == 3) {
    					unset($list[$key]);
    				}
    			}
    		}
    		return $list;
    	}
    }

    See the code, we have called parent of getHookModyleExecList() function and store of the modules array which are going to list on prestashop.
    Now we have hook display name, module ID, hook Id that will not be changed, so we can check using  displayPayment (Hook that display payment modules) string as well. If ($hook_name == ‘displayName’) {} In this condition only payment modules list will be available so, you can unset any payment module which you want to remove. As i have removed bankwire payment module which has ID 3,

    So you can see in the below when customer comes to pay, they won’t be able to see bankwire payment module.

    Remove Payment

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home