add data to a table after order is confirmed

  • Posts: 20
  • Thank you received: 0
13 years 7 months ago #14389

I'd like to be able to add some data to fields in 1 table in my database whenever a payment is verified and order becomes confirmed. where can I do that?

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

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
13 years 7 months ago #14395

You will need to create a plugin and implement the function onAfterOrderUpdate(&$order,&$send_email) of our API:
www.hikashop.com/en/support/documentatio...r-documentation.html

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

  • Posts: 20
  • Thank you received: 0
13 years 7 months ago #14413

Hi nicolas,

I attempted to create a plugin for that so when order is updated, a coupon is created. This is my first time and it doesn't work, would you be able to tell me what I did wrong? thank you!

I placed it in joomla/plugins/hikashop/
and already registered it in the jos_plugin.

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

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
13 years 7 months ago #14416

Is it published ?
Do you see it in the joomla plugin manager ?
Could you post an archive of the files ?

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

  • Posts: 20
  • Thank you received: 0
13 years 7 months ago #14420

sorry i thought i attached the file. here you go.

yes it is published, i can see it in the plugin manager as well.

File Attachment:

File Name: create_coupon.zip
File Size:1 KB

Attachments:

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

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
13 years 7 months ago #14428

1. There is no xml file for your plugin in that archive. Without a properly defined xml file, the plugin won't work.
2. You wrote $order_>order_full_price instead $order->order_full_price
3. The discount_id is generated automatically when saving a new coupon you should not specify it.
4. The discount_code should be unique for each coupon. So you need to make it dynamic, otherwise, if you already have a coupon with the same name it won't work.
5. You said that you wanted your code to run when the order was confirmed. As you wrote it, it will be triggered each time an order is modified. You should check the order_status attribute of the order.

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

  • Posts: 20
  • Thank you received: 0
13 years 7 months ago #14446

Hi nicolas,

the reason y I only use static value such as 'testcode' for discount_code is because I wanted to see it working first before I put in any other things to complicate it. Also 'testcode' has never been created in discount before.
However, I've made the following changes, and the plugin still wouldn't create a discount entry for me. also attached php+xml files.

1. I've now included the xml file
2. I also fixed the $order_>order_full_price to $order->order_full_price
3. I've removed the discount_id line.
4. I use order_id + today's date&time as discount_code, so it's always unique
5. I've added the condition to check the order_status == 'confirmed'

I also tried with static values, still doesn't work. The plugin is called each time i change the order status (because if i have a syntax error in create_discount.php the order status will not change), but it never creates the discount entry.

File Attachment:

File Name: create_cou...0416.zip
File Size:1 KB

Attachments:

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

  • Posts: 20
  • Thank you received: 0
13 years 7 months ago #14447

sorry uploaded the wrong file.

File Attachment:

File Name: create_cou...16-2.zip
File Size:1 KB

Attachments:

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

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
13 years 7 months ago #14450

Hi,

In your xml, and in the files name, you call your plugin create_coupon, so the name of the class should be plgHikashopCreate_coupon
That's probably why your plugin is not triggered. Joomla loads the file (so that's why you get the errors) but since the class name is not good, it's not able to register itself to the event and thus it is not triggered.

Instead of hikashop::getTime(getdate()) you can just do time()

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

  • Posts: 20
  • Thank you received: 0
13 years 7 months ago #14455

Ahhh!!! you are right! it adds the entry now!
But there's one problem, it's not assigning the order_full_price to the discount amount. is there something wrong with the following line:

$discount->discount_flat_amount = $order->order_full_price;

in my order table, the order_full_price field is 60.00, so i expect the discount amount to be 60.00 as well. is it wrong?

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

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
13 years 7 months ago #14457

That's normal. You need to first load the order data.
When the function onAfterOrderUpdate is triggered, you don't know what is in $order. You can expect that there is order_id but that's all. Somewhere is the code you could just do like that:
$order = null;
$order->order_id = 5;
$order->order_status = "confirmed";
$orderClass = hikashop::get('class.order');
$orderClass->save($order);

So in your code, you will only get the id and the status.

So you need to load the order data before using that data:
if(empty($order->order_full_price)){
$orderClass = hikashop::get('class.order');
$dbOrder = $orderClass->get($order->order_id);
$order->order_full_price = $dbOrder->order_full_price;
}

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

  • Posts: 20
  • Thank you received: 0
13 years 7 months ago #14458

