Problems getting custom onAfterOrderCreate to work

  • Posts: 32
  • Thank you received: 2
12 years 3 months ago #62289

As per the documentation I've studied the History plugin in order to create my own plug in. In fact I simply plagiarized the entire History plugin in an attempt to simply fire off an email to myself to indicate that the plugin callback hook has been called! Only problem is, it doesn't get called and I can't work out why.

I created a zip containing a modified set of three files (contents below), same as the History plugin. Joomla quite happily installed this new plugin, and it's showing as enabled in the plugins list. Hikashop also shows my custom plugin and allows me to configure it. I added a "mail()" line to the History plugin, same as my custom plugin, but when I order a test product, I get the email from the History plugin but not the custom plugin (and also get the "order confirmed" email).

Here's the hikasim.php file:

<?php
defined('_JEXEC') or die('Restricted access');
?>
<?php
class plgHikashopSim extends JPlugin
{
	function plgHikashopSim(&$subject, $config){
		parent::__construct($subject, $config);
		if(!isset($this->params)){
			$plugin =& JPluginHelper::getPlugin('hikashop', 'hikasim');
			jimport('joomla.html.parameter');
			$this->params = new JParameter( $plugin->params );
		}
    }
    function onAfterOrderCreate(&$order,&$send_email){
    	if(!empty($order->order_id)){

        mail("xxx@yyy.co.uk", "Hikashop test 2", "Order ID: " + $order->order_id);
    	}
		return true;
    }
}

Here's the XML file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE install SYSTEM "http://dev.joomla.org/xml/1.5/plugin-install.dtd">
<install type="plugin" version="1.5" method="upgrade" group="hikashop">

	<name>Hikashop Sim Plugin</name>
	<files>
		<filename plugin="hikasim">hikasim.php</filename>
	</files>
	<params addpath="/components/com_hikashop/params"></params>

</install>

I've masked/removed personal bits. Although I'm using Joomla 2.5 and the notes above refer to 1.5, the history module seems to work okay - backward compatible I guess. I'm using the free/basic version of Hikashop.

Any ideas please? Thanks.

Please Log in or Create an account to join the conversation.

  • Posts: 13201
  • Thank you received: 2322
12 years 3 months ago #62348

Hi GeoffM,

First of all, in your mail function try to change the

"Order ID: " + $order->order_id
by
"Order ID: ".$order->order_id
In your XML file, I'm not sure that you need the <params> tag.

Please Log in or Create an account to join the conversation.

  • Posts: 32
  • Thank you received: 2
12 years 3 months ago #62421

Xavier wrote: Hi GeoffM,

First of all, in your mail function try to change the

"Order ID: " + $order->order_id
by
"Order ID: ".$order->order_id
In your XML file, I'm not sure that you need the <params> tag.


Hi, thanks for the reply. The Mail() line is the same in the History module and my plugin (apart from subject line to differentiate between the two); it works from the History but not from my plugin. Anyway, I changed it and there was no difference - History mails, my plugin doesn't.

Please Log in or Create an account to join the conversation.

  • Posts: 83024
  • Thank you received: 13403
  • MODERATOR
12 years 3 months ago #62542

In your XML your plugin name is hikasim and its group is hikashop, so the plugin class name should be:
plgHikashopHikasim and not plgHikashopSim.
Since Joomla's event dispatcher doesn't found the correct class, it doesn't use your plugin.

Please Log in or Create an account to join the conversation.

  • Posts: 32
  • Thank you received: 2
12 years 3 months ago #62664

nicolas wrote: In your XML your plugin name is hikasim and its group is hikashop, so the plugin class name should be:
plgHikashopHikasim and not plgHikashopSim.
Since Joomla's event dispatcher doesn't found the correct class, it doesn't use your plugin.


Thanks, that sounds logical. However, assuming you mean to change the <filename plugin=...> line, it still doesn't work! Instead what happens now is Joomla installs into a directory called plgHikashopSim. In addition, this doesn't match the History plugin which has:
<filename plugin="history">history.php</filename>
and
class plgHikashopHistory extends JPlugin

I'll investigate further later.

