Loading
Cancel
Webmail Login
Control Panel Login
Prestashop : How to remove a payment module depending on the cart value.
.CO.UK ONLY £5.33
Find your perfect domain here

Prestashop : How to remove a payment module depending on the cart value.

The below blog is by Dip-Slides.com (providers of microbiological dipslides). It explains how to remove a payment module if the cart value is over a certain amount. In this case, the payment module in question is Nochex but the below should work for any payment module.

Dip-slides.com : Hiding a payment module.

There are various payment modules available for PrestaShop. After careful consideration, we recently signed up to Nochex (to allow payment via credit/debit card). As with many merchant accounts, we had a maximum transaction limit set (in this case - £600). This itself is not an issue as orders larger than this would likely be paid using a bank transfer.

However, one small problem that could occur is if someone tries to make a payment over £600 using the Nochex module. This would result in them being redirected to a page stating:

"The payment you are attempting to make exceeds the Sellers agreed receive limit. Please contact the Seller directly for further advice on how to process this payment."

Whilst many customers would contact us requesting advice, it would be nice to stop this from happening (and in turn remove the risk of losing a customer). In order to do this, we needed a way to disable the payment module if the cart value was over a certain amount.

The first step was to find out where the Nochex payment tpl file was located. This is the file that generates the payment link on the final stage of the checkout. With some payment modules, this would be located in modules/module-name/view/templates/front. However, a quick search showed in Nochex's case, it was located in modules/nochex/nochex.tpl.

Looking at this file, we can see the following:

 
<!-- The <p> tags below are details of the button which users select to go to Nochex page.-->
<style>
a.nochex:after {
display: block;
content: "\f054";
position: absolute;
right: 15px;
margin-top: -11px;
top: 50%;
font-family: "FontAwesome";
font-size: 25px;
height: 22px;
width: 14px;
color: #777777;
}
</style>

<p class="payment_module">
	<a href="javascript:$('#nochex_form').submit();" title="{l s='Pay with Nochex APC' mod='NoChex'}" style="padding: 0;width: 49%;" class="nochex">
		
		<img src="https://www.nochex.com/logobase-secure-images/logobase-banners/clear-amex-mp-vme.png" style="height:100px;">&nbsp;
	
	</a>
</p>

<!-- The form below is details from the nochex.php form which variables are attached to the relevant field. These will be sent to Nochex. -->
<form action="https://secure.nochex.com/" method="post" id="nochex_form" class="hidden">
	<input type='hidden' name='amount' value="{$amount}" />
	<input type='hidden' name='description' value="Order #{$order_id} details: {$description}" />
	<input type='hidden' name='xml_item_collection' value="{$xml_item_collection}" />
	<input type='hidden' name='postage' value="{$postage}" />
	<input type='hidden' name='billing_fullname' value="{$billing_fullname}" />
	<input type='hidden' name='billing_address' value="{$billing_address}" />
	<input type='hidden' name='billing_city' value="{$billing_city}" />
	<input type='hidden' name='billing_postcode' value="{$billing_postcode}" />
	<input type='hidden' name='delivery_fullname' value="{$delivery_fullname}" />
	<input type='hidden' name='delivery_address' value="{$delivery_address}" />
	<input type='hidden' name='delivery_city' value="{$delivery_city}" />
	<input type='hidden' name='delivery_postcode' value="{$delivery_postcode}" />
    <input type='hidden' name='customer_phone_number' value="{$customer_phone_number}" />
	<input type='hidden' name='email_address' value="{$email_address}" />
	<input type='hidden' name='order_id' value="{$order_id}" />
	<input type='hidden' name='optional_1' value="{$optional_1}" />
	<input type='hidden' name='merchant_id' value="{$merchant_id}" />
    <input type='hidden' name='success_url' value="{$successurl}"/>
	<input type='hidden' name='test_success_url' value="{$successurl}"/>
	<input type='hidden' name='cancel_url' value="{$cancelurl}"/>
	<input type='hidden' name='declined_url' value="{$cancelurl}"/>
	<input type='hidden' name='callback_url' value="{$responderurl}" />
	<input type='hidden' name='test_transaction' value="{$test_transaction}" />
</form>

 

We can see the whole template file contains the data to display the Nochex payment button and the values to pass onto Nochex for the payment. So, to stop the Nochex payment button from being displayed at the checkout, we would simply need to stop this code from being run.

How to hide the payment button

After locating where the payment button code was, we started work on removing it from the final stage of the PrestaShop checkout. To do this, you add the following code on the very first line:

{600 assign='nochex_max_order_value'}
{math equation='b-a' a=$total_price b={toolsConvertPrice price=$nochex_max_order_value} assign='max_order_val_minus_cart_value'}
{if $max_order_val_minus_cart_value > 0}

and the following code on the very last line

{/if}

So what does this code do? Let's take a closer look.