brilliant! Thank you Nicolas! the best support on earth!

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

  • Posts: 10
  • Thank you received: 0
12 years 11 months ago #31515

Bonjour,

I need to perform same task : execute code after order is confirm.
I created a plugin :
defined('_JEXEC') or die('Restricted access');
?>
<?php
class plgHikashopBuy_AUP extends JPlugin
{
function onAfterOrderUpdate(&$order,&$send_email){
if(!empty($order->order_id)){
if($order->order_status == 'confirmed'){
//insert code here

Payment is not implemented in my website, so I have to test it manually.
This is working properly when a user creates an order. Order is with status created, if I switch status to confirm, plugin is triggered.

I have added bank transfer payment (easy to test with this one I guess).
And I have modified this payment processor (in the properties of the plugin) to have status Confirmed (instead of Created).
So when a user buys something with this payment processor, the order is generated with status Confirmed.

But here the plugin is not triggered.

Could you please explain why ?

Many thanks.

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

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
12 years 11 months ago #31545

Bonjour,

That's because you implemented the function onAfterOrderUpdate which is only called when the order is updated. When the order is created, it's the function onAfterOrderCreate which is called and since you didn't implement it nothing happens.

For example, you can look at the hikashop history plugin where both functions are implemented since the plugin needs to be triggered in both cases.

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

  • Posts: 10
  • Thank you received: 0
12 years 11 months ago #31549

Do you think I am so stupid to have forgotten function onAfterOrderCreate ?
hum.... I AM so stupid !!

So I added :
function onAfterOrderCreate(&$order,&$send_email){
return $this->onAfterOrderUpdate($order,$send_email);
}

which is what you mean (correct ?).

But still no way to trigger the code when bank transfer (and Confirmed is the default status).

btw, I've noticed when I create an order like said before, the history of the order shows me 2 entry : one with status Created, and a second one with status Confirmed.
So I guess the function onAfterOrderUpdate should be enough (anyway, I've implemented your suggestion, which makes sense to me).
Maybe what is wrong here is something else ?

(anyway, many thanks for your help)

Last edit: 12 years 11 months ago by ursule15.

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

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
12 years 11 months ago #31550

That's indeed what I meant.

You should put a trace directly at the beginning of your function onAfterOrderUpdate(&$order,&$send_email) like that:
echo "test"; exit;

that way, you will see the "test" message if the plugin is triggered. If it is not triggered, then the only option is that the plugin is not published which should not be the case since you say that it works with other tests.

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

  • Posts: 10
  • Thank you received: 0
12 years 11 months ago #31567

I am using jdump to check.
And I confirm, when creating an order with confirmed status as default status on bank transfer, onAfterOrderCreate is triggered once, onAfterOrderUpdate is triggered twice.

But my sql DB is not updated this way, even if th code is working well when updating manually.
Something is wrong in my code, not same variable I guess when updating manually.

I will check.
Many thanks for your help.




edit :

OK, I can see why it is not working.
in the code, I need the userid.
I am using the following :
$orderClass = hikashop::get('class.order');
$dbOrder = $orderClass->get($order->order_id);
$orderClass=$orderClass->loadFullOrder($order->order_id, false, false);

Then $orderClass->customer->id is the userid.

This code works when manually changing the status.
But it does not work when the order is created the proper way (by the user, frontend side, bank transfer payment).

There is no customer property for $orderClass.

What do I miss here ?

Last edit: 12 years 11 months ago by ursule15.

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

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
12 years 11 months ago #31586

The customer is indeed only loaded in $orderClass when on the back end.

You should add these lines:
$userClass = hikashop_get('class.user');
$orderClass->customer = $userClass->get($orderClass->order_user_id);

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

  • Posts: 10
  • Thank you received: 0
12 years 11 months ago #31604

Hello,

I tried your piece of code (just modified hikashop_get to hikasho::get).
But $userClass->get($orderClass->order_user_id) return null (even if $userClass seems to be correctly created before).

I think I need to learn a bit more about php before trying to understand all of this (totally noob regarding this language).

Last edit: 12 years 11 months ago by ursule15.

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

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
12 years 11 months ago #31616

If that code returns null it means that either $orderClass->order_user_id does not contain the user id or that the user has been deleted in HikaShop.
So you should look at that.
hikashop_get won't work in old versions of HikaShop and in that case you should use hikashop::get instead.

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

Time to create page: 0.096 seconds
Powered by Kunena Forum