VirtueMart Forum

VirtueMart 2 + 3 + 4 => Plugins: Payment, Shipment and others => Topic started by: webgobe on April 07, 2023, 17:16:55 PM

Title: Joomla 4 - triggering an event on order status change
Post by: webgobe on April 07, 2023, 17:16:55 PM
For Joomla 3 I used plgVmCouponUpdateOrderStatus, and worked perfectly.
On Joomla 4 the events whose name does not start with "on" no longer work. I tried to find in VM 4.0.12 I could use instead to catch the order status change event (I need that to trigger actions in a custom built component) but no luck so far. Any hint, idea or solution is highly appreciated!
Title: Re: Joomla 4 - triggering an event on order status change
Post by: GJC Web Design on April 08, 2023, 11:25:50 AM
plgVmOnUpdateOrderPayment  ?
Title: Re: Joomla 4 - triggering an event on order status change
Post by: webgobe on April 08, 2023, 12:31:57 PM
Thanks, will try your tip obviously, the problem seemingly is not the triggering event - the one I used is still there in the code of VM - but this:

Quote"On Joomla 4 the events whose name does not start with "on" no longer work."

I did not marked it correctly to be obvious, but the sentence is from this page: https://docs.joomla.org/Potential_backward_compatibility_issues_in_Joomla_4#Plugins_.28Events.29
Title: Re: Joomla 4 - triggering an event on order status change
Post by: webgobe on April 09, 2023, 09:03:40 AM
Yup, as I suspected. Nothing. Not even an error message. Anyways, the client needs the event to be triggered when the order is confirmed by shop admin :(
Title: Re: Joomla 4 - triggering an event on order status change
Post by: GJC Web Design on April 09, 2023, 18:41:07 PM
Works fine for me

QuoteAnyways, the client needs the event to be triggered when the order is confirmed by shop admin

That is what I use it for

function plgVmOnUpdateOrderPayment ($data,$old_order_status) {
                    $status = $this->params->get('status', 'C');
                    $status_array = explode(',',$status);
      if(in_array($data->order_status, $status_array)){
                               // do something
                           }
                   }


Title: Re: Joomla 4 - triggering an event on order status change
Post by: webgobe on April 16, 2023, 11:22:54 AM
I have this:

function plgVmOnUpdateOrderPayment ($data,$old_order_status){
Factory::getApplication()->enqueueMessage('Royalty check  on UpdateOrderStatus', 'message');
vmdebug('Royalty check  on UpdateOrderStatus '.$data->order_status);
// .... and some other code
}

I get nothing, The plugin isn't triggered. The code works on J3...
Title: Re: Joomla 4 - triggering an event on order status change
Post by: webgobe on April 17, 2023, 12:36:13 PM
Quote from: GJC Web Design on April 09, 2023, 18:41:07 PM
Works fine for me

That is what I use it for
On Joomla 4/VM 4.0.12??
Title: Re: Joomla 4 - triggering an event on order status change
Post by: GJC Web Design on April 17, 2023, 13:27:07 PM
yep -- exactly the code above in a VMCustom plugin triggered by the order going to Confirmed
Title: Re: Joomla 4 - triggering an event on order status change
Post by: webgobe on April 18, 2023, 08:24:13 AM
Can you please share the full plugin code??? I am doing something wrong somewhere, and can't find out what... :(
Title: Re: Joomla 4 - triggering an event on order status change
Post by: GJC Web Design on April 18, 2023, 14:57:44 PM
I have pasted the relevant code here.. the rest has nothing to do with the triggering

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.0" type="plugin" group="vmcustom" method="upgrade">
        <name>Order Save for VirtueMart</name>
        <author>GJC</author>
        <creationDate>April 2023</creationDate>
        <copyright>www.GJCWebdesign.com</copyright>
        <license>GNU General Public License</license>
        <authorEmail>webmaster@gjcwebdesign.com</authorEmail>
        <authorUrl>https://www.gjcwebdesign.com</authorUrl>
        <version>2.2.0</version>
        <description>Virtuemart Plugin to save order details incl. images for each order. Needs the GJC VMReview 3.0.0 component.</description>

        <files>
                <filename plugin="ordersave">ordersave.php</filename>
<folder>language</folder>
        </files>

<config>
<fields name="params" addpath="/administrator/components/com_virtuemart/elements">
<fieldset name="basic">
<field name="status" type="vmorderstate" scope="com_virtuemart" default="C" size="3"
               label="VMCUSTOM_ORDERSAVE_STATUS_LABEL" description="VMCUSTOM_ORDERSAVE_STATUS_DESC"/>
<field
name="noreg"
type="radio"
default="1"
label="VMCUSTOM_ORDERSAVE_NOREG_LABEL"
description="VMCUSTOM_ORDERSAVE_NOREG_DESC">

<option
value="0">JNO</option>
<option
value="1">JYES</option>
</field>
<field
name="nameuse"
type="radio"
default="1"
label="VMCUSTOM_ORDERSAVE_NAMEUSE_LABEL"
description="VMCUSTOM_ORDERSAVE_NAMEUSE_DESC">

<option
value="1">VMCUSTOM_ORDERSAVE_NAMEUSE2</option>
<option
value="2">VMCUSTOM_ORDERSAVE_NAMEUSE3</option>
</field>

<field
name="imagehandle"
type="radio"
default="0"
label="VMCUSTOM_ORDERSAVE_IMAGEH_LABEL"
description="VMCUSTOM_ORDERSAVE_IMAGEH_DESC">
<option
value="0">VMCUSTOM_ORDERSAVE_IMAGEH1</option>
<option
value="1">VMCUSTOM_ORDERSAVE_IMAGEH2</option>
<option
value="2">VMCUSTOM_ORDERSAVE_IMAGEH3</option>
</field>

<field
name="imagefolder"
type="text"
size="40"
default="/media/ordersave/"
label="VMCUSTOM_ORDERSAVE_IMGF_LABEL"
description="VMCUSTOM_ORDERSAVE_IMGF_DESC" />

<field
name="debug"
type="radio"
default="0"
label="VMCUSTOM_ORDERSAVE_DEBUG_LABEL"
description="VMCUSTOM_ORDERSAVE_DEBUG_DESC">
<option
value="0">JNo</option>
<option
value="1">JYes</option>
</field>



</fieldset>

</fields>

</config>
</extension>



<?php
/*------------------------------------------------------------------------
# ordersave.php v2.1.8
# ------------------------------------------------------------------------
# @license - GNU/GPL http://www.gnu.org/copyleft/gpl.html
# Author: GJC Web Design
# Websites:  https://www.gjcwebdesign.com
-------------------------------------------------------------------------*/

defined('_JEXEC') or die('Restricted access');

if (!
class_exists('vmPSPlugin'))
    require(
JPATH_VM_PLUGINS DS 'vmpsplugin.php');

if (!
class_exists'VmConfig' )) require(JPATH_ROOT .'/administrator/components/com_virtuemart/helpers/config.php');
VmConfig::loadConfig ();

