📦 Estados de pedido — referencia y transiciones
Actualizado: 2024-12-01
Los estados de pedido en PrestaShop controlan el flujo del ciclo de vida de un pedido. Cada modulo de pago debe asignar el estado correcto segun el resultado del pago. Los estados se manejan a traves de la clase OrderHistory.
#Estados predefinidos por ID
| ID | Constante | Descripcion |
|---|---|---|
| 1 | PS_OS_CHEQUE | Pago aceptado (cheque) |
| 2 | PS_OS_PAYMENT | Pago aceptado |
| 3 | PS_OS_PREPARATION | En preparacion |
| 4 | PS_OS_SHIPPING | Enviado |
| 5 | PS_OS_DELIVERED | Entregado |
| 6 | PS_OS_CANCELED | Cancelado |
| 7 | PS_OS_REFUND | Reembolsado |
| 8 | PS_OS_ERROR | Error de pago |
| 9 | PS_OS_OUTOFSTOCK | Sin stock (pago aceptado) |
| 10 | PS_OS_BANKWIRE | Pago pendiente (transferencia) |
| 11 | PS_OS_PAYPAL | Pago remoto (PayPal) |
| 12 | PS_OS_WS_PAYMENT | Pago por API |
Usar las constantes de estado
php
<?php
// Las constantes son valores de Configuration, no literales
$paidStateId = (int) Configuration::get('PS_OS_PAYMENT'); // = 2
$pendingStateId = (int) Configuration::get('PS_OS_BANKWIRE'); // = 10
$errorStateId = (int) Configuration::get('PS_OS_ERROR'); // = 8
$cancelledStateId = (int) Configuration::get('PS_OS_CANCELED'); // = 6
#Cambiar el estado de un pedido
Cambiar estado con OrderHistory
php
<?php
/**
* Cambia el estado de un pedido y envia el email de notificacion.
*
* @param int $idOrder ID del pedido
* @param int $idOrderState ID del nuevo estado
*/
private function setOrderState(int $idOrder, int $idOrderState): void
{
$order = new Order($idOrder);
if (!Validate::isLoadedObject($order)) {
throw new \RuntimeException("Order {$idOrder} not found");
}
// Crear el historial de estado
$history = new OrderHistory();
$history->id_order = $idOrder;
$history->id_order_state = $idOrderState;
$history->id_employee = (int) Context::getContext()->employee->id;
$history->date_add = date('Y-m-d H:i:s');
$useExistingPayment = !$order->hasInvoice();
$history->addWithemail($useExistingPayment);
// addWithemail(true) → usa el pago existente en lugar de crear uno nuevo
// addWithemail(false) → fuerza la creacion de factura si el estado lo requiere
}
// Ejemplo en el ConfirmationFrontController tras pago exitoso:
$this->setOrderState($idOrder, (int) Configuration::get('PS_OS_PAYMENT'));
#Crear un estado personalizado
Crear y eliminar un OrderState personalizado
php
<?php
// En mymodule.php → install()
private function installOrderState(): bool
{
if (Configuration::get('MYMODULE_OS_PENDING')) {
return true; // Ya existe
}
$state = new OrderState();
$state->color = '#4169e1'; // Azul
$state->hidden = false;
$state->delivery = false;
$state->logable = true; // Aparece en estadisticas
$state->invoice = false;
$state->module_name = $this->name;
$state->send_email = true;
$state->template = 'payment'; // Plantilla de email (mails/{lang}/payment.html)
$state->unremovable = false;
foreach (Language::getLanguages() as $lang) {
$state->name[$lang['id_lang']] = 'Pago pendiente (MyModule)';
}
if (!$state->add()) {
return false;
}
Configuration::updateValue('MYMODULE_OS_PENDING', (int) $state->id);
return true;
}
private function uninstallOrderState(): bool
{
$id = (int) Configuration::get('MYMODULE_OS_PENDING');
if ($id) {
$state = new OrderState($id);
$state->delete();
Configuration::deleteByName('MYMODULE_OS_PENDING');
}
return true;
}
#Hooks de transicion de estado
| Hook | Cuando se llama | Parametros clave |
|---|---|---|
| actionOrderStatusUpdate | Al cambiar cualquier estado de pedido | newOrderStatus, id_order |
| actionOrderStatusPostUpdate | Despues de guardar el nuevo estado | newOrderStatus, id_order, objOrder |
| actionObjectOrderHistoryAddAfter | Al crear un OrderHistory | object (OrderHistory) |
| actionPaymentConfirmation | Al confirmar el pago (estado paid) | id_order |
| actionOrderEdited | Al editar un pedido manualmente | order |
Ejemplo de hook de transicion de estado
php
<?php
public function hookActionOrderStatusUpdate(array $params): void
{
$newStatus = $params['newOrderStatus']; // OrderState object
$idOrder = (int) $params['id_order'];
// Reaccionar solo a un estado especifico
if ($newStatus->id === (int) Configuration::get('PS_OS_SHIPPING')) {
// El pedido acaba de pasar a 'Enviado'
$order = new Order($idOrder);
// ... notificar al ERP, actualizar stock externo, etc.
}
}
Descargar en Markdown
Pensado para pegar en ChatGPT, Claude u otra IA. Incluye solo el contenido de esta pagina.