Reputation: 136
I have written a small php script that I want to execute every time an order status changes to "Confirmed".
Ι'm aware that I can put it directly in the order.php file, however doing so will delete my script when virtuemart gets updated. Is there any other solution?
Upvotes: 0
Views: 86
Reputation: 663
This is old thread but here is how you can create a plugin that will do what you are looking for. This will be a /plugins/system/vmorderwebook This is plugin I am using to listen for order payment confirmation and calling curl to make a call to external service. This is good starting point for making what you need. Interesting is that I could not find such plugin / addon. (language files are not included)
<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" version="3.0" method="upgrade" group="system">
<name>System - Reflect VM Order Webhook</name>
<author>Reflect DEV</author>
<creationDate>April 2024</creationDate>
<copyright>Copyright (C) 2024. All rights reserved.</copyright>
<license>GNU General Public License version 2 or later</license>
<version>1.0.0</version>
<description>PLG_SYSTEM_VMORDERWEBHOOK_XML_DESCRIPTION</description>
<files>
<filename plugin="vmorderwebhook">vmorderwebhook.php</filename>
<folder>language</folder>
</files>
<!-- <languages folder="language">
<language tag="en-GB">en-GB/en-GB.plg_system_vmorderwebhook.ini</language>
<language tag="en-GB">en-GB/en-GB.plg_system_vmorderwebhook.sys.ini</language>
</languages> -->
<config>
<fields name="params">
<fieldset name="basic">
<field
name="webhook_url"
type="text"
label="PLG_SYSTEM_VMORDERWEBHOOK_WEBHOOK_URL_LABEL"
description="PLG_SYSTEM_VMORDERWEBHOOK_WEBHOOK_URL_DESC"
size="50"
default=""
/>
</fieldset>
</fields>
</config>
</extension>
<?php
defined('_JEXEC') or die;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Factory;
// Load VirtueMart
if (!class_exists('VmConfig')) {
$vmPath = JPATH_ROOT . '/administrator/components/com_virtuemart';
if (file_exists($vmPath)) {
require($vmPath . '/helpers/config.php');
VmConfig::loadConfig();
if (!class_exists('vmPlugin')) {
require(VMPATH_ADMIN . '/plugins/vmplugin.php');
}
if (!class_exists('vmCustomPlugin')) {
require(VMPATH_ADMIN . '/plugins/vmcustomplugin.php');
}
}
}
class PlgSystemVmorderwebhook extends vmPlugin
{
public $autoloadLanguage = true;
public $_tablename = '#__virtuemart_payment_plg_vmorderwebhook';
public $_tableId = 'id';
public $_loggingEnabled = true;
public $_cryptedFields = array();
public function __construct(&$subject, $config)
{
parent::__construct($subject, $config);
Log::addLogger(
array(
'text_file' => 'vmorderwebhook.log.php'
),
Log::ALL,
array('vmorderwebhook')
);
}
public function getVmPluginCreateTableSQL()
{
return array();
}
function getTableSQLFields()
{
return array();
}
public function plgVmOnUpdateOrderPayment($data, $old_order_status)
{
$this->handleOrder($data, "plgVmOnUpdateOrderPayment");
return true;
}
private function handleOrder($orderData, $event = "--")
{
try {
$orderId = isset($orderData->virtuemart_order_id) ? $orderData->virtuemart_order_id :
(isset($orderData['virtuemart_order_id']) ? $orderData['virtuemart_order_id'] : null);
if (!$orderId) {
return false;
}
$orderInfo = $this->getOrderInfo($orderId);
if ($orderInfo) {
$orderInfo->items = $this->getOrderItems($orderId);
$orderInfo->event = $event;
$this->sendWebhook($orderInfo);
}
} catch (Exception $e) {
return false;
}
return true;
}
private function getOrderInfo($orderId)
{
try {
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select('o.*, u.email, u.name')
->from($db->quoteName('#__virtuemart_orders', 'o'))
->leftJoin(
$db->quoteName('#__users', 'u') .
' ON ' . $db->quoteName('o.virtuemart_user_id') . ' = ' . $db->quoteName('u.id')
)
->where($db->quoteName('o.virtuemart_order_id') . ' = ' . (int)$orderId);
$db->setQuery($query);
$result = $db->loadObject();
return $result;
} catch (Exception $e) {
return false;
}
}
private function getOrderItems($orderId)
{
try {
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from($db->quoteName('#__virtuemart_order_items'))
->where($db->quoteName('virtuemart_order_id') . ' = ' . (int)$orderId);
$db->setQuery($query);
$result = $db->loadObjectList();
return $result;
} catch (Exception $e) {
return array();
}
}
private function sendWebhook($orderInfo)
{
// Do what every ou want here
// try {
// $webhookUrl = $this->params->get('webhook_url', '');
// if (empty($webhookUrl)) {
// return false;
// }
// $data = json_encode($orderInfo);
// $ch = curl_init($webhookUrl);
// curl_setopt_array($ch, array(
// CURLOPT_POST => true,
// CURLOPT_POSTFIELDS => $data,
// CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
// CURLOPT_RETURNTRANSFER => true,
// CURLOPT_SSL_VERIFYPEER => false,
// CURLOPT_SSL_VERIFYHOST => 0,
// CURLOPT_TIMEOUT => 30,
// CURLOPT_CONNECTTIMEOUT => 10
// ));
// $result = curl_exec($ch);
// $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// if ($result === false) {
// curl_close($ch);
// return false;
// }
// curl_close($ch);
// return true;
// } catch (Exception $e) {
// return false;
// }
}
public function plgVmOnStoreInstallPluginTable($psType)
{
return $this->onStoreInstallPluginTable($psType);
}
public function plgVmDeclarePluginParamsPayment($name, $id, &$data)
{
return $this->declarePluginParams('system', $name, $id, $data);
}
public function plgVmSetOnTablePluginParamsPayment($name, $id, &$table)
{
return $this->setOnTablePluginParams($name, $id, $table);
}
}
Upvotes: 0