How to calculate and save the information in custom fields

  • Posts: 231
  • Thank you received: 28
  • Hikashop Business
8 months 1 week ago #359789

-- HikaShop version -- : 5.0.3
-- Joomla version -- : 4.4.2
-- PHP version -- : 8.1.27
-- Browser(s) name and version -- : Google chrome

Hi,
I want to add 2 new custom fields witch are visibles in back-end (order), and fromnt end (affiliate).
The main question is how can I get the value from "Subtotal" and "Coupon" and to do basic math like "subtotal - coupon" and to save the result in a custom field.
I need so save the value in a custom field because I want to show this value (sum of more custom fields) in Front-end -> Affiliate account, like the value that the affiliate will recive.
I need to do it like this because it's a custom way to do the math for what I need.

Attachments:

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
8 months 1 week ago #359791

Hi,

You need to develop a HikaShop plugin and implement in it the event onBeforeOrderCreate and onBeforeOrderUpdate:
www.hikashop.com/support/documentation/6...#onBeforeOrderCreate
In them, you want to have your PHP code to:
- potentially load the data of the order from the database if not already available in $order ( www.hikashop.com/support/documentation/6...umentation.html#code )
- substract the subtotal and coupon values. You'll want to do a var_dump() of the data to check that you have what you need for your calculations and how to access it. For example, when the order is being created, you'll have the value of the coupon in $order->order_discount_price and the whole data of the coupon in $order->cart->coupon, and you'll have the subtotal object in $order->cart->total ( $order->cart->total->price_value_with_tax or $order->cart->total->price_value based on whether you want the subtotal with or without taxes )
- set the result of your calculation in $order->xxx where xxx is the column name of your custom field. The save of the order afterwards will automatically save the value to the database for you.

The following user(s) said Thank You: oxido

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

Time to create page: 0.064 seconds
Powered by Kunena Forum