Opencart Check if current page is product - opencart

Does OpenCart Version 2.3.0.2 have some methods to check in header or footer .tpl files that current page is product? Example if (is_product) { // do something }

You can try to check if your session has product. $this->session->data['product_id'] You can check it anywhere. You can write the product to the session... for example:
$this->session->data['product_id'] = /*your product_id*/;
And now you can retrieve this product anywhere.

No, But you can create it.
File:
catalog/controller/common/header.php
Find:
return $this->load->view('common/header', $data);
Add Before:
$data['is_product'] = isset($this->request->get['route']) && $this->request->get['route'] == 'product/product';
File:
catalog/view/theme/default/template/common/header.tpl
Add where you need:
<?php if (!empty($is_product)) { ?>
<p>This is the product page</p>
<?php } else { ?>
<p>This is not the product page</p>
<?php } ?>

Related

Check if a user is logged in or not on product page using OpenCart 2.3.X

I'm using Opencart 2.3.0.2 and would liek to show or hide the product code based on if the user is logged in.
in product.tpl I have tried changing:
<li><?php echo $text_model; ?> <?php echo $model; ?></li>
to
<?php if ($logged) { ?>
<li><?php echo $text_model; ?> <?php echo $model; ?></li>
<?php } ?>
but I get an error message stating that logged is not defined in product.tpl
I have also editing controller/common/header.php to include the following in various places with no luck.
$data['logged'] = $this->customer->isLogged();
What am I doing wrong?
Thank you
You must declare logged in corresponding controller file. So you nee add $data['logged'] = $this->customer->isLogged();
to:
catalog/controller/product/product.php file.

Move "Submitted By" from node.tpl to page.tpl

I'm looking to move "Submitted By" info from node.tpl to page.tpl however when I add the following from node.tpl I get errors. Im assuming I dont have access to those variables, but would like to know how I can set up a pre-proccess to get it to display as it does in the node.tpl
<?php if ($display_submitted): ?>
<div class="submitted">
<?php print $submitted; ?>
</div>
<?php endif; ?>
You can either use a preprocess function in the template.php of your theme, as explained here:
https://drupal.stackexchange.com/questions/40222/how-can-i-print-node-authors-last-login-date-on-page-tpl-php
In your case it would look like this (tested on Drupal 7):
function yourtheme_preprocess_page(&$variables) {
$variables['author'] = "";
if (isset($variables['node']) && ($account = user_load($variables['node']->uid))) {
$variables['author'] = $account->name;
}
}
And then in your page.tpl.php use this:
Submitted by: <?php print $author; ?>
If you don’t want to touch any of your theme’s files, but you need to output the author’s name in another region as the node content, you could create a view (block display) containing the node author, and assign it to the region.
While normally done in node.tpl.php, if the page is a node view page, the $node variable is also available in page.tpl.php
You can then use something like:
if (isset($node)) {
// Check if display submitted variable is set for this node type
if (variable_get('node_submitted_'. $node->type, 0)) {
// Do stuff
}
}
An alternative approach would be adding the required logic instead to an implementation of
hook_preprocess_page
Bonus update: You can see the $node variable added to page.tpl.php in core template_preprocess_page
if ($node = menu_get_object()) {
$variables['node'] = $node;
}
In page.tpl.php:
<div class="submitted">
<?php echo format_date($node->created, 'custom','d.m.Y'); ?><br />
<?php echo 'by ' . $node->name; ?>
</div>

Use product tags for meta keywords in Opencart 1.5.3.1

I'd like to use my product tags as meta keywords on every product page. Can anyone help me with this code?
Thaks
Below are some tips and they are general but give a direction of the different approaches to do this. Step 4 is a bit obvious. I am sorry but this not a module request forum, your question I think is fair so this direction set should get you in the right direction.
1 - Open catalog/view/theme/default/product/product.tpl
And do a find and replace with the header echo:
<?php
$getKeysandReplace=$header;
$getKeysandReplace=str_replace("<meta name=", "");
?>
Understand? Or use a regex to delete the Meta Keyword and Meta Description from the $header string and replace them with your own on the product.tpl - just grab the tags echo in the below code.
<?php if ($tags) { ?>
<div class="tags"><b><?php echo $text_tags; ?></b>
<?php for ($i = 0; $i < count($tags); $i++) { ?>
<?php if ($i < (count($tags) - 1)) { ?>
<?php echo $tags[$i]['tag']; ?>,
<?php } else { ?>
<?php echo $tags[$i]['tag']; ?>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
2 - You can edit header.tpl and do a IF($GET or if page is product type page then display this Meta Tag or display the normal tags.
3 - You can do an SQL crob job that copies the product tags, and places them as product keywords in your database everyday.
4 - Make sure staff copy and paste the tags as keywords to in the admin panel - stating the obvious

how to display a message on cart and checkout page for particular category product?

lets say i have a category "test" and its product "t1". IF i add this product to cart, it must show some message "my custom message". Also on checkout page, how can i do the same on payment step. i.e check if it is from category test then display this message ?
what i tried so far on cart page is this before form on cart.phtml:
$_catCollection = $this->getItem()->getProduct()->getCategoryCollection();
foreach ($_catCollection as $_category) {
// do stuff with your Mage_Catalog_Model_Category
print_r($_category);
}
?>
But getting this
Fatal error: Call to a member function getProduct() on a non-object
Set up an attribute for each product that will contain your special message.
Then you can maybe do something along the lines of:
<?php foreach ($this->getItems() as $item) : ?>
<?php if ($item->getSpecialMessage) : ?>
<?php echo $item->getSpecialMessage ?>
<?php endif ?>
<?php endforeach; ?>

Add Date and Author to node.tpl.php

In the code below I'd like to show the created date, author and have them be linked but nothing for they do not show. I believe I need to be doing $node-> rather that row, haven't figured out the exact code. Or what if anything I need to change under Views in my Drupal 6 installation. Thanks in advance!
<?php if($node->type == 'blog'): ?>
<div class="blog-page">
<div class="title-post">
<div class="top-image">
<?php print $node->field_image[0]['view'] ?>
</div><!--TOP-IMAGE-->
<p>Posted on ?php $row['created'] ?>, by
<?php print $row['name'] ?></p>
</div>
<div class="content-page">
<?php print $node->content['body']['#value'] ?>
</div>
</div>
<?php else: ?>
<?php print $content ?>
<?php endif ?>
Drupal has a node object with tons of related information in it. Basically if you ever need to use information from it like author, date, title, etc, you can easily determine the code by printing the node object.
echo '<pre>';
print_r($node);
echo '</pre>';
lets say it outputted something like this for simplicity's sake:
stdClass {
nid = 3
content = stdClass {
raw = " ... "
clean = " ... "
}
}
To output those bits of information in your template, you'd write in the following way.
for a field with no subclass:
<?php print $node->nid ?>
for a field with a subclass:
<?php print $node->content['raw'] ?>
Does that make sense? after you get that down, you literally can figure out anything when programming drupal templates.
so, if you wanted to construct a url, you'd just chain it up:
<?php
$nid = $node->nid;
$uri = "some/path".$nid;
print $uri;
?>