Updating Order Details Not Accurate

  • Posts: 56
  • Thank you received: 1
1 year 3 weeks ago #350599

Using a sales component, which the new record submitted, will append a hikashop order using ($orderClass = hikashop_get('class.order'); $fullOrder = $orderClass->loadFullOrder((int)$orderId, true, false); $orderClass->save($fullOrder);). It is working fine when adding new record, but comes to edit the existing component record, the order details are updated correctly except the order_full_price. The field order_full_price not able to updated. Even there is value when running the save function. Any suggestion to solve this?

The code for update hikashop order details when editing record is as follows:

if (!empty($array['custom_field5'])) { //if is update record
						$orderId = $array['custom_field5'];
						$orderClass = hikashop_get('class.order');
						$fullOrder = $orderClass->loadFullOrder((int)$orderId, true, false);

						//$orderObj = new stdClass();
						$fullOrder->order_billing_address_id = $array['customer'];
						$fullOrder->order_shipping_address_id = $array['customer'];
						$fullOrder->order_user_id = $customerID;
						$fullOrder->order_status = 'confirmed';
						$fullOrder->order_type = 'sale';
						
						//update full price
						//$orderObj->order_full_price = $array['total_value'];
						$fullOrder->receipt = $receiptFile;
						$fullOrder->branch = $array['record_branch'];
						$fullOrder->source = $array['record_source'];

						foreach ($fullOrder->products as $product) {
							$class = hikashop_get('class.order');
							JRequest::setVar('order_id', $fullOrder->order_id);
							JRequest::setVar('order_product_id', $product->order_product_id);
							$class->saveForm('product_delete');
						}

						$productarray = array();

						if (isset($array['product']) && $array['product'] != "") {
							// calculate the order full price
							$finalPrice = 0;
							$discount = 0;

							foreach ($array['product'] as $key => $value) {
								$data = Dt_salesHelper::ProductDetail($value["productsname"]);

								$product = new stdClass();
								
								$write['data'] = $data;

								//check if shipping item
								$shipping_id = $data->shipping_id;
								if (!empty($shipping_id)) {
									$write['shipping_id'] = $shipping_id;
									$db = JFactory::getDbo();
									$query = $db->getQuery(true);
									$query
										->SELECT('shipping_name,shipping_params')
										->FROM($db->quoteName('#__hikashop_shipping'))
										->where($db->quoteName('shipping_id') . ' = ' . $db->quote($shipping_id));
									$db->setQuery($query);
									$row = $db->loadAssoc();
									$shipping_price = $value['unitprice'] *  $value["quantity"];
									$shipping_name = $row['shipping_name'];
									$shipping_params = $row['shipping_params'];

									$order_full_price += $shipping_price; // add shipping cost if applicable
								} 
								//check if discount item
								else if ($data->product_id == '9'){
									$discount = $value['unitprice'] *  $value["quantity"];
									//$total_discount = preg_replace("/[^a-zA-Z0-9]+/", "", $total_discount);

									$write['discount'] = gettype($discount);

								}
								//normal product add
								else{
									$product->product_id = $value["productsname"];
									$product->order_product_price = $value['unitprice'];
									$product->order_product_quantity = $value["quantity"];
									$product->order_product_name = $data->product_name;
									$product->order_product_code = $data->product_code;

									$order_full_price += $product->order_product_price * $product->order_product_quantity;

									$productarray[] = $product;
								}
							}

							$fullPrice = $array['total_value'];
							$finalPrice = $fullPrice - $discount;

							$fullOrder->order_full_price = $order_full_price; // convert to double
							$fullOrder->order_discount_price = abs($discount); // remove negative sign from discount

						}

						$write['finalPrice'] = gettype($finalPrice);
						$write['finalPriceq'] = $finalPrice;
						$write['fullPrice'] = $fullPrice;
						$write['order_full_price'] = $fullOrder->order_full_price;
						$write['total_discount'] = abs($discount);

						if (empty($shipping_id)) {
							$fullOrder->order_shipping_id = ''; 
							$fullOrder->order_shipping_method = '';
							$fullOrder->order_shipping_price = '';
							$fullOrder->order_shipping_params = '';
						}else{
							$fullOrder->order_shipping_id = $shipping_id; 
							$fullOrder->order_shipping_method = $shipping_name;
							$fullOrder->order_shipping_price = $shipping_price; 
							$fullOrder->order_shipping_params = $shipping_params; 
						}
						
						/*if (empty($total_discount)) {
							$fullOrder->order_discount_price = '';
						}else{
							$fullOrder->order_discount_price = $total_discount; 
						}*/

						$fullOrder->cart->products = $productarray;

						$fullOrder->order_id = $fullOrder->order_id;
						$fullOrder->history = new stdClass();
						$fullOrder->history->history_notified = 1;

						unset($fullOrder->order_subtotal);
						unset($fullOrder->order_subtotal_no_vat);

						$orderClass->save($fullOrder);

						$write['saved'] = $fullOrder;

						$myfile = fopen("aUp.txt", "w") or die("Unable to open file!");
						fwrite($myfile,print_r($write,true));
						fclose($myfile);

					}

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
1 year 3 weeks ago #350611