The very first line sets out what the maximum amount of funds we can accept via Nochex are. In this case, we wanted to set it to 600. It then assigns this value to the name "nochex_max_order_value". You can change the 600 to whatever your maximum transaction amount with your payment provider is.

{600 assign='nochex_max_order_value'}

The next line does a little mathematics. First it converts the £600 (since pounds is our default currency) to the currency used by the customer. It then assigns this to $nochex_max_order_value:

{toolsConvertPrice price=$nochex_max_order_value} 

Next it calculates $nochex_max_order_value minus the total price of the cart. It then assigns this value to "max_order_val_minus_cart_value".

{math equation='b-a' a=$total_price b={toolsConvertPrice price=$nochex_max_order_value} assign='max_order_val_minus_cart_value'}

Due to the above code, we can now tell if the value of the shopping cart is more than we can accept via Nochex. So if we were to use pounds as an example, the equation would be £600 minus the shopping cart value. If this figure is a negative number (e.g. £600-£700 = minus £100), we need to stop Nochex from being displayed.

To stop Nochex from showing up as a payment option, we use the next line of code:


{if $max_order_val_minus_cart_value > 0}

This is an "if" statment. An "if" statment will only run the code wrapped in its "if" tags when the value meets the required criteria. So if we had the code:


{if $max_order_val_minus_cart_value > 0}
text to be displayed
{/if}

it would only show the wording "text to be displayed" if the value provided was greater than zero. As such, if we place the {/if} at the end of the nochex.tpl file, all the code in the file will only be run/displayed if the criteria is met. In this case if the value is under £600.

That's it. Your website should now restrict the payment method (in this case Nochex) from showing up if the maximum value you can accept is exceeded in the cart.

For your convenience, the whole code is listed below with the above edits (remember to change 600 to your required value).


{600 assign='nochex_max_order_value'}
{math equation='b-a' a=$total_price b={toolsConvertPrice price=$nochex_max_order_value} assign='max_order_val_minus_cart_value'}
{if $max_order_val_minus_cart_value > 0}

<!-- The <p> tags below are details of the button which users select to go to Nochex page.-->
<style>
a.nochex:after {
display: block;
content: "\f054";
position: absolute;
right: 15px;
margin-top: -11px;
top: 50%;
font-family: "FontAwesome";
font-size: 25px;
height: 22px;
width: 14px;
color: #777777;
}
</style>

<p class="payment_module">
	<a href="javascript:$('#nochex_form').submit();" title="{l s='Pay with Nochex APC' mod='NoChex'}" style="padding: 0;width: 49%;" class="nochex">
		
		<img src="https://www.nochex.com/logobase-secure-images/logobase-banners/clear-amex-mp-vme.png" style="height:100px;">&nbsp;
	
	</a>
</p>

<!-- The form below is details from the nochex.php form which variables are attached to the relevant field. These will be sent to Nochex. -->
<form action="https://secure.nochex.com/" method="post" id="nochex_form" class="hidden">
	<input type='hidden' name='amount' value="{$amount}" />
	<input type='hidden' name='description' value="Order #{$order_id} details: {$description}" />
	<input type='hidden' name='xml_item_collection' value="{$xml_item_collection}" />
	<input type='hidden' name='postage' value="{$postage}" />
	<input type='hidden' name='billing_fullname' value="{$billing_fullname}" />
	<input type='hidden' name='billing_address' value="{$billing_address}" />
	<input type='hidden' name='billing_city' value="{$billing_city}" />
	<input type='hidden' name='billing_postcode' value="{$billing_postcode}" />
	<input type='hidden' name='delivery_fullname' value="{$delivery_fullname}" />
	<input type='hidden' name='delivery_address' value="{$delivery_address}" />
	<input type='hidden' name='delivery_city' value="{$delivery_city}" />
	<input type='hidden' name='delivery_postcode' value="{$delivery_postcode}" />
    <input type='hidden' name='customer_phone_number' value="{$customer_phone_number}" />
	<input type='hidden' name='email_address' value="{$email_address}" />
	<input type='hidden' name='order_id' value="{$order_id}" />
	<input type='hidden' name='optional_1' value="{$optional_1}" />
	<input type='hidden' name='merchant_id' value="{$merchant_id}" />
    <input type='hidden' name='success_url' value="{$successurl}"/>
	<input type='hidden' name='test_success_url' value="{$successurl}"/>
	<input type='hidden' name='cancel_url' value="{$cancelurl}"/>
	<input type='hidden' name='declined_url' value="{$cancelurl}"/>
	<input type='hidden' name='callback_url' value="{$responderurl}" />
	<input type='hidden' name='test_transaction' value="{$test_transaction}" />
</form>

{/if}



Leave a comment

Has this guide helped you? Have an idea for a guide or need help? Let us know below.

© Copyright 2014 | Squirrel Hosting Limited
Company Number 9031308