Update Payment/shipment plugin using new core restrictions

Written by Max Milbers on .

The VirtueMart Core version 3.4.3.100018 has new restrictions for shipment and payment methods, which are provided by the core. 

  • By shoppergroup
  • category, blocking category
  • country, blocking country
  • min, max amount
  • shipment

It is relativly simple to update an existing plugin to use the provides restrictions. The system is implemented als legacy default mode system. So any unadjusted plugin works as before.

First we must update the xml file and tell VirtueMart that the plugin is updated. Just add within the group params

<field name="checkConditionsCore" value="1" default="1" type="hidden"/>

Virtuemart shows now the new core restrictions on the first tab. If not, check if you set the field within the correct group of fields

<fields name="params">

We need another line in the plugin constructor to add automatically the "varsToPush" to the method.

$this->addVarsToPushCore($varsToPush, 1);


The first parameter is your $varsToPush variable, which is later used in $this->setConfigParameterable($this->_configTableFieldName, $varsToPush). The second parameter says if it is a payment or shipment. Usually it looks like this, then

$varsToPush = $this->getVarsToPush();
$this->addVarsToPushCore($varsToPush, 1);
$this->setConfigParameterable($this->_configTableFieldName, $varsToPush);

Finally we just need to update the "checkConditions" function. For example

- The payment "default" uses any restrictions provided by the core and has no extra restrictions, it is enough to remove the whole function.
- in case of the paypal plugin, it is enough to test for the paypal extra restrictions (publishup, publishdown) and execute then the parent function.

protected function checkConditions($cart, $activeMethod, $cart_prices) {

//Check method publication start
if ($activeMethod->publishup) {
$nowDate = JFactory::getDate();
$publish_up = JFactory::getDate($activeMethod->publishup);
if ($publish_up->toUnix() > $nowDate->toUnix()) {
return FALSE;
}
}
if ($activeMethod->publishdown) {
$nowDate = JFactory::getDate();
$publish_down = JFactory::getDate($activeMethod->publishdown);
if ($publish_down->toUnix() <= $nowDate->toUnix()) {
return FALSE;
}
}

return parent::checkConditions($cart, $activeMethod, $cart_prices);
}

In case of the default shipment, we just could add two lines, to use the old values.


$method->min_amount = $method->orderamount_start;
$method->max_amount = $method->orderamount_stop;

The function plgVmSetOnTablePluginShipment regarding plgVmSetOnTablePluginPayment can be used to adjust old values to the new ones. In the core we removed this dirty construction, and just updated the keys by sql.

$q = 'UPDATE `#__virtuemart_shipmentmethods` SET `shipment_params`= REPLACE(`shipment_params`, "orderamount_start", "min_amount") WHERE `shipment_element` ="weight_countries"';

How to write a backward compatible plugin

The xml value is just ignored, so we dont need todo anything there. The constructor must consider that the function addVarsToPushCore may not exist, so we just use

$varsToPush = $this->getVarsToPush();
if(method_exists($this,'addVarsToPushCore')) $this->addVarsToPushCore($varsToPush, 1);
$this->setConfigParameterable($this->_configTableFieldName, $varsToPush);

So when we cannot replace the whole checkConditions anyway, then we just use there at begin

if(method_exists($cart,'getST')){
$address = $cart->getST();
} else {
$address = (($cart->ST == 0) ? $cart->BT : $cart->ST);
}

or, when we could remove the whole function checkConditions for the vm3.6+ core, then we just use at begin

if(method_exists($cart,'getST')){
return parent::checkConditions($cart, $method, $cart_prices);
}