News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Disabling payment methods based on selected shipment methods

Started by Ghost, April 21, 2016, 11:10:50 AM

Previous topic - Next topic

Ghost

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):

<?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:

<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:

if($method->shipments && in_array($cart->virtuemart_shipmentmethod_id,$method->shipments))
{
return false;
}

Ghost

Anyone want to take a look at this? Important core feature, simple to implement..

lindapowers

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

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.