- Posts: 71
- Thank you received: 0
Get discount percentage for each product in order
-- Joomla version -- : 3.4.1
Hi,
I've developed a plugin that passes the orders made in Hikashop to our invoicing platform. This has been working fine now for several months. Now I'd like to be able to pass the discounts that have been attributed for specific products in the order. The code below shows how I create the product objects that are passed to our invoicing platform. Unfortunately I could not find any field in the hikashop api docu for the orde r object that would allow me to pass the original price of the product and the percentage of discount that must be applied.
Thanks for any info how to implement this!
Michel
Please Log in or Create an account to join the conversation.
That information is not stored in the order.
So either you load it from the product data (but the discount might not be in place anymore), or you store that extra information in the order when the order is created (in custom order or item fields). I would recommend the second option.
You can do that by implementing the onBeforeOrderCreate trigger and add the data you want in the fields you want based on the info you can get from the cart with a call to the loadFullCart function.
Please Log in or Create an account to join the conversation.
Thanks for the reply. I'm not quite sure which object to add custom fields to (see screenshot), so that it's available in the object returned by the loadfullcart function: would it be Produkt, Artikel, or Eintrag? Also do you have information about the object returned by the loadfullcart function?
Many thanks,
Michel
Please Log in or Create an account to join the conversation.
As I said, either custom order fields (which will go in $order and be stored in hikashop_order) or custom item fields (for each product which will go in $order->products and be stored in hikashop_order_product).
We don't have a list of the object returned by loadFullCart but you can var_dump it your self:
$class = hikashop_get('class.cart');
$cart = $class->loadFullCart();
var_dump($cart);
Please Log in or Create an account to join the conversation.
Thanks for the reply.
I have added 2 fields: flo_discount and flo_originalprice to the hikashop_order_product table (through custom fields interface). I have implemented the onBeforeOrderCreate function and added the code below.
However the values are not saved in the custom fields. I have also tried to update the corresponding fields in the order object that is passed into the onBeforeOrderCreate function, but also there, the fields are not updated.
What am I missing?
Thanks,
Michel
Please Log in or Create an account to join the conversation.
You need to set these attributes to the $order variable that is passed as a reference to the onBeforeOrderCreate function of your plugin and not in the $cart object.
Please Log in or Create an account to join the conversation.
Yes, that is what I tried first (see code below), but that didn't work either...
Thanks,
Michel
Please Log in or Create an account to join the conversation.
That code is wrong.
It should be like that:
Please Log in or Create an account to join the conversation.
Thanks for the code.
Unfortunately this does not work...
The code inside the inner loop:
is never executed. Although there are 2 products in the cart, there are no products in the $order variable (the output of the enquemessage call is 0).
Thanks for any suggestion.
Michel
Please Log in or Create an account to join the conversation.
- Posts: 12953
- Thank you received: 1778
In that case you should check what you have on the $cart->products variable to understand why the code inside the loop isn't executed.
Please Log in or Create an account to join the conversation.
Thanks for the reply.
I have done a dump of the $order variable that is passed in the onBeforeOrderCreate function.
As you can see, the custom fields ([flo_discount] and [flo_originalprice]) I have defined in item (hikashop_order_product table), appear only [shipping_groups] Array. Is this normal?
Thanks,
Michel
Please Log in or Create an account to join the conversation.
As you can see in the var_dump output, the products array is not in $order->products but in $order->cart->products
So change that in your code and it will go in the inner loop.
Please Log in or Create an account to join the conversation.
Ok, I checked again and my two custom fields are filled correctly. The problem seems to lie elsewhere, in the code below, where I try to get the information that wa stored in the custom fields. They are empty. Why is this?
Thanks,
Michel
Please Log in or Create an account to join the conversation.
These are custom item fields. They are already available in the $product object. So you don't want to load the product data from the product table. That would only be useful if your custom fields were of the table "product" (so that you could set them in the backend product edition interface).
Please Log in or Create an account to join the conversation.
That is not where the problem lies, because the data I load directly from the product table I need only to load additional info for the invoice that I have stored in another custom field on the product table.
In the onAfterOrderUpdate function I'm reading the values from the $fullorder variable ($fullorder = $orderClass->loadFullOrder($order->order_id, false, false)
Thanks for clearing this up,
Michel
Please Log in or Create an account to join the conversation.
Thanks,
Michel
Please Log in or Create an account to join the conversation.