Merx

Custom Invoice Numbers

By default the invoice number is generated by the page number.
$orderPage->invoiceNumber()

You can override this method to use a custom field.
To update the field a custom hook can be used.

Override invoiceNumber()

You can override the default invoiceNumber() method to return for example a content field. In this example the content of invoiceNumber field is returned.

site/models/order.php

<?php
class OrderPage extends OrderPageAbstract
{
  public function invoiceNumber(): string
  {
    return $this->content()->invoiceNumber()->toString();
  }
}

Be careful, the invoiceNumber() method has to return a string.

Update content field using a hook

To update the invoiceNumber content field you can use the completePayment:after hook.

site/config/config.php


<?php
return [
  'hooks' => [
    'ww.merx.completePayment:after' => function ($orderPage) {
      $invoiceNumber = 0;
      if ($prevOrder = $orderPage->prevListed()) {
        $invoiceNumber = (int)$prevOrder->invoiceNumber();
      }
      $invoiceNumber = $invoiceNumber + 1;
      $orderPage->update([
        'invoiceNumber' => $invoiceNumber,
      ]);
    },
  ],
];

Return Invoice Number

To return the invoice number you should use the custom $orderPage->invoiceNumber() method.

site/templates/order.php

<?php
echo 'Invoice Number: ';
echo $page->invoiceNumber();