Post by Pedanov » Wed Aug 17, 2016 4:42 pm

Hi guys!

Want to create custom layout for some of products in my OC 2.1 shop. What do I do:
1. Adding product1.php to /catalog/controller/product folder (from the copy of product.php);
2. Changinf links for template files to product1.tpl, changing class to ControllerProductProduct1 in the beginning of file;
3. Adding product1.tpl file in template/product folder copying product.tpl with appropriate changes (background color);
4. Adding new layout "Product1" in Design/Layout menu in admin panel with route: "product/product1";
5. Adding module which I want to see on the custom product page;
6. Changing design for product I want to see with custom design to Product1;
7. Modifications/Refresh.

But as a result I can see this product with default prosuct.tpl design without my changes but with module which I added for custom layout Product1. :choke:

What's strange is that /index.php?route=product/product1 also gives default product.tpl as a template.

WDIDW? Or how is possible to make custom product layouts for different products?

Newbie

Posts

Joined
Wed Aug 17, 2016 4:29 pm

Post by qahar » Wed Aug 17, 2016 8:05 pm

Based on your description, it should work. Do you use other modification than OcMod? maybe the output is altered.


The problem with your approach is that your new product controller is not updated when updating OpenCart. Or you have ext that modificate product controller, it won't get to your new constoller. Instead duplicate the controller, its easier to change the template based on product id in product controller.

Code: Select all

$product_id = $this->request->get['product_id'];

if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/product_' . $product_id . '.tpl')) {
    $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/product/product_' . $product_id . '.tpl', $data));
} elseif (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/product.tpl')) {
    $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/product/product.tpl', $data));
} else {
    $this->response->setOutput($this->load->view('default/template/product/product.tpl', $data));
}
Assuming you have product with id 250, then if you have template product_250.tpl in your active theme it will be used instead of general product.tpl. If you also interesting to load specific stylesheet or script for specific product you can refer here. Old tutorials but the concept is still applied for OpenCart 2.

Now to apply custom Design Layout, you can create new Layout with empty route. Lets say that you create layout with name "Specific Product - cool offer", make sure the route is empty; to avoide conflict with existing controller. Then on admin product for id 250 > tab Design, select "Specific Product - cool offer" as Layout override.

This would give you the trick. Custom product template and design layout whenever you need for future custom product.

User avatar
Expert Member

Posts

Joined
Tue Jun 29, 2010 10:24 pm
Location - Indonesia

Post by Pedanov » Wed Aug 17, 2016 9:16 pm

qahar wrote:Based on your description, it should work. Do you use other modification than OcMod? maybe the output is altered.
VQMod but I doubt it should not modify product1.php file in controller
qahar wrote:The problem with your approach is that your new product controller is not updated when updating OpenCart.
How can I fix it? Seems that OC core doesn't know anything about new product controller
qahar wrote:Instead duplicate the controller, its easier to change the template based on product id in product controller.

Code: Select all

$product_id = $this->request->get['product_id'];

if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/product_' . $product_id . '.tpl')) {
    $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/product/product_' . $product_id . '.tpl', $data));
} elseif (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/product.tpl')) {
    $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/product/product.tpl', $data));
} else {
    $this->response->setOutput($this->load->view('default/template/product/product.tpl', $data));
}
Assuming you have product with id 250, then if you have template product_250.tpl in your active theme it will be used instead of general product.tpl. If you also interesting to load specific stylesheet or script for specific product you can refer here. Old tutorials but the concept is still applied for OpenCart 2.

Now to apply custom Design Layout, you can create new Layout with empty route. Lets say that you create layout with name "Specific Product - cool offer", make sure the route is empty; to avoide conflict with existing controller. Then on admin product for id 250 > tab Design, select "Specific Product - cool offer" as Layout override.

This would give you the trick. Custom product template and design layout whenever you need for future custom product.
This approach is not quite good if I want to have different layouts for products from different categories. I'll have to create too many new product templates and manage them in code instead of admin panel.

Newbie

Posts

Joined
Wed Aug 17, 2016 4:29 pm

Post by Pedanov » Wed Aug 17, 2016 9:20 pm

qahar wrote:Now to apply custom Design Layout, you can create new Layout with empty route. Lets say that you create layout with name "Specific Product - cool offer", make sure the route is empty; to avoide conflict with existing controller. Then on admin product for id 250 > tab Design, select "Specific Product - cool offer" as Layout override.

This would give you the trick. Custom product template and design layout whenever you need for future custom product.
And where should I define the layout template file (.tpl) in this case?

Newbie

Posts

Joined
Wed Aug 17, 2016 4:29 pm

Post by qahar » Thu Aug 18, 2016 3:35 pm

Pedanov wrote:This approach is not quite good if I want to have different layouts for products from different categories. I'll have to create too many new product templates and manage them in code instead of admin panel.
Pedanov wrote:And where should I define the layout template file (.tpl) in this case?
You misunderstand it, Template and Design Layout is two different thing.
Design Layout populated by route or specific item when it's overrided; which is configured in admin.
While template file is defined in controller file.

Old tutorial for layout, but still apply for OpenCart 2.

User avatar
Expert Member

Posts

Joined
Tue Jun 29, 2010 10:24 pm
Location - Indonesia

Post by Pedanov » Tue Aug 23, 2016 8:41 pm

qahar wrote: Design Layout populated by route or specific item when it's overrided; which is configured in admin.
So there is no way to use different template files for different products? I mean without any modules or extensions.

Newbie

Posts

Joined
Wed Aug 17, 2016 4:29 pm
Who is online

Users browsing this forum: No registered users and 5 guests