Hi,

The save function of class.order was not meant to be used for a complete order update like this.
The $fullOrder->cart->products array is processed after the order data is saved. That's because it's meant to be used when a new order is created, and thus is needs to add the order_id in the order_product table when adding the entries, but the order_id is not known before the order data is saved to the database.
Now, when you call the save function on an existing order, the system is looking at whether you're providing things which could change the total price:
- a payment fee
- a shipping fee
- a coupon value
- a product change (in $order->product)
And in that case, it will recalculate the total amount based on these and the list of products already in the database before saving the order data.
But since it then process the products in $fullOrder->cart->products after the order data save, it doesn't take the new products data into account in your case.

Ideally, you would use the saveForm function to save all the new order data, like you did for the product deletion.
But you would have to rewrite all the code.
A simpler solution would be to have it recalculate the total after the save call, like this:

$recalculateOrder = new stdClass();
$recalculateOrder->order_id = $fullOrder->order_id;
$orderClass->recalculateFullPrice($recalculateOrder);
$orderClass->save($recalculateOrder);

The following user(s) said Thank You: jiawen

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

  • Posts: 56
  • Thank you received: 1
1 year 3 weeks ago #350619

Thank you. It works. Another issue is would like to add customer details for the order. Is it can use the $orderClass->save($fullOrder); function do this?

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
1 year 3 weeks ago #350620

Hi,

What user details do you want to add exactly ?
In the order, you only have the order_user_id which links the order to a user.
So if you want to add data in custom fields of the table "user", it's the save of class.user you want to use instead.

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

  • Posts: 56
  • Thank you received: 1
1 year 3 weeks ago #350634

Want to insert customer data for this part.

Attachments:

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
1 year 3 weeks ago #350637

Hi,

Well, there are several steps in that case:
- first, do you already have an entry for your data in the hikashop_user table ? If you do, you just have to fill the order_user_id of the order in the hikashop_order table. This can be done with the save of class.order
- if you don't have an entry for your data in the hikashop_user table, you need to create one with the save of class.user. You'll need to provide the user_email and potentially the user_cms_id provided you want to link it to a Joomla user account. In that case the user_cms_id must be the id of the user account in the users table of Joomla. If you don't provide the user_cms_id, the user entry in HikaShop will be as a guest user.
- And so if you want to have a user account, you first need to use the Joomla API to add the user account in Joomla.
It's an old page on this:
stackoverflow.com/questions/1904809/how-...from-within-a-script
A more recent example would be in the "register" method of class.user in HikaShop but it's a lot of code as it's doing a lot more stuff than necessary for you.

The following user(s) said Thank You: jiawen

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

Time to create page: 0.072 seconds
Powered by Kunena Forum