首页 > 解决方案 > 在结帐页面 Opencart:2.3.0.2 中获取页面加载时的产品详细信息

问题描述

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

正如我们在结帐页面中所知,通常有六个下订单步骤,每个步骤的信息都在

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

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

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

标签: phpopencartopencart2.xopencart-moduleopencart2.3

解决方案


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

由于您正在考虑将“购物车”的内容插入页面,因此您需要从购物车“模块”控制器复制控制器逻辑catalog/controller/common/cart.php并从视图中复制标记catalog/view/theme/default/template/common/cart.tpl(根据您的模板进行更改)。

  1. 从控制器文件中获取这些行(公共函数 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) 上使用。


推荐阅读