use Drupal\commerce_order\Entity\OrderInterface; /** * Cherche récursivement une entité de commande dans un tableau. */ function _jango_sub_find_order_recursive($data) { if ($data instanceof OrderInterface) { return $data; } if (is_array($data)) { foreach ($data as $k => $v) { if ($k === '#order' || $k === '#order_entity') { if ($v instanceof OrderInterface) { return $v; } } $found = _jango_sub_find_order_recursive($v); if ($found instanceof OrderInterface) { return $found; } } } return NULL; } /** * Préprocess pour le template "commerce-order-receipt.html.twig". */ function jango_sub_preprocess_commerce_order_receipt(array &$variables) { // 1) Tenter les clés "classiques". /** @var \Drupal\commerce_order\Entity\OrderInterface|null $order */ $order = $variables['order'] ?? ($variables['order_entity'] ?? NULL) ?? (!empty($variables['elements']['#order']) ? $variables['elements']['#order'] : NULL) ?? (!empty($variables['elements']['#order_entity']) ? $variables['elements']['#order_entity'] : NULL); // 2) Si toujours rien, fouille récursivement toutes les variables. if (!$order instanceof OrderInterface) { $order = _jango_sub_find_order_recursive($variables); } // 3) Si on n’a toujours pas l’ordre → valeurs “sûres” et on sort. if (!$order instanceof OrderInterface) { $variables['order_number'] = '—'; $variables['order_date'] = '—'; $variables['order_status'] = '—'; $variables['items'] = []; $variables['subtotal'] = '—'; $variables['shipping_total'] = '—'; $variables['grand_total'] = '—'; $variables['shipping_address_html'] = '—'; $variables['billing_address_html'] = '—'; $variables['shipping_method'] = '—'; $variables['payment_method'] = '—'; $variables['support_email'] = \Drupal::config('system.site')->get('mail') ?: 'info@akaguriro.com'; return; } // --- Date/format. /** @var \Drupal\Core\Datetime\DateFormatterInterface|null $date_formatter */ $date_formatter = _jango_sub_service('date.formatter'); $variables['order_number'] = $order->getOrderNumber() ?: $order->id(); $placed_time = method_exists($order, 'getPlacedTime') && $order->getPlacedTime() ? $order->getPlacedTime() : \Drupal::time()->getRequestTime(); $variables['order_date'] = $date_formatter ? $date_formatter->format($placed_time, 'custom', 'd/m/Y H:i') : date('d/m/Y H:i', $placed_time); $variables['order_status'] = $order->getState()->getLabel(); // --- Lignes. $items = []; foreach ($order->getItems() as $order_item) { $items[] = [ 'title' => $order_item->label(), 'qty' => (string) (int) $order_item->getQuantity(), 'unit_price' => _jango_sub_format_price($order_item->getUnitPrice()), 'total_price' => _jango_sub_format_price($order_item->getTotalPrice()), 'options_html'=> '', ]; } $variables['items'] = $items; // --- Totaux. $subtotal = method_exists($order, 'getSubtotalPrice') ? $order->getSubtotalPrice() : NULL; if (!$subtotal) { $sum = NULL; foreach ($order->getItems() as $it) { $line = $it->getUnitPrice()->multiply($it->getQuantity()); $sum = $sum ? $sum->add($line) : $line; } $subtotal = $sum; } $variables['subtotal'] = _jango_sub_format_price($subtotal); $variables['shipping_total'] = NULL; $variables['discount_total'] = NULL; $variables['tax_total'] = NULL; $variables['grand_total'] = _jango_sub_format_price($order->getTotalPrice()); // --- Livraison (adresse + méthode + frais). $variables['shipping_address_html'] = '—'; $variables['shipping_method'] = '—'; if ($order->hasField('shipments') && !$order->get('shipments')->isEmpty()) { $shipments = $order->get('shipments')->referencedEntities(); if ($shipments) { $shipment = reset($shipments); if ($shipment->hasField('shipping_profile') && !$shipment->get('shipping_profile')->isEmpty()) { $sp = $shipment->get('shipping_profile')->entity; $variables['shipping_address_html'] = _jango_sub_format_address_html($sp); } if (method_exists($shipment, 'getShippingMethod') && $shipment->getShippingMethod()) { $variables['shipping_method'] = $shipment->getShippingMethod()->label(); } if (method_exists($shipment, 'getAmount') && $shipment->getAmount()) { $variables['shipping_total'] = _jango_sub_format_price($shipment->getAmount()); } } } // --- Facturation. $variables['billing_address_html'] = '—'; if (method_exists($order, 'getBillingProfile') && ($bp = $order->getBillingProfile())) { $variables['billing_address_html'] = _jango_sub_format_address_html($bp); } // --- Moyen de paiement. $variables['payment_method'] = '—'; if ($order->hasField('payment_gateway') && !$order->get('payment_gateway')->isEmpty()) { $gateway = $order->get('payment_gateway')->entity; if ($gateway) { $variables['payment_method'] = $gateway->label(); } } // --- Support & OBR placeholders. $variables['support_email'] = \Drupal::config('system.site')->get('mail') ?: 'info@akaguriro.com'; $variables['obr_nif'] = $variables['obr_nif'] ?? '—'; $variables['obr_rc'] = $variables['obr_rc'] ?? '—'; $variables['obr_centre'] = $variables['obr_centre'] ?? '—'; $variables['obr_adresse'] = $variables['obr_adresse'] ?? '—'; $variables['obr_status'] = $variables['obr_status'] ?? NULL; $variables['obr_signature'] = $variables['obr_signature'] ?? NULL; $variables['obr_receipt_number'] = $variables['obr_receipt_number'] ?? NULL; } Page not found | Akaguriro.com Skip to main content
The requested page could not be found.