class 
plgVmCustomOrdersave extends vmPSPlugin {

    
// instance of class
    
public static $_this false;

    function 
__construct(& $subject$config) {
parent::__construct($subject$config);
    }

  
function plgVmOnUpdateOrderPayment ($data,$old_order_status) {
$app JFactory::getApplication();
if (!$app->isClient('administrator')){
}
$status $this->params->get('status''C');
$status_array explode(',',$status);
if(in_array($data->order_status$status_array)){

//Check if already saved
$db JFactory::getDbo();
$query '';
$db->setQuery($query);
$already $db->loadResult();

if(empty($already)){
$result $this->plgVmOrdersave($data,$old_order_status );
}else{
vmAdminInfo("Order already saved to GJC Reviews table");
return;
}
if ($result == 'saved') {
vmAdminInfo("Order saved to GJC Reviews table");
}elseif ($result == 'noreg') {
vmAdminInfo("User is not registered - order not saved to GJC Reviews");
}else{
vmAdminInfo("Order save to GJC Reviews table didn't work :( ");
}
}else{
#print 'Debug Line '.__LINE__.' $status_array <pre>'; print_r ($status_array); print "</pre><br />\n";die();
}
    
}
}


Title: Re: Joomla 4 - triggering an event on order status change
Post by: webgobe on April 18, 2023, 17:08:00 PM
Thank you!! Will get back to you as I test it.
Title: Re: Joomla 4 - triggering an event on order status change
Post by: webgobe on April 18, 2023, 17:20:30 PM
Bingo!!! Worked, thank you!