Jump to content

Order ID receiving as empty instead of number in custom module


Recommended Posts

Hi, I installed module which gives customer a option to choose shipping method (carrier or terminal). After fixing some issues module working and completes order with chosen terminal option, but:

Once order is attempting to be reviewed in back office, it throws an error that there is a DB syntax error:
 

An exception has been thrown during the rendering of a template ("You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LIMIT 1' at line 1<br /><br /><pre>SELECT id_terminal FROM ps_omniva_terminal_for_cart WHERE id_cart= LIMIT 1</pre>").

I am using hookDisplayOrderDetail function to render shipping information in order:

/**
	 * Hook which called when customer viewing his order details.
	 *
	 * @param $params
	 *
	 * @return string Smarty generated html
	 */
	public function hookDisplayOrderDetail($params)
	{
		$terminal = $this->getTerminalInfoByCartID($params['order']->id_cart);

		if ($terminal) {
			$this->smarty->assign('terminal', $terminal);
			return $this->fetch('module:omniva/views/templates/hook/order-details.tpl');
		}
	}

But in $params['order']->id_cart it returns empty value and SQL query can not be completed. I also tried to replace this option with $params['cart']->id, but it shows the same result in getTerminalInfoByCartID function:

/**
	 * Return terminal's array by order's cart id.
	 *
	 * @param $cart_id Order's cart id
	 *
	 * @return array|null
	 */
	public function getTerminalInfoByCartID($cart_id) {
		$sql = 'SELECT id_terminal FROM ' . _DB_PREFIX_ . 'omniva_terminal_for_cart WHERE id_cart=' . $cart_id;
		//$sql = 'SELECT id_terminal FROM ' . _DB_PREFIX_ . 'omniva_terminal_for_cart WHERE id_cart=6';
		if ($row = Db::getInstance()->getRow($sql)) {
			$terminal_id = $row['id_terminal'];
			if ($terminal = $this->getTerminalInfoByTerminalID($terminal_id)) {
				return $terminal;
			}
		}
		return null;
	}

Maybe someone could guide me where mistake is made and how to solve it, as $param works on order confirmation hook.

 

Any insight would be appreciated.

Link to comment
Share on other sites

	/**
	 * Hook which called when customer viewing his order details.
	 *
	 * @param array{cookie: Cookie, cart: Cart, altern: int, order: Order} $params
	 *
	 * @return string Smarty generated html
	 */
	public function hookDisplayOrderDetail(array $params)
	{
		/** @var Order $order */
		$order = $params['order'];

		if (!Validate::isLoadedObject($order)) {
			return '';
		}

		$terminal = $this->getTerminalInfoByCartID($order->id_cart);

		if ($terminal) {
			$this->smarty->assign('terminal', $terminal);
			return $this->fetch('module:omniva/views/templates/hook/order-details.tpl');
		}
	}

Warning : you must cast value in SQL Query to avoid SQL injection ! Always ensure id_cart is an integer !

	/**
	 * Return terminal's array by order's cart id.
	 *
	 * @param $cart_id Order's cart id
	 *
	 * @return array|null
	 */
	public function getTerminalInfoByCartID($cart_id) {
		$sql = 'SELECT id_terminal FROM ' . _DB_PREFIX_ . 'omniva_terminal_for_cart WHERE id_cart=' . (int) $cart_id;
		//$sql = 'SELECT id_terminal FROM ' . _DB_PREFIX_ . 'omniva_terminal_for_cart WHERE id_cart=6';
		if ($row = Db::getInstance()->getRow($sql)) {
			$terminal_id = $row['id_terminal'];
			if ($terminal = $this->getTerminalInfoByTerminalID($terminal_id)) {
				return $terminal;
			}
		}
		return null;
	}

 

  • Like 1
Link to comment
Share on other sites

Your corrections solved my issue, thanks!

However, right now I'm facing another issue:

In Back Office, once i reviewing order details, it does not display .tpl file.

I'm trying to display tpl via hookDisplayAdminOrder:

/**
	 * Hook which called in admin order's page.
	 * Showing which terminal is chosen by customer.
	 *
	 * Possible other position for this block: displayAdminOrderContentShip but then also needs displayAdminOrderTabShip
	 *
	 * @param array{cookie: Cookie, cart: Cart, altern: int, order: Order} $params
	 *
	 * @return string HTML
	 */
	public function hookDisplayAdminOrder(array $params)
	{
		/** @var Cart $order */
		$order = $params['cart'];

		if (!Validate::isLoadedObject($order)) {
			return 'There was an issue loading shipping details in admin side';
		}

		$terminal = $this->getTerminalInfoByCartID($order->id);

		if ($terminal) {
			$this->smarty->assign('terminal', $terminal);
			return $this->display(__FILE__,'views/templates/hook/admin-order.tpl');
		}
	}

And once module is installed, at the bottom displays message There was an issue loading shipping details in admin side.

Does anyone know where I making a mistake in order to display module tpl file?

Thank you for your insights. 

Link to comment
Share on other sites

displayAdminOrder

Display new elements in the Back Office, tab AdminOrder This hook launches modules when the AdminOrder tab is displayed in the Back Office

Located in:

- admin-dev/themes/default/template/controllers/orders/helpers/view/view.tpl

- src/PrestaShopBundle/Resources/views/Admin/Sell/Order/Order/view.html.twig

Parameters:

array( 'id_order' = (int) Order ID )

public function hookDisplayAdminOrder($params)
{
    $id_order = $params['id_order'];
    $id_cart = Order::getCartIdStatic((int)$id_order);
    ....
    ....
}

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...