在结帐页面 Opencart:2.3.0.2 中获取有关页面加载的产品详细信息

Fetch product details on page load in checkout page Opencart:2.3.0.2

我想在结帐页面加载页面时获取添加到购物车的产品详细信息。

我们知道结账页面一般有六个步骤来下订单,每个步骤的信息都在

<div class="panel-body"></div>

在我们到达那一步之前它一直是空的。 因此,产品详细信息在要获取的最后一步(第 6 步)下。 但我想获取我将在右侧显示的产品详细信息

<div classs="col-md-4"> </div>

理想情况下,您希望为此创建一个 OCMOD,但这超出了回答此问题的范围。

由于您要将 "cart" 的内容插入到页面中,因此您需要从购物车 "module" 控制器 catalog/controller/common/cart.php 和来自视图的标记 catalog/view/theme/default/template/common/cart.tpl(根据您的模板更改)。

  1. 从控制器文件中获取这些行(Public 函数 index()):

    $this->load->language('common/cart');
    
    // Totals
    $this->load->model('extension/extension');
    
    $totals = array();
    $taxes = $this->cart->getTaxes();
    $total = 0;
    
    // Because __call can not keep var references so we put them into an array.
    $total_data = array(
        'totals' => &$totals,
        'taxes'  => &$taxes,
        'total'  => &$total
    );
    
    // Display prices
    if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
        $sort_order = array();
    
        $results = $this->model_extension_extension->getExtensions('total');
    
        foreach ($results as $key => $value) {
            $sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
        }
    
        array_multisort($sort_order, SORT_ASC, $results);
    
        foreach ($results as $result) {
            if ($this->config->get($result['code'] . '_status')) {
                $this->load->model('extension/total/' . $result['code']);
    
                // We have to put the totals in an array so that they pass by reference.
                $this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
            }
        }
    
        $sort_order = array();
    
        foreach ($totals as $key => $value) {
            $sort_order[$key] = $value['sort_order'];
        }
    
        array_multisort($sort_order, SORT_ASC, $totals);
    }
    
    $data['text_empty'] = $this->language->get('text_empty');
    $data['text_cart'] = $this->language->get('text_cart');
    $data['text_checkout'] = $this->language->get('text_checkout');
    $data['text_recurring'] = $this->language->get('text_recurring');
    $data['text_items'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), $this->currency->format($total, $this->session->data['currency']));
    $data['text_loading'] = $this->language->get('text_loading');
    
    $data['button_remove'] = $this->language->get('button_remove');
    
    $this->load->model('tool/image');
    $this->load->model('tool/upload');
    
    $data['products'] = array();
    
    foreach ($this->cart->getProducts() as $product) {
        if ($product['image']) {
            $image = $this->model_tool_image->resize($product['image'], $this->config->get($this->config->get('config_theme') . '_image_cart_width'), $this->config->get($this->config->get('config_theme') . '_image_cart_height'));
        } else {
            $image = '';
        }
    
        $option_data = array();
    
        foreach ($product['option'] as $option) {
            if ($option['type'] != 'file') {
                $value = $option['value'];
            } else {
                $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
    
                if ($upload_info) {
                    $value = $upload_info['name'];
                } else {
                    $value = '';
                }
            }
    
            $option_data[] = array(
                'name'  => $option['name'],
                'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value),
                'type'  => $option['type']
            );
        }
    
        // Display prices
        if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
            $price = $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
        } else {
            $price = false;
        }
    
        // Display prices
        if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
            $total = $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')) * $product['quantity'], $this->session->data['currency']);
        } else {
            $total = false;
        }
    
        $data['products'][] = array(
            'cart_id'   => $product['cart_id'],
            'thumb'     => $image,
            'name'      => $product['name'],
            'model'     => $product['model'],
            'option'    => $option_data,
            'recurring' => ($product['recurring'] ? $product['recurring']['name'] : ''),
            'quantity'  => $product['quantity'],
            'price'     => $price,
            'total'     => $total,
            'href'      => $this->url->link('product/product', 'product_id=' . $product['product_id'])
        );
    }
    
    // Gift Voucher
    $data['vouchers'] = array();
    
    if (!empty($this->session->data['vouchers'])) {
        foreach ($this->session->data['vouchers'] as $key => $voucher) {
            $data['vouchers'][] = array(
                'key'         => $key,
                'description' => $voucher['description'],
                'amount'      => $this->currency->format($voucher['amount'], $this->session->data['currency'])
            );
        }
    }
    
    $data['totals'] = array();
    
    foreach ($totals as $total) {
        $data['totals'][] = array(
            'title' => $total['title'],
            'text'  => $this->currency->format($total['value'], $this->session->data['currency']),
        );
    }
    
  2. catalog/controller/common/文件夹中创建一个新文件,命名为products.php并使用以下结构:

    <?php 
    class ControllerCommonProducts extends Controller {
        public function index(){
            //placeholder
    
            return $data;
        }
    }
    
  3. 将复制的文本粘贴到新文件中,替换 //placeholder 行。

  4. 编辑catalog/controller/checkout/checkout.php文件,插入这一行来将我们刚刚创建的控制器的数据加载到结帐页面视图中: $data['products_view'] = $this->load->controller('common/products'); 将其插入到这一行的正上方:

    $this->response->setOutput($this->load->view('checkout/checkout', $data));
    
  5. 现在编辑视图 catalog/view/theme/default/template/checkout/checkout.tpl 您现在将拥有一个新数组 products_view,您可以根据需要在视图上使用它。从数组的 var_dump 开始,看看您必须使用哪些数据。

总结: 我们在这里所做的是复制一个现有的控制器(1、2 和 3)——不包括将数据加载到视图中的控制器逻辑。然后,我们编辑了我们正在显示数据的页面的控制器索引操作 (3),并将新控制器 (4) 加载到一个数组中,以便在前端视图 (5) 上使用。