phpopencartopencart2.xopencart-moduleopencart2.3

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


I want to fetch the product details that are added to cart, on page load in checkout page.

As we know in checkout page there is generally six Steps to place order, where each step's information is in

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

which remains empty until we reached to that step. so, product details is under the last step(6th) to be fetched. But i want to fetch the products details which i will show on right side in

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

Solution

  • Ideally you're going to want to create an OCMOD for this but that is outside of the scope of answering this question.

    Since you're looking at inserting the contents of the "cart" into the page, you'll need to copy the controller logic from the cart "module" controller catalog/controller/common/cart.php and the markup from the view catalog/view/theme/default/template/common/cart.tpl (change according to your template).

    1. Grab these lines from inside the controller file (Public function 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. Create a new file in the catalog/controller/common/ folder, call it products.php and use the following structure:

      <?php 
      class ControllerCommonProducts extends Controller {
          public function index(){
              //placeholder
      
              return $data;
          }
      }
      
    3. Paste the copied text it into the new file replacing the //placeholder line.

    4. Edit the catalog/controller/checkout/checkout.php file, insert this line to load the data from the controller we have just created into the checkout page view: $data['products_view'] = $this->load->controller('common/products'); insert it just above this line:

      $this->response->setOutput($this->load->view('checkout/checkout', $data));
      
    5. Now edit the view catalog/view/theme/default/template/checkout/checkout.tpl you will now have a new array products_view that you can use on the view as you need. Start with a var_dump of the array to see what data you have to work with.

    Summary: What we've done here is replicate an existing controller (1, 2 & 3) - excluding the controller logic where it loads the data into a view. We then edited the controller's index action (3) of the page we're displaying data on and loaded the new controller (4) into an array to be used on the front end view (5).