I am having difficulty getting a variable to display on the product page in Opencart. I have added a SQL query to catalog/model/catalog/product.php file as below:
public function getLargeItems($product_id) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "oc_product_attribute WHERE product_id = '" . (int)$product_id . "' AND attribute_id=17 AND (text LIKE '10lt' OR text LIKE '30lt' OR text LIKE '50lt')");
return $query->row;
}
I then added the following to the catalog/controller/product/product.php file:
$large_items = $this->model_catalog_product->getLargeItems($product_id);
Finally I added the following to the /catalog/view/theme/mytheme/template/product/product.tpl page:
<?php if ($large_items) { ?>
<h1>Tree</h1>
<?php } ?>
And it returns Undefined variable: large_items in /catalog/view/theme/mytheme/template/product/product.tpl
I am at a bit of a loss now. Is anyone able to see by my code why its unable to pull the variable from the controller?
I've tried simplifying it by adding $large_items = "Hello" to the controller to see if I can echo this on the view page however it still cant see the variable.
The version of Opencart is 2.3.0.2
Thank you for your time.
I managed to resolve this by doing two things:
Removing oc_ from the query as the prefix is already defined, I was however only able to notice this after discovering that controller changes dont get picked up without clearing the cache in the admin panel > modifications > click the blue refresh button.
I was then able to see the broken query in the php logs.
The issue was my shoddy code, but I hope that the cache refresh information is useful to others if they get stuck.
Thanks
First you must remove oc_ from "oc_product_attribute WHERE product_id =... because prefix is already defined like " . DB_PREFIX . ".
Next you not properly defined $large_items in your controller. must be:
$data['large_items'] = $this->model_catalog_product->getLargeItems($product_id);
then you can get it on your .tpl like $large_items or in .twig {{ large_items }}
in twig your code should be:
{% if large_items %}
<h1>Tree</h1>
{% endif %}
Good luck
Related
I'd like to get all my categories in my header (header.tpl) of my Prestashop theme but it seems not working well...
My code header.tpl :
{$childCategories= Category::getChildren(0, 0, $active = true, $id_shop = false);}
{printf($childCategories)}
Issue : Error 500
The code you have written is not valid for smarty. Prestashop uses Smarty to render templates. Please, take a look to the rules if you want to avoid troubles like this. Also, you have a lot of examples in the default theme of Prestashop to learn more about coding in Smarty.
The right code would be:
{assign var='childCategories' value=Category::getChildren(1, 1, true, false)}
Arguments to be passed
$id_parent : The parent category id. The root id category is 1 and the home id category is 2.
$id_lang: The id language. You can check it in the localization area to get the id of a language. If you have enable multiple languages, you could use the $language variable to get the id. List of global variables in Prestashop.
$active: Return only the active caregories.
$id_shop: The id of a shop if you have got multiple shops in an instalation.
Printing variables to debug
If you want debug or see the variables, you could try the following snippets of code:
{* Print only the variable $childCategories *}
{$childCategories|var_dump}
or:
{* Print all variables *}
{debug}
A template header.tpl comes from FrontController.php function displayHeader().
Category doesn't exists there, because header.tpl is a comprehensive template used in all pages.
There are several hooks which you can use adding content there: displayHeader, displayTop, displayLeftColumn, displayRightColumn, displayFooter.
You can add all categories in any module and one of these hooks like that:
$category = new Category((int)Configuration::get('PS_HOME_CATEGORY'), $this->context->language->id);
$sub_categories = $category->getSubCategories($this->context->language->id);
// else code
i'm beginner in php so i want to ask this question
I read about url in Codeigniter and know url is include controller name and then method name but i didn't know what's question mark means yet ?
for example
where form post data if the url after click on submit button is
http://localhost/code/forums/topic/25674318?addpost=Publish
i'm using Codigniter may i use this url to post submitted data to model i mean insert that data to database.
This is the symbol ? which starts the begining of query in url. And after that you can use & symbol
<?php echo $this->input->get('addpost');?>
http://www.codeigniter.com/user_guide/libraries/input.html?highlight=get
Or
<?php echo $_GET['addpost'];?>
http://php.net/manual/en/reserved.variables.get.php
And then should return Publish
The fist query in url good to have ? then any other query you can use &
Example: http://www.demo.com/example/?token=123456&something=test
$var1 = 123456;
$var2 = 'test';
site_url('example/'. '?token=' . $var1 . '&somthing=' . $var2);
Config
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-&=?';
i am currently developing one joomla site which contains virtuemart and my written component which is working quite well, but the problem shows up when i try to search for products using virtuemart search in example when i search for 12/4 name it replaces 12/4 name with 124 name and displays no results.
So the question is where is that sql-query that loads those items so i can modify it?
or is there another plug-in or something that works with / or is there somekind of fix to this problem?
So after a while i finnaly fixed this issue. My fix is not weary good beacose i didn't find where exactly virtuemart component removes slash from search query so i just did like this......
As frontend uses modules who are located at admin/components/com_virtuemart/modules i had to edit module named product. And in the function named sortSearchListQuery2 i added some code
if ($useCore) {
// if ( $this->keyword !== "0" and $group ===false) {
if (!empty($this->keyword) and $this->keyword !== '' and $group === FALSE) {
$this->setKeyWord($_GET['keyword']);//Added this line!
//$keyword = trim(preg_replace('/\s+/', '%', $keyword), '%');
$keyword = '"%' . $this->_db->getEscaped ($this->keyword, TRUE) . '%"';
//var_dump($keyword,$this->keyword,$_GET['keyword']); debug_zval_dump($keyword); debug_print_backtrace(); die();
And on frontend view named Category in view.html.php i replaced $keyword=vmRequest::uword('keyword', '', ' ') with $keyword = $_GET['keyword']
And that is my approach to fix this problem!
How can I show the full text of a leading article (even if it contains a read more) on a Category Blog Layout?
Is there a back-end setting I can change or which PHP file do I need to overwrite? I only want the leading article's fulltext to show, intro articles should still display the readmore with only intro text.
Thanks in advance.
You don't specify which version of Joomla! so this reply is based on 2.5.x
You will need to create a layout override for com_content, specifically for the category view tmpl file blog.php or blog_item.php.
If your template already has an override installed for com_content you will find it at this location:
/templates/your-template/html/com_content/category/
If not you can copy the original files from your Joomla! 2.5's component directory at /components/com_content/views/category/tmpl/
In that directory you will find a series of files starting with blog and ending with .php - you can override 1 or all of these by coping them to the first path listed above. N.B. Only copy the files you need to change Joomla! is smart enough to look in the default directory if it doesn't find a version in the overrides location.
The default Joomla! 2.5 installation has these blog files:
blog_children.php
blog_item.php
blog_links.php
blog.php
Basically blog.php is the main file and it includes the sub-layouts like blog_item.php as required.
If you're working from the default Joomla! 2.5 files, to achieve your goal you will probably have to override blog.php and blog_item.php and find a way to check whether you are in the leading item. Once you know that your in a leading item you will then want to echo out the full text of the item.
blog_item.php normally just outputs the intro text with a line like this:
<?php echo $this->item->introtext; ?>
You'll want to have the full text echo'd so after you've wrapped the line above in an if to check if you're doing the leading item your output line will look something like this:
<?php echo $this->item->introtext.$this->item->fulltext; ?>
Note: I've haven't check this works it's just speculation and relies on the $item having the full row from the #__content table.
So the articles full text isn't added to the article item in a standard install i.e. you can simply echo $this->item->fulltext.
There are two ways to get the full text.
The simplest & first is to modify the core file at /components/com_content/models/articles.php starting with the first $query->select() in the function getListQuery() which starts on/near line 153. Add ' a.fulltext,' after the a.introtext and before the single apostrophe, so the line looks like this:
function getListQuery()
{
// Create a new query object.
$db = $this->getDbo();
$query = $db->getQuery(true);
// Select the required fields from the table.
$query->select(
$this->getState(
'list.select',
'a.id, a.title, a.alias, a.title_alias, a.introtext, a.fulltext, ' .
'a.checked_out, a.checked_out_time, ' .
The second way, and the Joomla! update proof way (& therefore probably the better way) is to load the entire article in the override (ugly but it's not a hack) and concatenate the fulltext to the introtext of the first article.
To do this you simply use getTable to load the article using the $this->item->id in blog.php for the first leading item, so at about line 54, between the <?php and the $this->item = &$item; put:
if($leadingcount == 0) {
$contentTable = JTable::getInstance('Content');
if($contentTable->load($item->id)) {
$item->introtext .= $contentTable->fulltext;
}
}
N.B. The if just limits it to the first leading article, if you remove the if it will attach the full text for all leading articles.
This works on my dev installation of Joomla! 2.5.6 so you should be able to do it as well.
Considering the two answers above, there are three changes required to blog.php and blog_item.php. This works in Joomla 3.1 The best is to copy these files to the template override
copy blog.php and blog_item.php
from components/com_content/views/category/tmpl/
to [my template]/html/com_content/category/
In blog.php in the loop for the leading items add one single line:
<?php foreach ($this->lead_items as &$item) : ?>
<div class="leading-<?php echo ...?>">
<?php
$this->item = &$item;
$this->item->leadingItem = true; //ADD THIS LINE !!!
echo $this->loadTemplate('item');
?>
</div>
<div class="clearfix"></div>
<?php
$leadingcount++;
?>
<?php endforeach; ?>
In the blog_item.php replace "echo $this->item->introtext;" with
if ($this->item->leadingItem){
//this is a leading article - show full text and remove "Read more..." button
$itemID = $this->item->id;
$db =& JFactory::getDBO();
$query = "SELECT `fulltext` FROM `#__content` WHERE `id` =" . $itemID;
$db->setQuery($query);
$fulltext = $db->loadResult();
echo $this->item->introtext . $fulltext;
}else{
echo $this->item->introtext;
}
Finally in the blog_item.php do not to show the "Read more..." button at the bottom of each article
Edit line
if ($params->get('show_readmore') && $this->item->readmore)
into
if ($params->get('show_readmore') && $this->item->readmore &&
!$this->item->leadingItem)
Joomla 2.5.x, in your template blog_item.php:
$itemID = $this->item->id;
$db =& JFactory::getDBO();
$query = "
SELECT `fulltext`
FROM `#__content`
WHERE `id` = $itemID;
";
$db->setQuery($query);
$fulltext = $db->loadResult();
and you can use the $fulltext string variable.
How can I read the section a certain URI belongs to?
I want to enhance the mod_breadcrumb to put section and category into the HTML. JApplication->getPathway() returns a JPathway which basically holds an assiciative array combining a name and an URL (as $list[]->name and $list[]->link). I think, it should be possible to get the section and category from a link, but don't know how.
A starting point could be the parsing into JURI-Object, but from there I don't know how get get further. Any ideas?
Pretty straight forward...
I assume you want to add category and section for the article and not your custom component.
Check if requested current URL is for article. If it is for article you know the article ID, use this article Id to go database and get catid from #__content, Use this cat_id to go to #__categories and get section (this is section id), go to #__sections to get the proper section name. All this can be done in 1 sql statement.
$breadcrumbs =& JFactory::getApplication()->getPathway();
$breadcrumbs->addItem("SECTION_NAME", JRoute::_("index.php?option=com_content&view=section&id=SECTION_ID"));
$breadcrumbs->addItem("CATEGOY_NAME", JRoute::_("index.php?option=com_content&view=category&id=CATEGORY_ID"));
$breadcrumbs->addItem("Article");
Alternatively, if you know the URL from the breadcrumb item. You can parse it and get IDS. The trick here is not to get the default URI object by JFactory::getURI() because things will get ugly, use JFactory::getURI('YOU_URI_NAME').
<?php
// You need to get Your own uri, you do not want to modify default URI
// because this will messup a lot of things
$uri = JFactory::getURI('MyCustomURI');
// Test # 1 [ID = SECTION_ID]
$url = "index.php?option=com_content&view=section&id=SECTION_ID";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');
// Test # 2 [ID = 123]
$url = "index.php?option=com_content&view=section&id=123";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');
?>