【问题标题】:OpenCart trigger Controller Function from Template PageOpenCart 从模板页面触发控制器功能
【发布时间】:2014-07-17 19:46:32
【问题描述】:

以下函数在控制器admin/controller/sale/customer.php 中调用时有效,但有人可以告诉我如何从模板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);

    }

顺便说一句,该函数的作用是获取用户保存的购物车的内容(通过 Admin View Customers Saved Cart 扩展程序呈现),然后从管理员端将其转换为订单。我们在食品购买俱乐部中使用它,在货物到达之前,价格和供应情况都是近似的。

到目前为止,这是我在模板中的一点点:

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

【问题讨论】:

    标签: php opencart


    【解决方案1】:

    控制器中的函数称为 actions,您可以通过 HTTP 请求调用每个操作(无论是直接 URL 命中、AJAX 请求、cURL 请求、API 调用等)。

    关键是在具体的URL请求数据。

    您可能不知道 OpenCart 中控制器操作的 URL 是如何完成的您可以查看我的不太老的answer about the routing in OpenCart。在那里您可以发现,如果您想在控制器 SaleCustomerController 内调用操作 process_cart(),您需要请求此 URL:

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

    注意token 部分 - 这是 CSRF 管理保护,此令牌始终可供变量下每个控制器中的登录用户使用

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

    因此我建议始终在控制器中构建请求 URL,例如:

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

    然后你可以在你的模板中打印这个$process_order_action 变量来使用你想要的URL。

    编辑: 添加其他查询字符串参数您必须将它们添加到与令牌参数相同的字符串中:

    $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 (&)  ->        ->         ->            ->           ^^^^^^^^^
    

    【讨论】:

    • 似乎工作正常,但我需要在 customer_id 的 url 中添加另一个键值对。 $this-&gt;data['process_order_action'] = $this-&gt;url-&gt;link('sale/customer/checkout', 'token=' . $this-&gt;session-&gt;data['token'], 'customer_id=' . $this-&gt;request-&gt;get['customer_id']); 不工作...
    • 还尝试添加, '&amp;customer_id=' . $this-&gt;request-&gt;get['customer_id'] - 与&amp;
    • 检查我编辑的答案。只需查看 OpenCart 的源代码就可以观察到这样的事情。不客气。
    • 漂亮,伙计。我使用了一个逗号,我应该只是连接一个句号。呃!非常感谢,祝您有美好的一天。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多