Update order status programatically and notify customer.

  • Posts: 76
  • Thank you received: 5
  • Hikamarket Multivendor Hikashop Business Hikashop Essential
2 years 8 months ago #339672

-- HikaShop version -- : 4.4.5
-- Joomla version -- : 3.10.5
-- PHP version -- : 7.4.25
-- Browser(s) name and version -- : Firefox Developer 98.0b4

Hi,

I created a CLI CRON script to send orders to the client ERP that is working fine for now.
My client wants now to mark all exported orders as shipped and notify customers.
I followed the solution on this thread : www.hikashop.com/forum/development/88659...fy-the-customer.html
It works fine in the web application but doesn't in a CLI script because the order class requires JApplication to trigger plugin events.
My terminal send me back this error message "Error: Failed to start application: Failed to start application"

Here is my code :

		if(!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
		if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')) return true;

		foreach ($ids as $id) // Order IDs
		{
			$orderClass = hikashop_get('class.order');
			$order = new stdClass();
			$order->order_id = (int)$id;
			$order->order_status = 'shipped';
			$order->history = new stdClass();
			$order->history->history_notified = 1;
			$order->history->history_reason = 'CRON update';
			$order->history->history_type = 'modification';
			$orderClass->save($order);
		}

Is there another way to do that so that I can use it in a CLI script ?

Thanks in forward for your help,

Manu @Agerix


Adishatz, erix
www.agerix.fr

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
2 years 8 months ago #339677

Hi,

I think the issue is that you need to define JApplication yourself in the factory and you didn't because Joomla doesn't do it for you in cli mode.
As per forum.joomla.org/viewtopic.php?t=981661 they recommend you have

JFactory::$application = $this;
in your code to initialize JApplication so that the code can then use JFactory::getApplication()

Last edit: 2 years 8 months ago by nicolas.
The following user(s) said Thank You: erix

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

  • Posts: 76
  • Thank you received: 5
  • Hikamarket Multivendor Hikashop Business Hikashop Essential
2 years 8 months ago #339695

Hi Nicolas,
First of all thanks for your quick answer.
Unfortunately the code provided by mbaker doesn't work but it pointed me to the right direction.
Here is a minimalistic example that WORK.
Hope it may help other users ;-)

<?php
const _JEXEC = 1;
if (file_exists(dirname(__DIR__) . '/defines.php')) {
	require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES')) {
	define('JPATH_BASE', dirname(__DIR__));
	require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';

$app = JFactory::getApplication('site');

class UpdateOrdersCli extends JApplicationCli
{
	public function doExecute()
	{
		if (!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
		if (!include_once(rtrim(JPATH_ADMINISTRATOR, DS) . DS . 'components' . DS . 'com_hikashop' . DS . 'helpers' . DS . 'helper.php')) return true;
		// See : https://www.hikashop.com/forum/development/886597-set-statut-programmatically-notify-the-customer.html
		$orderClass = hikashop_get('class.order');
		$order                            = new stdClass();
		$order->order_id                  = 4219;
		$order->order_status              = 'shipped';
		$order->history                   = new stdClass();
		$order->history->history_reason   = 'CRON update';
		$order->history->history_notified = 1;
		$order->history->history_type     = 'modification';
		$orderClass->save($order);
	}
}
JApplicationCli::getInstance('UpdateOrdersCli')->execute();

Thanks a lot,
Manu @Agerix


Adishatz, erix
www.agerix.fr
The following user(s) said Thank You: nicolas

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

Time to create page: 0.049 seconds
Powered by Kunena Forum