Please Log in or Create an account to join the conversation.

  • Posts: 83024
  • Thank you received: 13403
  • MODERATOR
12 years 3 months ago #62666

No. I was saying to keep it like that and just change the line:
class plgHikashopSim extends JPlugin

to:
class plgHikashopHikasim extends JPlugin

The following user(s) said Thank You: GeoffM

Please Log in or Create an account to join the conversation.

  • Posts: 32
  • Thank you received: 2
12 years 3 months ago #62672

Apologies, I obviously didn't read your earlier post properly! It now works perfectly. Thank you.

Please Log in or Create an account to join the conversation.

  • Posts: 32
  • Thank you received: 2
12 years 3 weeks ago #80147

This was working perfectly through testing but now it's gone live I've suddenly realised I've been using the wrong function :woohoo: . Customers get download information when they create the order (exactly as the function says :) ) and before they've paid.

So I decided I just needed to change the method name to

function onAfterOrderConfirm(&$order,&$methods,$method_id){

. However, I can't get that to fire. I thought maybe it was a typo but I copied it directly from the dev manual. Does anybody have any ideas please? The very first "mail" line never gets called. All I've done is change the method name (and was only using the order parameter, so that shouldn't be an issue).
<?php
defined('_JEXEC') or die('Restricted access');
?>
<?php
class plgHikashophikasimsig extends JPlugin
{
	function plgHikashopSimSig(&$subject, $config){
		parent::__construct($subject, $config);
		if(!isset($this->params)){
			$plugin =& JPluginHelper::getPlugin('hikashop', 'hikasimsig');
			jimport('joomla.html.parameter');
			$this->params = new JParameter( $plugin->params );
		}
    }
   function onAfterOrderConfirm(&$order,&$methods,$method_id){
   mail("####", "Hika test", "Order ID: ".$order->order_id);
      if(!empty($order->order_id)){
   mail("####", "Hika test 2", "Order ID: ".$order->order_id);
         $class = hikashop_get('class.user');
         $user = $class->get($order->order_user_id);
         foreach($order->cart->products as $product){
   mail("####", "Hika test 3", "Order ID: ".$order->order_id);
            $path = $_SERVER['DOCUMENT_ROOT'];
            $path .= "####";
            include_once($path);
            hikalicense($order->order_id, $product->product_id, $user->user_cms_id, $product->order_product_quantity);
            mail("####", "Hikashop order notification", "Order ID: ".$order->order_id."; Product ID: ".$product->product_id."; User ID: ".$user->user_cms_id."; Count: ".$product->order_product_quantity);
         }
	 return true;
      }
   }
}

The ####s are where I've masked things out in this post.

Last edit: 12 years 3 weeks ago by GeoffM. Reason: ####

Please Log in or Create an account to join the conversation.

  • Posts: 13201
  • Thank you received: 2322
12 years 3 weeks ago #80196

Hi,

First of all your class name is different as your function name, try to change it. Else I don't see any problems.

Please Log in or Create an account to join the conversation.

  • Posts: 32
  • Thank you received: 2
12 years 3 weeks ago #80284

Xavier wrote: Hi,

First of all your class name is different as your function name, try to change it. Else I don't see any problems.


I appreciate the response but I don't see how that can help. Hikashop is looking for a class named in a particular way (see Nicolas' post). Then surely the functions contained within that class must match the onAfterXXX/onBeforeXXX methods?

Please Log in or Create an account to join the conversation.

  • Posts: 32
  • Thank you received: 2
12 years 3 weeks ago #80288

Just looked at the PayPal pluging. The class is declared as follows:

class plgHikashoppaymentPaypal extends JPlugin


and the method as follows:

function onAfterOrderConfirm(&$order,&$methods,$method_id){


Also other methods like function onPaymentNotification(&$statuses) and function onPaymentDisplay(&$order,&$methods,&$usable_methods).

I must have a typo somewhere but for the life of me I can't find it. No errors or warnings are generated (unless I deliberately type nonsense, then it does show an error).

Please Log in or Create an account to join the conversation.

  • Posts: 32
  • Thank you received: 2
12 years 2 weeks ago #80294

Reformatted, checked opening and closing { } but to no avail.

What I have managed to do, however, is use onAfterOrderUpdate:

function onAfterOrderUpdate(&$order,&$send_email){
	if(!empty($order->order_id)){
		if($order->order_status == 'confirmed'){
			$class = hikashop_get('class.user');
			$user = $class->get($order->order_user_id);
			mail("####", "Hikashop order complete 1", "Order ID: ".$order->order_id);
			foreach($order->cart->products as $product){
				$path = $_SERVER['DOCUMENT_ROOT'];
				$path .= "/####";
				include_once($path);
				####($order->order_id, $product->product_id, $user->user_cms_id, $product->order_product_quantity);
				mail("####", "Hikashop order complete 2", "Order ID: ".$order->order_id."; Product ID: ".$product->product_id."; User ID: ".$user->user_cms_id."; Count: ".$product->order_product_quantity);
			}
		}
	} // if
	return true;
}

However, the foreach never fires, presumably because the cart is empty at this point. I set the option "Clean cart when order is " to "confirmed" but that doesn't affect anything. I do wonder whether another plugin is emptying the cart, or maybe the system calls this after emptying the cart while in the confirmed stage.

Failing that, there must be something else in $order to find out what was ordered! More digging and reading.

Last edit: 12 years 2 weeks ago by GeoffM.

Please Log in or Create an account to join the conversation.

  • Posts: 26165
  • Thank you received: 4029
  • MODERATOR
12 years 2 weeks ago #80318

Hi,

What Xavier would told you is that the "constructor" function would not be called. So some parameters could not be loaded correctly by your plugin.
Please use "__construct" in order to be sure that the function would be called, even if you change the class name.

About your other problem. After a "Order Update", you might not have the complete cart.
What you can do is to load the full order.

$orderClass = hikashop_get('class.order');
$full_order = $orderClass->loadFullOrder($order->order_id, true, false);
At this moment, you would have the complete full order, with cart etc.

Best regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 32
  • Thank you received: 2
12 years 2 weeks ago #80336

Jerome wrote: Hi,

What Xavier would told you is that the "constructor" function would not be called. So some parameters could not be loaded correctly by your plugin.
Please use "__construct" in order to be sure that the function would be called, even if you change the class name.


To be honest, this is just as cryptic. All I can think of is that the constructor is supposed to have the same name as the class. Like C/C++/C# I guess. Like I say, I'm not a PHP programmer.

In any case, trying it of course it makes zero difference because we already know that two methods already get called; the third doesn't. Quite why is beyond me, and the documentation offers no clues, nor does poring through various other pieces of code. In fact I don't even know what that constructor is doing: I simply followed the instructions in the dev manual to examine the History plugin.

Jerome wrote: About your other problem. After a "Order Update", you might not have the complete cart.
What you can do is to load the full order.

$orderClass = hikashop_get('class.order');
$full_order = $orderClass->loadFullOrder($order->order_id, true, false);
At this moment, you would have the complete full order, with cart etc.

Best regards,


Still nothing. I can email myself just before the foreach and that gets received. Another email just inside the foreach never gets called. Here's the relevant chunk of code:
function onAfterOrderUpdate(&$order,&$send_email){
	if(!empty($order->order_id)){
		if($order->order_status == 'confirmed'){
			$class = hikashop_get('class.user');
			$user = $class->get($order->order_user_id);
			$orderClass = hikashop_get('class.order');
			$full_order = $orderClass->loadFullOrder($order->order_id, true, false);
			mail("####", "Hikashop onAfterOrderUpdate 1", "Order ID: ".$order->order_id);
			foreach($full_order->cart->products as $product){
				mail("####", "Hikashop onAfterOrderUpdate 2", "Order ID: ".$order->order_id);
				[...etc...]

I looked up loadFullOrder to see what those parameters were. ABout the only mention of it was "You can look at the function loadFullOrder for more information...". Fine. Where? Am I supposed to pore through code to work out what it does?

Please Log in or Create an account to join the conversation.

  • Posts: 26165
  • Thank you received: 4029
  • MODERATOR
12 years 2 weeks ago #80413

Hi,

loadFullOrder would load products in the $full_order->products array.
It would load addresses in $full_order->shipping_address and $full_order->billing_address objects.

$order->cart is for cart objects which "are becoming" orders (in triggers : onBeforeOrderCreate, onAfterOrderConfirm)

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 32
  • Thank you received: 2
12 years 2 weeks ago #80550

There was just too much weird stuff (not) going on so I decided to use the chunks out that were working and then do the rest manually. The onAfterOrderCreate now inserts the cart data into a custom table. Then the onAfterOrderUpdate pulls the data from the table and issues the licenses. The only nagging thought is if the order changes between creation and payment. But it's something I can live with as I've spent far too much trying to make this work in the first place. The potential cost in losses is way under what I would spend trying to fix this.

I wish my server supported .net. :(

Please Log in or Create an account to join the conversation.

  • Posts: 32
  • Thank you received: 2
11 years 5 months ago #111983

So for six months this has worked perfectly. Until now. Yet nothing's changed! I just updated to HikaShop Starter V2.2.0 just in case something changed with PayPal. What's even more frustrating is that sometimes it's working, sometimes it's not - though all of my most recent orders have failed.

After putting various debugging into the plugin and my own code, here is what have:

function onAfterOrderCreate(&$order,&$send_email){
	if(!empty($order->order_id)){
		$class = hikashop_get('class.user');
		$user = $class->get($order->order_user_id);
		foreach($order->cart->products as $product){
			$path = $_SERVER['DOCUMENT_ROOT'];
			$path .= "/xxxxxxx/hikalicense.php";
			include_once($path);
            		hikalicense_create($order->order_id, $product->product_id, $user->user_cms_id, $product->order_product_quantity);
		}
	}
	$send_email = false;

	return true;
}

Just before the "return true" I put some debugging in which correctly gets triggered, so I know that execution is returning from my stuff and back into the HikaShop code. I also have this code, in the same file:
function onAfterOrderUpdate(&$order,&$send_email){
	if(!empty($order->order_id)){
		if($order->order_status == 'confirmed'){
			$path = $_SERVER['DOCUMENT_ROOT'];
			$path .= "/xxxxxx/hikalicense.php";
			include_once($path);
			hikalicense_confirm($order->order_id);
		}
	} // if
	return true;
}

Now, admittedly I'm struggling to determine whether this actually gets called. However, I do know from the database that the order_status is created, not confirmed, so the second "if" fails.

I only have a PayPal payment method so everything is through that. When a customer orders a product I get an email from PayPal to confirm that payment has been made, with the HikaShop order number contained in that email. So it would seem that somehow there is a PayPal->payment confirmed->HikaShop order confirmed interaction issue. I've no ideas why though, whether it's a misconfiguration on my end or whatever. Any ideas appreciated.

Last edit: 11 years 5 months ago by GeoffM.

Please Log in or Create an account to join the conversation.

  • Posts: 26165
  • Thank you received: 4029
  • MODERATOR
11 years 5 months ago #111986

Hi,

I made a fix in the MassAction system, the package has been updated so you can download it.
It should resolve your problem.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 32
  • Thank you received: 2
11 years 5 months ago #111989

Jerome wrote: Hi,

I made a fix in the MassAction system, the package has been updated so you can download it.
It should resolve your problem.

Regards,


Wow, that was quick - thank you! Only issue is, where do I download it from? The download page still shows V2.2.0, albeit dated today and the package I downloaded earlier appears to be binary identical to one just now. This assumes you mean to download the entire Hika package rather than just the MassAction plugins/mods/whatever?

Please Log in or Create an account to join the conversation.

  • Posts: 26165
  • Thank you received: 4029
  • MODERATOR
11 years 5 months ago #111991

Hi,

The version did not grow up, we do not push HikaShop 2.2.0 available in the auto-updater yet.
You can reinstall HikaShop 2.2.0 over HikaShop 2.2.0, it will just update the files.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

Time to create page: 0.121 seconds
Powered by Kunena Forum