Jump to content

How To Get Order Number In Blesta


tdphong

Recommended Posts

Currently, i order a product and i wanna get order number which is created.So, how can i do it?

 

At what point do you want to get the order number? And from where? What do you plan to do with the order number?

 

We don't yet understand what you're trying to do so we're not sure how to answer your question. We could use more information.

 

For example, you can fetch an order by it's order ID (assuming you have it) by calling the OrderOrders model of the Order plugin from within your controller:

$this->uses(array("Order.OrderOrders"));
$order_id = "15";
$order = $this->OrderOrders->get($order_id);

if ($order) {
    $order_number = $order->order_number;
}
Link to comment
Share on other sites

I think it's first important to understand the payment flow here. When a payment is made using a non-merchant gateway, the gateway is unaware of how the invoice it is intending to pay is created, or even if there is an invoice at all. You can make a payment using the non-merchant gateway to pay for nothing.

 

In your case, however, you want to pay for an invoice created from an order in the order system using the Order plugin. This is a special case, and should be treated as such. Which means, you should not rely on this to always be true, and must setup logic to handle the cases where it is not.

 

In your non-merchant gateway's class, you should have defined a buildProcess method. This method accepts, optionally, an array of invoice amounts as its third parameter. In that array is a list of IDs representing the invoices being paid. If you are paying an invoice on the order form, you should only have one invoice ID in the list. You need to take this invoice ID and find the associated order ID from the order system.

 

Considering the order system is a plugin, it may not be installed for the company. You should check that it is first.

Loader::loadModels($this, array("PluginManager"));
$order_number = "N/A";

// Is the Order plugin installed?
if ($this->PluginManager->isInstalled("order", Configure::get("Blesta.company_id"))) {
    ...
}

The Order plugin associates an invoice with the order, so you must find the correct order that matches the invoice ID you were given. However, the Order plugin does not provide a method to find an order by invoice, so you must write your own. First, make sure you have an invoice number.

$invoice_id = null;
if (isset($invoice_amounts[0])) {
    $invoice_id = $invoice_amounts[0]['id'];
}

Now you need to find the order number associated with the invoice. Since the Order plugin does not have a method for doing this, you can either 1) add a method for it to do it from the OrderOrders model by updating the source code yourself, or use the Record component to query the database for it manually. The later is more difficult as you will need to manually connect to the database by passing valid credentials yourself. See here for an example of someone that has. I will skip that part and simply assume the Record component is setup to perform a query.

Loader::loadComponents($this, array("Record"));
#
# TODO: create a connection to the database
#
try {
    $order = $this->Record->select()->from("order_orders")
             ->where("invoice_id", "=", $invoice_id)->fetch();
    if ($order) {
        $order_number = $order->order_number;
    }
} catch (Exception $e) {
    // Unable to fetch the order number
}

Putting it all together now:

Loader::loadModels($this, array("PluginManager"));
Loader::loadComponents($this, array("Record"));
$order_number = "N/A";

// Is the Order plugin installed?
if ($this->PluginManager->isInstalled("order", Configure::get("Blesta.company_id"))) {
    // Do we have one invioce?
    $invoice_id = null;
    if (isset($invoice_amounts[0]) && count($invoice_amounts) === 1) {
        $invoice_id = $invoice_amounts[0]['id'];

        #
        # TODO: create a connection to the database so we can run a query
        #
        // Can we fetch an order for this invoice?
        try {
            $order = $this->Record->select()->from("order_orders")
                     ->where("invoice_id", "=", $invoice_id)->fetch();
            if ($order) {
                $order_number = $order->order_number;
            }
        } catch (Exception $e) {
            // Unable to fetch the order number
        }
    }
}

// Use the $order_number

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...