Author Topic: Disabling payment methods based on selected shipment methods  (Read 1500 times)

Ghost

  • Jr. Member
  • **
  • Posts: 419
Disabling payment methods based on selected shipment methods
« on: April 21, 2016, 11:10:50 am »
This shows how to add ability to disable payment methods based on selected shipment methods. E.g. to disable cash payment on parcel delivery. This is an important feature and should be part of the core. Especially considering it's so simple to integrate.

This is what to do:

1. Create shipments field type (/administrator/components/com_virtuemart/fields/vmshipments.php):

Code: [Select]
<?php
defined
('JPATH_PLATFORM') or die;
if (!
class_exists('VmConfig')) require JPATH_ROOT.'/administrator/components/com_virtuemart/helpers/config.php';
JFormHelper::loadFieldClass('list');
jimport('joomla.form.formfield');

class 
JFormFieldVmShipments extends JFormFieldList {
var $type 'vmshipments';

protected function getOptions() {

VmConfig::loadConfig();
VmConfig::loadJLang('com_virtuemart'false);

$sModel VmModel::getModel('shipmentmethod');
$values $sModel->getShipments();

foreach ($values as $v) {
$options[] = JHtml::_('select.option'$v->virtuemart_shipmentmethod_id$v->shipment_name.' ('.$v->virtuemart_shipmentmethod_id.')');
}
return $options;
}

}

2. In payment plugin XML files add a shipments field:

Code: [Select]
<field name="shipments" type="vmshipments" label="VMPAYMENT_STANDARD_SHIPMENTS" default="" multiple="multiple" />
3. In payment plugin's PHP file, inside checkConditions function add this check:

Code: [Select]
if($method->shipments && in_array($cart->virtuemart_shipmentmethod_id,$method->shipments))
{
return false;
}

Ghost

  • Jr. Member
  • **
  • Posts: 419
Re: Disabling payment methods based on selected shipment methods
« Reply #1 on: May 03, 2016, 12:51:24 pm »
Anyone want to take a look at this? Important core feature, simple to implement..

lindapowers

  • Full Member
  • ***
  • Posts: 1335
  • If you're going through hell, keep going.
    • Venta de naranjas online y mandarinas
  • Skype Name: manu.gonzalez91
  • VirtueMart Version: Latest avi
Re: Disabling payment methods based on selected shipment methods
« Reply #2 on: May 03, 2016, 20:18:17 pm »
Most people use 3rd party one page checkouts and the most popular from Rupostel already allows this option to disable payments on specific shipments.

However I agree with you that it will be a nice addon if is as simple as you show it.

Regards

jimijot

  • Beginner
  • *
  • Posts: 7
Re: Disabling payment methods based on selected shipment methods
« Reply #3 on: May 31, 2016, 23:59:34 pm »
Thank you Ghost for you advice, it works perfectly, but only for payments that use function in constructor:
$varsToPush = $this->getVarsToPush ();
in main .php payment file.

Unfortunately it doesn't work for example with paypal method because it uses self defined $varsToPush:

$varsToPush = array(
         'paypal_merchant_email' => array('', 'char'),
         'accelerated_onboarding' => array('', 'int'),
         'api_login_id' => array('', 'char'),
         ......
);


Unfortunately more payment methods don't use $varsToPush = $this->getVarsToPush (); declaration.

Solution is to replace self defined $varsToPush with function getVarsToPush as described above.

It is important to use new option:
Ajax for OPC
in configuration menu in VM and script from new cart template:
components\com_virtuemart\views\cart\tmpl\default.php
If you have old template, you must refresh it.