Post by OutlawPlz » Tue Aug 11, 2020 10:59 pm

Hi guys,

I'm on OpenCart 3.0.3.2. I'd like to check the current page layout name, in order to dynamically hide the content of a category page. So far I've tried to add a variable in the controller

Code: Select all

category.php
with no luck.

Code: Select all

$data['layouts'] = $this->model_catalog_category->getCategoryLayouts($category_id);
When I try to load the page i get the error: "Notice: Undefined property: Proxy::getCategoryLayouts in /mnt/dati/www/hosting/insology/lollipopshop.it/storage/modification/system/engine/action.php on line 79".

How may I check which is the layout of the current page?

Thanks,
Matteo.

Newbie

Posts

Joined
Tue Aug 11, 2020 10:44 pm

Post by cyclops12 » Wed Aug 12, 2020 12:36 am

better to use $category_id

Expert Member

Posts

Joined
Sun Sep 27, 2015 1:10 am

Post by letxobnav » Wed Aug 12, 2020 10:10 am

There is no function getCategoryLayouts($category_id) in catalog/model/catalog/category.php
There is getCategoryLayoutId($category_id) but that is useless to hide/show dynamic content.

Normally,modules which are defined in a layout are executed when that layout is used, no additional control there, it is a static definition.
If you want a module, defined there, not to be executed under certain conditions, you need to do that in the module itself.
In the module's index function you simply add as the first line:
if (your condition) return;
Then that module returns nothing.

To check what kind of page you are on you check the route as in
$this->request->get['route']
which would get you product/category or product/product or information/information etc.
to check for a specific page on a route you check if a certain id is set as well like
$this->request->get['product_id'] or $this->request->get['information_id']
for route $this->request->get['route'] = 'product/category' you check $this->request->get['path'] and if that has a _ in it you need to extract the last number to get the category id

so if you have module bestsellers defined in the layout for all product pages but do not want it to show for some products you would edit the bestsellers module first line after index():

Code: Select all

if (isset($this->request->get['route']) && $this->request->get['route'] == 'product/product') {
	$product_id_exclude = array(12,43,22);
	if (isset($this->request->get['product_id']) && in_array($this->request->get['product_id'],$product_id_exclude)) return;
}
Then again, if you have a module which is very dynamic in terms of when and what to show, I would suggest not to use the layout but simply control it via the controllers and call that module there based on conditions of when to show and give the module parameters to control what to show (like product id exclusion).

controller:

Code: Select all

$data['content'] = false;
if (your condition) {
	$setting = whatever you need and controller accepts
	$data['content'] = $this->load->controller('route_to_module_controller',$setting);
}
twig:

Code: Select all

{% if content %}{{ content}}{% endif %}

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by i.am.retailer » Fri Aug 14, 2020 1:05 pm

Add Function getLayoutName in model>>design>>layout.php

Code: Select all

public function getLayoutName($route) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "layout_route WHERE '" . $this->db->escape($route) . "' LIKE route AND store_id = '" . (int)$this->config->get('config_store_id') . "' ORDER BY route DESC LIMIT 1");

		if ($query->num_rows) {
			return  $query->row['name'];
		} else {
			return '';
		}
	}
In calatog>>controller>>common>>header.php

Code: Select all

               if (isset($this->request->get['route'])) {
                    $route = $this->request->get['route'];
                } else {
                    $route = 'common/home';
                }
                $layout = $this->model_design_layout->getLayoutName($route);
                $data['layout_name'] = $layout ;
In header.twig file

Code: Select all

  <p><?php echo $layout_name; ?></p>

New member

Posts

Joined
Thu Jun 04, 2020 2:10 pm

Post by letxobnav » Fri Aug 14, 2020 2:16 pm

you don't put php in a twig file.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by sw!tch » Fri Aug 14, 2020 3:34 pm

i.am.retailer wrote:
Fri Aug 14, 2020 1:05 pm
Add Function getLayoutName in model>>design>>layout.php

Code: Select all

public function getLayoutName($route) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "layout_route WHERE '" . $this->db->escape($route) . "' LIKE route AND store_id = '" . (int)$this->config->get('config_store_id') . "' ORDER BY route DESC LIMIT 1");

		if ($query->num_rows) {
			return  $query->row['name'];
		} else {
			return '';
		}
	}

Code: Select all

return  $query->row['name'];
???

Curious... Do you even look at the code before posting? You threadjack a detailed and informative post to provide a non-working solution.

I suggest you also review your query carefully and correct your post.

https://github.com/opencart/opencart/bl ... .sql#L1842

Again .. Users who come across this will try your solution and wonder why they have undefined index notice.

Please don't post half baked solutions just to get your post count up.

Full Stack Web Developer :: Send a PM for Custom Work.
Backup and learn how to recover before you make any changes!


Active Member

Posts

Joined
Sat Apr 28, 2012 2:32 pm
Location - USA
Who is online

Users browsing this forum: No registered users and 34 guests