Roundoff the total amount ofter delivery charge

  • Posts: 3
  • Thank you received: 0
1 year 8 months ago #349541

-- url of the page with the problem -- : odc.com.my
-- Joomla version -- : 3

Dear hikashop,
How should I do if wanna take the total (rounded off price) to add the delivery charge instead of the subtotal.

Attachments:

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
1 year 8 months ago #349544

Hi,

I'm not sure I understand what you want.
Maybe you want to activate the "round prices during calculations" so that the subtotal and total are the same when there is no shipping/coupon/payment ?

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

  • Posts: 3
  • Thank you received: 0
1 year 8 months ago #349547

Dear Hikashop,
In site, the order's total will be rounded off automatically as full order price. However, if admin edits order in backend, all order products will be recalculated in fucntion recalculateFullPrice(&$order, $products = null), then the $order->order_full_price will not be rounded with 1 decimal, example RM 100.16 shoud be RM 100.20. Can I modify the code in the recalculateFullPrice function by adding ($order->order_full_price = round($order->order_full_price,1);)?

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
1 year 8 months ago #349558

Hi,

Well, you can but that's not a long term solution as you would need to do the change after each update.
It would be better for us to properly understand the situation, in order to add an appropriate patch so that no modification is necessary on your end.

How is the rounding to 1 decimal done on your frontend ?
Is this something you configured in your currency settings ?
Could you provide a screenshot of that ?
What is the status of the setting "round prices during calculations" ?
What is the price of each product and its quantity in the order ?

I've looked into this recalculateFullPrice function's code and I think such code should do the job:

public function recalculateFullPrice(&$order, $products = null) {
		//load all the prices and recalculate total
		if(empty($products)) {
			$query = 'SELECT * FROM '.hikashop_table('order_product').' WHERE order_id = ' . (int)$order->order_id;
			$this->database->setQuery($query);
			$products = $this->database->loadObjectList();
		}
		$total = 0.0;
		$taxes = array();
		$bases = array();
		JPluginHelper::importPlugin('hikashop');
		$app = JFactory::getApplication();
		
		if(empty($order->old) && !empty($order->order_id)) {
			$order->old = $this->get($order->order_id);
		}
		$old = @$order->old;
		
		$rounding = 2;
		$currencyClass = hikashop_get('class.currency');
		if(!empty($old->order_currency_id)) {
			$rounding = $currencyClass->getRounding($old->order_currency_id, true);
		}

		foreach($products as $i => $product) {
			if($product->order_product_code != 'order additional') {
				$app->triggerEvent( 'onBeforeCalculateProductPriceForQuantityInOrder', array( &$products[$i]) );
				if(function_exists('hikashop_product_price_for_quantity_in_order')) {
					hikashop_product_price_for_quantity_in_order($product);
				} else {
					$product->order_product_total_price = ((float)$product->order_product_price + (float)$product->order_product_tax) * (int)$product->order_product_quantity;
				}
				$app->triggerEvent('onAfterCalculateProductPriceForQuantityInOrder', array( &$products[$i]) );
			} else {
				$product->order_product_total_price = ((float)$product->order_product_price + (float)$product->order_product_tax);
			}

			$total += $currencyClass->round($product->order_product_total_price, $rounding);

			// Recalculate taxes for that product line
			if(!empty($product->order_product_tax_info)) {
				if(is_string($product->order_product_tax_info))
					$product_taxes = hikashop_unserialize($product->order_product_tax_info);
				else
					$product_taxes = $product->order_product_tax_info;

				foreach($product_taxes as $tax) {
					if(!isset($taxes[$tax->tax_namekey])) {
						$taxes[$tax->tax_namekey] = 0;
						$bases[$tax->tax_namekey] = 0;
					}
					if($product->order_product_code == 'order additional') {
						$taxes[$tax->tax_namekey] += (float)@$tax->tax_amount;
						$bases[$tax->tax_namekey] += (float)@$tax->amount;
					} else {
						$taxes[$tax->tax_namekey] += (float)@$tax->tax_amount * $product->order_product_quantity;
						$bases[$tax->tax_namekey] += (float)@$tax->amount * $product->order_product_quantity;
					}
				}
			}
		}


		if(!isset($order->order_discount_price))
			$order->order_discount_price = @$old->order_discount_price;

		if(!isset($order->order_shipping_price))
			$order->order_shipping_price = @$old->order_shipping_price;

		if(!isset($order->order_payment_price))
			$order->order_payment_price = @$old->order_payment_price;

		$additionals = 0 - $currencyClass->round((float)$order->order_discount_price, $round) + $currencyClass->round((float)$order->order_shipping_price, $round) + $currencyClass->round((float)$order->order_payment_price, $round);

		$order->order_full_price = $total + $additionals;

		if($order->order_full_price < 0 && $total > 0)
			$order->order_full_price = 0;

		//recalculate total taxes
		$config =& hikashop_config();
		if(!isset($order->order_tax_info) || empty($order->order_tax_info)) {
			if(!empty($old->order_tax_info)) {
				$order->order_tax_info = $old->order_tax_info;
			} elseif($config->get('detailed_tax_display', 1)) {
				$order->order_tax_info = array();
			}
		}

		if(!empty($order->order_tax_info) || $config->get('detailed_tax_display', 1)) {
			if(is_string($order->order_tax_info))
				$order->order_tax_info = hikashop_unserialize($order->order_tax_info);

			if(count($order->order_tax_info)){
				foreach($order->order_tax_info as $k => $tax) {
					$order->order_tax_info[$k]->todo = true;
				}
			}
			if(!empty($taxes)) {
				foreach($taxes as $namekey => $amount) {
					$found = false;
					foreach($order->order_tax_info as $k => $tax) {
						if($tax->tax_namekey == $namekey) {
							$tax_additionals = @$tax->tax_amount_for_shipping + @$tax->tax_amount_for_payment - @$tax->tax_amount_for_coupon;
							$gross_additionals = $tax_additionals - $order->order_discount_price + $order->order_shipping_price + $order->order_payment_price;
							$order->order_tax_info[$k]->tax_amount = $amount + $tax_additionals;
							$order->order_tax_info[$k]->amount = $bases[$namekey] + $gross_additionals;
							if($order->order_full_price == 0) {
								$order->order_tax_info[$k]->tax_amount = 0;
								$order->order_tax_info[$k]->amount = 0;
							}
							unset($order->order_tax_info[$k]->todo);
							$found = true;
							break;
						}
					}
					if(!$found) {
						$obj = new stdClass();
						$obj->tax_namekey = $namekey;
						$obj->tax_amount = $amount;
						$obj->amount = $bases[$namekey];

						if($order->order_full_price == 0) {
							$obj->tax_amount = 0;
							$obj->amount = 0;
						}
						$order->order_tax_info[$namekey] = $obj;
					}
				}
			}

			$unset = array();
			foreach($order->order_tax_info as $k => $tax) {
				if(isset($tax->todo)) {
					$tax_additionals = @$tax->tax_amount_for_shipping + @$tax->tax_amount_for_payment - @$tax->tax_amount_for_coupon;
					$gross_additionals = $tax_additionals - $order->order_discount_price + $order->order_shipping_price + $order->order_payment_price;
					$order->order_tax_info[$k]->tax_amount = $tax_additionals;
					$order->order_tax_info[$k]->amount = $gross_additionals;
					if($order->order_full_price == 0) {
						$order->order_tax_info[$k]->tax_amount = 0;
						$order->order_tax_info[$k]->amount = 0;
					}
					if(!bccomp(sprintf('%F',$order->order_tax_info[$k]->tax_amount),0,5)) {
						$unset[]=$k;
					} else {
						unset($order->order_tax_info[$k]->todo);
					}
				}
			}
			if(!empty($unset)) {
				foreach($unset as $u) {
					unset($order->order_tax_info[$u]);
				}
			}
		}
	}
So you could copy/paste it instead of the current code and see if that helps. But I can't say it will work for sure as I'm not able to reproduce the problem on my end as I don't have yet the necessary information for that.

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

Time to create page: 0.065 seconds
Powered by Kunena Forum