MikeiLL
MikeiLL

Reputation: 6570

OpenCart trigger Controller Function from Template Page

The following function works when called within the controller admin/controller/sale/customer.php, but can someone please show me how to call it from a form in the template admin/view/template/sale/customer_form.tpl?

    public function process_cart() {
        $this->load->model('sale/order');
        $this->load->model('sale/customer');
        $cust = $this->model_sale_customer->getCustomer($this->request->get['customer_id']);
          function add_tax(&$data) {
                foreach($data as &$details)
                    $details['tax'] = 0;
                return $data;
            }

          $order_product = add_tax($this->data['products']);

          $order_details = array(
                'store_id' => 1,
                'customer_id' => $cust['customer_id'],
                'customer_group_id' => 1,
                'firstname' => $cust['firstname'],
                'lastname' => $cust['lastname'],
                'email' => $cust['email'],
                'telephone' => $cust['telephone'],
                'fax' => $cust['fax'],
                'payment_firstname' => $cust['firstname'],
                'payment_lastname' => $cust['lastname'],
                'more_keys' =>  'and_values_etc',
                'order_product' => $order_product
                );

        $this->model_sale_order->addOrder($order_details);

    }

By the way, what the function does is take the contents of a user's saved cart (as rendered with the Admin View Customers Saved Cart extension) and, from the admin side, convert it to an order. We're using it in a Food buying Club where prices and availability are approximate until the shipment arrives.

This is what little I have in the template so far:

        <form action="<?php echo $action; ?>" method="post">
        <input type="hidden" name="process" value="process">
        <input type="submit" value="Process Order">
        </form>

Upvotes: 0

Views: 1937

Answers (1)

shadyyx
shadyyx

Reputation: 16065

The functions in controllers are called actions and You can call each action by an HTTP request (be it a direct URL hit, AJAX request, cURL request, API call, whatever).

The key is to request data at concrete URL.

May you not know how the URLs for controller actions in OpenCart are done You can check my not so old answer about the routing in OpenCart. There You can find out that if You want to invoke an action process_cart() within a controller SaleCustomerController You would need to request this URL:

http://myopencart.com/admin/index.php?route=sale/customer/process_cart&token=<SECRET_TOKEN>

Mind the token part - this is the CSRF administration protection and this token is always available for the logged in user within every controller under the variable

$this->session->data['token']

thus I recommend building the request URL(s) within the controller all the time, such as:

$this->data['process_order_action'] = $this->url->link('sale/customer/process_cart', 'token=' . $this->session->data['token']);

Then you can print this $process_order_action variable in Your template to use the URL however You would like.

EDIT: to add additional query string parameters You have to add them into the same string as token parameter:

$this->data['process_order_action'] = $this->url->link('sale/customer/process_cart', 'token=' . $this->session->data['token'] . '&customer_id=' . $this->request->get['customer_id']);
// note that the parameters in query string are joined by ampersand (&)  ->        ->         ->            ->           ^^^^^^^^^

Upvotes: 1

Related Questions