1

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>
Anmol singh
  • 158
  • 1
  • 12
  • you want to this ?? : https://youtu.be/zD1ptqOUvKc – Mujahid Bhoraniya Jan 08 '20 at 11:07
  • link under video description is not working. – Anmol singh Jan 08 '20 at 11:09
  • Actually i am finding the ajax query which fetches the details, but can not find at all. I have modified my steps like in video, so only want to fetch the details. – Anmol singh Jan 08 '20 at 11:10
  • yeah. it will be very tough. please contact opencart developer – Mujahid Bhoraniya Jan 08 '20 at 11:11
  • 1
    you can also use for this extension because it's free. https://www.opencart.com/index.php?route=marketplace/extension/info&extension_id=15580&filter_search=checkout&filter_category_id=5&filter_license=0 – Mujahid Bhoraniya Jan 08 '20 at 11:13
  • agree with @MujahidBhoraniya this is a broad task, cannot be answered here ... – Angel Deykov Jan 08 '20 at 11:14
  • Anmol, are you just wanting to display the cart details in a sidebar? If so, can you update your question to show what details you want displayed? e.g. product name, quantity, unit price etc – Daniel Jan 09 '20 at 11:08
  • Yes, Exactly.@Daniel. I want to show product Name & Price with (Discount, voucher, coupon) if applied. – Anmol singh Jan 10 '20 at 07:22
  • I have looked upon the ajax query in checkout page that is responsible to do so, but can't find it. – Anmol singh Jan 10 '20 at 07:26
  • Wouldn't it be easier to use the "Cart" information? Since you don't know details of shipping costs until those steps have been reached, you can only show products and the sub-total. If you want that information to update during the checkout steps, you're going to have to code a bit more here – Daniel Jan 10 '20 at 08:54
  • Ok,@Daniel. How can i show only products & sub total? – Anmol singh Jan 10 '20 at 13:50
  • Sorry, I wasn't alerted to this response @Anmolsingh. Do you have experience with MVC? – Daniel Jan 13 '20 at 13:15
  • Yes, i understand the MVC structure. You can give steps. I can follow.@Daniel – Anmol singh Jan 13 '20 at 15:37
  • @Anmolsingh please mark the answer as accepted if it works for you, it's the incentive for us to help you. – Daniel Jan 14 '20 at 09:42

1 Answers1

1

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).

Daniel
  • 2,167
  • 5
  • 23
  • 44