I'm working on a PrestaShop 8.0.1 installation, and I'm trying to automatically update the order status to "Shipped" when a tracking number is added. I've created a custom module named "UpdateShippedStatus" to achieve this functionality.
Here's what I've done so far:
Created Custom Module: I've created a custom module named "UpdateShippedStatus" and overridden the updateShippingAction function in the OrderController to add the logic for updating the order status.
Code Snippet: Here's the code snippet for my custom controller (CustomOrderController.php):
<?php
use PrestaShopBundle\Controller\Admin\Sell\Order\OrderController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
class CustomOrderController extends OrderController
{
public function updateShippingAction(int $orderId, Request $request): RedirectResponse
{
// Call the parent method to execute the original logic
$response = parent::updateShippingAction($orderId, $request);
// Update the order status to "shipped"
$this->updateOrderStatus($orderId, 4); // Assuming "shipped" status option value is 4
return $response;
}
// Function to update order status
private function updateOrderStatus(int $orderId, int $statusId): void
{
// Get the Order object and update its status
$order = $this->getDoctrine()->getRepository(Order::class)->find($orderId);
if ($order) {
$order->setCurrentState($statusId);
$this->getDoctrine()->getManager()->flush();
}
}
}
Module Registration: I've registered my custom controller in the module's main file (updateshippedstatus.php). Here's the code snippet:
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class UpdateShippedStatus extends Module
{
public function __construct()
{
$this->name = 'updateshippedstatus';
$this->tab = 'shipping_logistics';
$this->version = '1.0.0';
$this->author = 'Your Name';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Update Shipped Status');
$this->description = $this->l('Custom module to update order status when shipping is updated.');
// Register custom controller
$this->controllers = array('customordercontroller');
// Ensure that the controller file exists
if (file_exists($this->getLocalPath().'controllers/front/CustomOrderController.php')) {
require_once($this->getLocalPath().'controllers/front/CustomOrderController.php');
}
}
public function install()
{
return parent::install() &&
$this->registerHook('ActionAdminControllerSetMedia');
}
}
Despite these efforts, my custom module doesn't seem to trigger when adding a tracking number. I've verified that the module is installed and enabled, but the order status isn't updating as expected.
Any suggestions on what might be causing this issue or how to troubleshoot it further would be greatly appreciated.
Look at this dedicated hook you can include in your module that will be triggered on tracking number update:
actionAdminOrdersTrackingNumberUpdate
You can execute your code there.