11 replies [Last post]
psynaptic's picture
Offline
Early adopter... addicted to alphas.Not KulvikTheminator
Joined: 08/28/2007
Juice: 731
Was this information Helpful?

Does anyone know if it's possible to only allow a payment method (specifically PP WPS) using order total?

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Allow PayPal WPS only if order total below limit

It's only possible in a custom module. Enable all the payment methods you want to make available and remove the invalid ones from the checkout form using hook_form_alter(). I've done this for a recurring fee module that I'll hopefully commit sometime before Christmas.

psynaptic's picture
Offline
Early adopter... addicted to alphas.Not KulvikTheminator
Joined: 08/28/2007
Juice: 731
Re: Re: Allow PayPal WPS only if order total below limit

Thanks a lot Ryan, will do that then. Good luck meeting your tight deadline, it's only 11 months away Eye-wink

psynaptic's picture
Offline
Early adopter... addicted to alphas.Not KulvikTheminator
Joined: 08/28/2007
Juice: 731
Re: Re: Allow PayPal WPS only if order total below limit

Hi Ryan, I've managed to remove the PayPal option but need to get the order total. How can I do this?

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Re: Re: Allow PayPal WPS only if order total below limit

Well... unfortunately if you're doing this at page load, I guess you can only really go by the products' subtotal since that's all you'll know at the time. In this case you'd have to call uc_cart_get_contents() and tally up the total. Otherwise, to take into account any sort of shipping fee, discount, coupon, etc. you'd have to somehow hook into the order total preview which may require some core alteration or something. Puzzled

psynaptic's picture
Offline
Early adopter... addicted to alphas.Not KulvikTheminator
Joined: 08/28/2007
Juice: 731
Re: Re: Re: Re: Allow PayPal WPS only if order total below limit

Hey Ryan, thanks for the instructions. The project I have been asked to do this for doesn't use any shipping or tax calculations so that's not an issue at the moment. This is what I have so far:

<?php
/**
* Implementation of hook_form_alter().
*/
function uc_paypal_limit_form_alter($form_id, &$form) {
 
// build the input textarea for the limit amount in paypal wps settings page
 
if ($form_id == 'uc_payment_methods_form') {
   
$form['method_paypal_wps']['uc_paypal_wps_paypal_limit'] = array(
     
'#type' => 'textfield',
     
'#title' => t('Order total limit'),
     
'#description' => t('Enter the value above which PayPal will be deactivated at checkout.'),
     
'#size' => 5,
     
'#maxlength' => 5,
     
'#default_value' => variable_get('uc_paypal_limit_amount', 1000),
    );
  }
 
 
// remove the paypal wps option from the checkout form
 
if ($form_id == 'uc_cart_checkout_form') {

   

// Load the current shopping cart.
   
$items = uc_cart_get_contents();
   
//print "<pre>"; print_r($items); print "</pre>";
   
   
foreach ($items as $item) {
     
$item_cost = $item->price * $item->qty;
     
$order_total += $item_cost;
    }
    if (
$order_total > 250) {
     
drupal_set_message('PayPal is not allowed for orders over £250');
      unset(
$form['panes']['payment']['payment_method']['#options']['paypal_wps']);
    }
  }
}
?>

As you can see I've added a textfield to the PayPal WPS admin page to control the limit amount. I've not implemented the saving of the value since I don't yet know how to do it. I'm working on it!

psynaptic's picture
Offline
Early adopter... addicted to alphas.Not KulvikTheminator
Joined: 08/28/2007
Juice: 731
Re: Re: Re: Re: Re: Allow PayPal WPS only if order total below l

Well, here is what I have managed so far. It works but I'm sure it can be improved. Comments would be appreciated.

<?php
// $Id$

/**
* @file
* Limits the use of PayPal using order total value
*/

/*******************************************************************************
* Hook Functions (Drupal)
******************************************************************************/

/**
* Implementation of hook_form_alter().
*/

function uc_paypal_limit_form_alter($form_id, &$form) {
 
// build the input textarea for the limit amount in paypal wps settings page
 
if ($form_id == 'uc_payment_methods_form') {
   
$form['method_paypal_wps']['uc_paypal_wps_paypal_limit'] = array(
     
'#type' => 'textfield',
     
'#title' => t('Order total limit'),
     
'#description' => t('Enter the value above which PayPal will be deactivated at checkout.'),
     
'#size' => 5,
     
'#maxlength' => 5,
     
'#default_value' => variable_get('uc_paypal_limit_amount', 250),
     
'#validate' => array('uc_paypal_limit_validate' => array()),
    );
   
$form['#submit'] = array_merge(array('uc_paypal_limit_submit' => array()), $form['#submit']);
  }

  if (

$form_id == 'uc_cart_checkout_form') {

   

// Load the current shopping cart.
   
$items = uc_cart_get_contents();
   
//print "<pre>"; print_r($items); print "</pre>";
   
   
foreach ($items as $item) {
     
$item_cost = $item->price * $item->qty;
     
$order_total += $item_cost;
    }
    if (
$order_total > variable_get('uc_paypal_limit_amount', 250)) {
     
$limit = variable_get('uc_paypal_limit_amount', 250);
     
drupal_set_message('PayPal is not allowed for orders over £' .$limit. '.');
     
// remove the paypal wps option from the checkout form
     
unset($form['panes']['payment']['payment_method']['#options']['paypal_wps']);
    }
  }
}

