order_discount_price not saved to db when using discount - only with coupons

  • Posts: 88
  • Thank you received: 19
6 years 11 months ago #283429

-- HikaShop version -- : 3.2.1
-- Joomla version -- : 3.8.2

Hi

I'm expanding a custom plugin that saves orders as an xml file using onAfterOrderUpdate. I would like to calculate the discount amount as a percentage of the order price. I can do that with coupon codes as order_discount_price is saved to the database. This is however not the case with discounts.Why this difference?

How can I access the discount information from my plugin?

Thanks

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 11 months ago #283437

Hi,

The coupons are applied globally to the cart/order.
The discounts are applied to the product prices directly. So in the order you see the product prices already with the discount applied.
Storing the discount amount in order_discount_price is not possible. Imagine if you have two discounts on two different products of the order.
Even if we were to store the data of the discounts with the order, they wouldn't be stored there. They would be stored in the hikashop_order_product table along with the products on which they apply.
The name of the columns in the database use "discount" because the coupons are actually also stored in the hikashop_discount table, but that's all.

Since the discount information is not stored with the orders for now, the best would be to implement the onAfterOrderCreate trigger in your plugin in order to get the data of the discounts in the products from the $order->cart->products array and store them yourself in some column of the hikashop_order_product table.
In fact that's something we should do in a future release of HikaShop.

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

  • Posts: 88
  • Thank you received: 19
6 years 11 months ago #283498

Got it working now, thanks! :-)

Only I had to use onBeforeOrderCreate instead of onAfterOrderCreate for saving into hikashop_order.
If I were to use hikashop_order_product, how do I create custom fields there?

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 11 months ago #283502

You can create custom fields of the table "item" via the menu Display>Custom fields.

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

  • Posts: 88
  • Thank you received: 19
6 years 11 months ago #283574

I know, but what table should I select for custom fields in hikashop_order_products table? Tried order and products, but none of those adds to hikashop_order_products table.

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

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 11 months ago #283576

Hi,

In the onBeforeOrderCreate function of your plugin, you can simply add the data of your custom item field to each product object in the array $order->cart->products using the column name of the custom field as attribute of the product object.
Note that the "frontend" display setting of the custom field needs to be activated or the field will be skipped by HikaShop.

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

Time to create page: 0.063 seconds
Powered by Kunena Forum