Search code examples
opencartcheckoutopencart2.xopencart-module

How to get category in checkout cart in opencart?


first at all thanks for the help... Im new on opencart, hope somebody can help me...

Im getting crazy trying to find the answer in google for my question...

I wanna add the category name in checkout/cart

<a href="<?php echo $product['href']; ?>"><?php echo $category['name']; ?></a>

I try to add a new variable in the controller of cart.pl, but is impossible, as the answer that i found in internet doesnt work...

BTW i work on Opencart 2.2.0...

this is what i have in cart.pl on my template:

<?php foreach ($products as $product) { ?>      

                <div class="gry_part">
                        <div class="width1">

                        <a href="<?php echo $product['href']; ?>"><?php echo $category['name']; ?></a> - 
                        <a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a>

                        <?php if (!$product['stock']) { ?>
                          <span class="stock">***</span>
                        <?php } ?>

                        <div>
                            <?php foreach ($product['option'] as $option) { ?>
                            - <small><?php echo $option['name']; ?>: <?php echo $option['value']; ?></small><br />
                            <?php } ?>
                            <?php if($product['recurring']): ?>
                            - <small><?php echo $text_payment_profile ?>: <?php echo $product['profile_name'] ?></small>
                            <?php endif; ?>
                          </div>
                          <?php if ($product['reward']) { ?>
                          <small><?php echo $product['reward']; ?></small>
                          <?php } ?>
                        </div>
                        <div class="width2 quantity">
                            <input type="text" name="quantity[<?php echo $product['cart_id']; ?>]" value="<?php echo $product['quantity']; ?>" size="1" />
                            <input type="image" src="catalog/view/theme/amishop/image/update.png" alt="<?php echo $button_update; ?>" title="<?php echo $button_update; ?>" />
                        </div>
                        <div class="width3"><?php echo $product['price']; ?></div>
                        <div class="width4">
                        <button type="button" data-toggle="tooltip" title="<?php echo $button_remove; ?>" class="add_to99" onclick="cart.remove('<?php echo $product['cart_id']; ?>');">Delete</button>
                        </div>
                    </div>

        <?php } ?>

and this is what i try to add in the controller cart.pl

$categories = $this->model_catalog_product->getCategories($product_id);
        if ($categories)
            $categories_info = $this->model_catalog_category->getCategory($categories[0]['category_id']);
        $this->data['category_title'] = $categories_info['name'];

sorry for this, if is quite stupid question, but im new on this...

thanks for all..


Solution

  • Try following Source : A2bizz blogs

    Create an OCMOD file - category_on_cart.ocmod.xml

    Copy following code and save it

    <?xml version="1.0" encoding="utf-8"?>
    <modification>
        <name>Category_Cart</name>
        <version>1.0</version>
        <code>Category_Cart</code>
        <author>A2bizz</author>
        <link>http://blog.a2bizz.com</link>
        <file path="catalog/view/theme/*/template/checkout/cart.tpl">
            <operation>
                <search><![CDATA[
               <td class="text-left"><?php echo $column_name; ?></td>
                ]]></search>
                <add position="after" offset="0"><![CDATA[
                <td class="text-left"><?php echo $column_category; ?></td>
                ]]></add>
            </operation>
             <operation>
                <search><![CDATA[
               <td class="text-left"><?php echo $product['model']; ?></td>
                ]]></search>
                <add position="before" offset="0"><![CDATA[
                <td class="text-left"><?php echo $product['category_name']; ?></td>
                ]]></add>
            </operation>
        </file>
        <file path="catalog/controller/checkout/cart.php">
            <operation>
                <search><![CDATA[
               $data['column_name'] = $this->language->get('column_name');
                ]]></search>
                <add position="after" offset="0"><![CDATA[
                $data['column_category'] = $this->language->get('column_category');
                ]]></add>
            </operation>
             <operation>
                <search><![CDATA[
               $this->load->model('tool/upload');
                ]]></search>
                <add position="after" offset="0"><![CDATA[
                $this->load->model('catalog/custom');
                ]]></add>
            </operation>
            <operation>
                <search><![CDATA[
               $product_total = 0;
                ]]></search>
                <add position="before" offset="0"><![CDATA[
                $category_name = $this->model_catalog_custom->getproductcategory($product['product_id']);
                ]]></add>
            </operation>
            <operation>
                <search><![CDATA[
               'name'      => $product['name'],
                ]]></search>
                <add position="after" offset="0"><![CDATA[
                'category_name'=>$category_name,
                ]]></add>
            </operation>
        </file>
         <file path="catalog/language/*/checkout/cart.php">
            <operation>
                <search><![CDATA[
               $_['column_name']              = 'Product Name';
                ]]></search>
                <add position="after" offset="0"><![CDATA[
                $_['column_category']              = 'Product Category';
                ]]></add>
            </operation>
        </file>
    </modification>
    

    Create catalog/model/catalog/custom.php

    add following code in this file.

    <?php
    class ModelCatalogCustom extends Model {
        public function getproductcategory($product_id){
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
            $category = $this->getCategory($query->row['category_id']);
            return $category['name'];
        }
    
        public function getCategory($category_id) {
            $query = $this->db->query("SELECT DISTINCT *, (SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR '&nbsp;&nbsp;&gt;&nbsp;&nbsp;') FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id AND cp.category_id != cp.path_id) WHERE cp.category_id = c.category_id AND cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY cp.category_id) AS path, (SELECT DISTINCT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'category_id=" . (int)$category_id . "') AS keyword FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (c.category_id = cd2.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'");
    
            return $query->row;
        }
    }
    

    Now go to the Admin dashboard -> Extensions -> Extension installer to install the category_on_Cart.ocmod.xml file.

    After that go to the Admin dashboard-> Extensions->Modifications to refresh cache. Click on refresh buttton first button on right corner.

    enter image description here