/**
* Implementation of hook_validate().
*/
function uc_paypal_limit_validate($form_values) {
  if (isset(
$form_values['#value']) && (!is_numeric($form_values['#value']))) {
   
form_set_error('uc_paypal_wps_paypal_limit', t('Please enter a number.'));
  }
}

/**
* Implementation of hook_submit().
*/
function uc_paypal_limit_submit($form_id, $form_values) {
 
$limit = $form_values['uc_paypal_wps_paypal_limit'];
 
variable_set('uc_paypal_limit_amount', $limit);
}
?>
psynaptic's picture
Offline
Early adopter... addicted to alphas.Not KulvikTheminator
Joined: 08/28/2007
Juice: 731
Re: Re: Re: Re: Re: Re: Allow PayPal WPS only if order total bel

Ok. Here's another update:

<?php
// $Id$

/**
* @file
* Limits the use of PayPal using order total value
*/

/*******************************************************************************
* Hook Functions (Drupal)
******************************************************************************/

/**
* Implementation of hook_form_alter().
*/

function uc_paypal_limit_form_alter($form_id, &$form) {
 
// build the input textarea for the limit amount in paypal wps settings page
 
if ($form_id == 'uc_payment_methods_form') {
   
$form['method_paypal_wps']['uc_paypal_wps_limit_amount'] = array(
     
'#type' => 'textfield',
     
'#title' => t('Order total limit'),
     
'#description' => t('Enter the value above which WPS will be deactivated at checkout. Entering 0 here disables the limit.'),
     
'#size' => 5,
     
'#maxlength' => 5,
     
'#default_value' => variable_get('uc_paypal_wps_limit_amount', 250),
     
'#validate' => array('uc_paypal_wps_limit_amount_validate' => array()),
    );
   
$form['method_paypal_wps']['uc_paypal_wps_limit_enable'] = array(
     
'#type' => 'checkbox',
     
'#title' => t('Enable order total limit'),
     
'#description' => t('Enable the order total limit.'),
     
'#default_value' => variable_get('uc_paypal_wps_limit_enable', FALSE),
    );
   
$form['#submit'] = array_merge(array('uc_paypal_limit_submit' => array()), $form['#submit']);
  }
  if (
$form_id == 'uc_cart_checkout_form') {
   
// Load the current shopping cart.
   
$items = uc_cart_get_contents();
   
//print "<pre>"; print_r($items); print "</pre>";
   
foreach ($items as $item) {
     
$item_cost = $item->price * $item->qty;
     
$order_total += $item_cost;
    }
    if (
variable_get('uc_paypal_wps_limit_enable', FALSE) == TRUE) {
      if (
variable_get('uc_paypal_wps_limit_amount', 250) > 0) {
        if (
$order_total > variable_get('uc_paypal_wps_limit_amount', 250)) {
         
$limit = variable_get('uc_paypal_wps_limit_amount', 250);
         
//drupal_set_message('PayPal is not allowed for orders over £' .$limit. '.');
         
unset($form['panes']['payment']['payment_method']['#options']['paypal_wps']);
        }
      }
    }
  }
}

/**
* Implementation of hook_validate().
*/
function uc_paypal_wps_limit_amount_validate($form_values) {
  if (isset(
$form_values['#value'])) {
    if (!
is_numeric($form_values['#value'])) {
     
form_set_error('uc_paypal_wps_limit_amount', t('Please enter a number.'));
    }
    elseif ((
$form_values['#value']) < 0) {
     
form_set_error('uc_paypal_wps_limit_amount', t('Please enter a non-negative number.'));
    }
  }
}

/**
* Implementation of hook_submit().
*/
function uc_paypal_limit_submit($form_id, $form_values) {
 
//dpr($form_values);
 
if (isset($form_values['uc_paypal_wps_limit_amount'])) {
   
$limit = $form_values['uc_paypal_wps_limit_amount'];
   
variable_set('uc_paypal_wps_limit_amount', $limit);
  }
  if (isset(
$form_values['uc_paypal_wps_limit_enable'])) {
   
$state = $form_values['uc_paypal_wps_limit_enable'];
   
variable_set('uc_paypal_wps_limit_enable', $state);
  }
}
?>

Do you think it would be worth me making this work with other payment modules and submitting it as a contribution? This is my first drupal module (that does anything useful at least).

psynaptic's picture
Offline
Early adopter... addicted to alphas.Not KulvikTheminator
Joined: 08/28/2007
Juice: 731
Re: Re: Re: Re: Re: Re: Re: Allow PayPal WPS only if order total

I've just cleaned up the unneeded calls to variable_get() by setting a local variable in the hook_form_alter().

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Cool work. I'm sure

Cool work. Smiling I'm sure someone would find it useful to have this posted as a contrib... even if you don't end up making it generic for other payment methods, you could stick this up as a contrib and we could just tell folks to take your code and modify for their needs. Congrats on your first module. Eye-wink

psynaptic's picture
Offline
Early adopter... addicted to alphas.Not KulvikTheminator
Joined: 08/28/2007
Juice: 731
Re: Cool work. I'm sure

Thanks Ryan! Very encouraging stuff.

I would like to at least make it a little bit more flexible so I'll look into doing that before I add it as a contrib. Hopefully it will teach me a little more about module development in general.

psynaptic's picture
Offline
Early adopter... addicted to alphas.Not KulvikTheminator
Joined: 08/28/2007
Juice: 731
Re: Re: Cool work. I'm sure

I posted this a while ago but incase anyone is reading this and looking for the contrib, here it is: http://www.ubercart.org/contrib/3195