Skip to main content

Get discount percentage for each product in order

More
11 years 2 weeks ago #213936 by bmichu
-- HikaShop version -- : 2.5.0
-- 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
Code:
foreach ($fullorder->products as $product) { /* * Creating position object using array object class */ //jdbg::p($product); //$app->enqueueMessage('product'); $db->setQuery('SELECT product_invoice_description FROM #__hikashop_product WHERE product_id =' . (int) $product->product_id); $dbproduct = $db->loadObject(); $objPosition = new ArrayObject(array( 'type' => 2, 'number' => $product->order_product_code, 'name' => $product->order_product_name, 'description' => $dbproduct->product_invoice_description, 'cost' => $product->order_product_price, 'unit' => 7, 'amount' => $product->order_product_quantity, 'vat' => 0, 'discount' => 0 )); $objPositions[$cnt] = $objPosition; $cnt++; }

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

More
11 years 2 weeks ago #213940 by nicolas
Hi,

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.

More
11 years 2 weeks ago #213993 by bmichu
Hi Nicolas,

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.

More
11 years 2 weeks ago #213997 by nicolas
Hi,

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.

More
11 years 2 weeks ago #214059 by bmichu
Hi nicolas,

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
Code:
$class = hikashop_get('class.cart'); $cart = $class->loadFullCart(); $price =0; foreach ($cart->products as $product) { if (property_exists($product->prices[0], 'price_value_without_discount')) { $price = $product->prices[0]->price_value_without_discount; } else { $price = $product->prices[0]->price_value; } $cart->flo_discount = $product->discount->discount_percent_amount; $cart->flo_originalprice = $price; }

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

More
11 years 2 weeks ago #214075 by nicolas
Hi,

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.

More
11 years 2 weeks ago #214087 by bmichu
Hi Nicolas,

Yes, that is what I tried first (see code below), but that didn't work either...

Thanks,
Michel
Code:
$app = JFactory::getApplication(); $class = hikashop_get('class.cart'); $cart = $class->loadFullCart(); foreach ($cart->products as $product) { foreach ($order->products as $order_product) { if(strcmp($order_product->product_code, $product->product_code) ==0) { $price=0; if (property_exists($product->prices[0], 'price_value_without_discount')) { $price = $product->prices[0]->price_value_without_discount; } else { $price = $product->prices[0]->price_value; } $order_product->flo_discount = $product->discount->discount_percent_amount; $order_product->flo_originalprice = $price; } } }

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

More
11 years 2 weeks ago #214098 by nicolas
Hi,

That code is wrong.
It should be like that:
Code:
$app = JFactory::getApplication(); $class = hikashop_get('class.cart'); $cart = $class->loadFullCart(); foreach ($cart->products as $product) { foreach ($order->products as $k => $order_product) { if(strcmp($order_product->product_code, $product->product_code) ==0) { $price=0; if (property_exists($product->prices[0], 'price_value_without_discount')) { $price = $product->prices[0]->price_value_without_discount; } else { $price = $product->prices[0]->price_value; } $order->products[$k]->flo_discount = $product->discount->discount_percent_amount; $order->products[$k]->flo_originalprice = $price; } } }

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

More
11 years 1 week ago - 11 years 1 week ago #214213 by bmichu
Hi Nicolas,

Thanks for the code.
Unfortunately this does not work...
The code inside the inner loop:
Code:
foreach ($cart->products as $product) { $app->enqueueMessage('count order products:' . count($order->products)); foreach ($order->products as $k => $order_product) { ...... } }

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
Last edit: 11 years 1 week ago by bmichu.

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

More
11 years 1 week ago #214243 by Mohamed Thelji
Hello Michel,
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.

More
11 years 1 week ago #214385 by bmichu
Hi Mohamed,

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.

More
11 years 1 week ago #214520 by nicolas
Hi,

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.

More
11 years 1 week ago - 11 years 1 week ago #214529 by bmichu
Hi,

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?
Code:
function onAfterOrderUpdate(&$order,&$send_email) { try { if(!@include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')){ return false; } if($order->order_status =='confirmed') { ..... $orderClass = hikashop_get('class.order'); $fullorder = $orderClass->loadFullOrder($order->order_id, false, false); foreach ($fullorder->products as $product) { /* * Creating position object using array object class */ //jdbg::p($product); $db->setQuery('SELECT product_invoice_description FROM #__hikashop_product WHERE product_id =' . (int) $product->product_id); $dbproduct = $db->loadObject(); $objPosition = new ArrayObject(array( 'type' => 2, 'number' => $product->order_product_code, 'name' => $product->order_product_name, 'description' => $dbproduct->product_invoice_description, 'cost' => $product->flo_originalprice, 'unit' => 7, 'amount' => $product->order_product_quantity, 'vat' => 0, 'discount' => $product->flo_discount )); $objPositions[$cnt] = $objPosition; $cnt++; } .......

Thanks,
Michel
Last edit: 11 years 1 week ago by bmichu.

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

More
11 years 1 week ago #214533 by nicolas
Hi,

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.

More
11 years 1 week ago #214583 by bmichu
Hi,

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);) and the custom fields from the order_product table (flo_discount and flo_originalprice) which I assigned values to in the onBeforeOrderCreate function (see order dump attached), are empty in the object returned by loadFullOrder (see fullorder dump attached).

Thanks for clearing this up,
Michel

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

More
11 years 20 hours ago #215545 by bmichu
Any feedback on this?

Thanks,
Michel

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

Time to create page: 0.286 seconds
Powered by Kunena Forum