Display categories - smarty

I have my website working on social engine, I have a Main page named as articles.tpl, and in that I have a sub-page named as article.tpl, article.tpl is used to show the entire article, and articles.tpl is used to show topics of articles not the content,
My concern is with articles.tpl(pretty confusing articles and article)
In articles.tpl, 3 articles title, author of article and date is displayed, but I also want to display the category of that article,
For that I tried the following code,
$article_category = "";
$parent_category = "";
$article_category_query = $database->database_query("SELECT articlecat_id, articlecat_title, ".
"articlecat_dependency FROM se_articlecats WHERE articlecat_id='".
$rc_article->article_info[article_articlecat_id]."' LIMIT 1");
if($database->database_num_rows($article_category_query) == 1) {
$article_category_info = $database->database_fetch_assoc($article_category_query);
$article_category = $article_category_info[articlecat_title];
if($article_category_info[articlecat_dependency] != 0) {
$parent_category = $database->database_fetch_assoc(
$database->database_query("SELECT articlecat_id, articlecat_title".
" FROM se_articlecats WHERE articlecat_id='".
$article_category_info[articlecat_dependency]."' LIMIT 1"));
}
}
$smarty->assign('article_category', $article_category);
and in articles.tpl file I called it in this way
<span class="tahoma11_blue">| {$article_category}</span>
But when I check I get nothing, that space is blank, I am just able to see
|
How can I display the category?

i think you have to change
$smarty->assign('categories', $categories);
to
$smarty->assign('article_category', $article_category);
(in your example, you assign the (apparently empty) php variable $categories to the smarty variable categories. then, in the template, you use a smarty variable article_category - which never got assigned)

Related

Editing product page with dimsav/laravel-translatable

im using a package called Laravel-Translatable
but is giving me more problemns that i expect, mainly with quite simple tasks. For example i have a list of all records (products), and each of them haves 2 languages translated (en,es). But now i need to edit the product information to put in the inputs fields, and for this i wish in my edit page get all the translated details (title, description), but for some reason, is returning me only one language, it doesnt return all the translated details from a specific product:
ex: return Product::where('id', '2')->get();
Somebody uses this package?
Just read the documentation. Therein is all that you need.
Some example:
$product = Product::where('id', 2)->get();
$product->translate('de')->title = "Germany title";
$product->translateOrNew('pl')->title = "Polish title";
//Shortcut
$product->{'title:pl'} = 'lorem ipsum';
$product->save(); //It Will save all translations and main model

Get categories in a Prestashop theme

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

Removing id variables in joomla v1.5 router

I have custom Joomla(v1.5) component and currently working with component's router. The problem is I can't remove id numbers from SEF url. I get:
http://myaddress.com/componentalias/17-city-alias/130-item-alias
What I want to get:
http://myaddress.com/componentalias/city-alias/item-alias
Take a look at router.php methods below:
function ComponentnameBuildRoute(&$query) {
$segments = array();
if(isset($query['city_id'])){
$segments[] = $query['city_id'];
unset($query['city_id']);
}
if(isset($query['item_id'])){
$segments[] = $query['item_id'];
unset($query['item_id']);
}
if(isset($query['task'])){
switch($query['task']){
case 'pay':
$segments[] = JText::_('payment');
unset($query['task']);
break;
}
}
unset($query['view']);
return $segments;
}
/*
* Function to convert a SEF URL back to a system URL
*/
function ComponentnameParseRoute($segments) {
$var = array();
if(isset($segments[0])){
$cityData = explode(':',$segments[0]);
if(isset($cityData[0])){
$vars['city_id'] = $cityData[0];
}
}
if(isset($segments[1])){
$itemData = explode(':',$segments[1]);
if(isset($itemData[0])){
$vars['item_id'] = $itemData[0];
}
}
if(isset($segments[2])){
switch($segments[2]){
case JText::_('payment'):
$vars['task'] = 'pay';
break;
}
}
return $vars;
}
Any ideas? Your help would be appreciated.
The best place to start with your router.php file is reading this article (it's a bit dated but still good) and then reviewing the com_content's router.php file (components/com_content/router.php). You will notice that articles do achieve what you want so best to look at working code and got from there.
Longer answer:
You can only get rid of the item ID variables in a path to a content element if a menu item exists that points directly to the item, otherwise there is no way to find the item.
SEF URLs in Joomla! 1.5 etc are made from the alias' of the individual elements
eg. If I have this menu structure:
Recipes (The menu)
-- Seafood (<-- Category blog where category alias is `seafood` )
-- Grilled Snapper (<-- Recipe Item where item alias is `grilled-snapper` )
-- 'Other category' (<-- Another Category blog )
Full ID Removal
In the case where you're building the SEF URL for a recipe you can build the route by looking for the menu item it might appear in by getting the site menu $menu = &JSite::getMenu(); and comparing the query id in the current menu item against id value in the $query array passed in.
If you have a match you can build the segments up using the alias from the menu path and the recipe alias. (And reverse the process in your ParseRoute($segments) method).
So, from this example above you could build a SEF URL to the Grilled Snapper recipe that looks something like: recipes/seafood/grilled-snapper.
Partial ID Removal
Now say you also have another recipe (e.g. 'Garlic Prawns' alias garlic-prawns) that isn't directly linked to a menu but will appear in the 'Seafood' category blog page. In this situation you would end up with recipes/seafood/2:garlic-prawns
If you don't have a match (like the Garlic Prawns), you can build up partial match if your component has list views like category blogs or in our example Recipe category pages... Essentially in this case you look at the current menu item and determine if it's a list/category view that would contain the content item.
If it is then the path to the category/list view form you initial segments, but as there is no menu item for the article you will still have to use the ID of the item in the last element of the URL.
No Menu Item for content item or a list/category view
When the content item is being linked to directly (e.g. from an article, module or search result) and there are no menu items that point to it or could contain it then you can still create a URL without id's in it but you will be providing the path in the form of direct component access URL.
eg. /component/recipes/recipe/ice-cream-sundae where recipes is the name of the component, recipe is the view and ice-cream-sundae is the alias of the article.

How to set default selected options in magento product detail page

I have requirement in which i am getting product ID from external Application with product super_attribute options like color,size. I am getting all those and i can do the add to cart option.
But here my actual requirement is to select the requested options by customer and redirect them to product detail page in magento so that here they can still enter optional text to print. So i need to set the requested options in detail page and redirect them to product detail page instead of adding it to cart. They will enter more details and then they will do addtocart manually.
How can i set the selected options while loading itself.
Please help me.
Thankfully this is already built in to most themes. You need to know the ID of both attributes and values from the Simple product that is included in the Configurable product. I've only seen it work with Configurable types so that might be a limitation.
For each attribute make a key=value pair
Combine the attributes as a query string, eg. "123=345&678=890"
Append the string as a hash fragment (not as a query).
This is an example from the official demo, note that the first option needs to be selected for the second to work so it has two key/value pairs.
http://demo.magentocommerce.com/zolof-the-rock-and-roll-destroyer-lol-cat-t-shirt-126.html#525=99&272=22
Rough Magento2 example add the below for pre-selecting the custom options by name=value in:
/www/mysite/app/design/frontend/mycompany/mytheme/Magento_Catalog/templates/product/view/options.phtml
The below code looks at the label of the option and the text value of the select. And depends on your theme structure. Example below for Luma.
It expects the following format in the url
product.html?SelectLabel=OptionValue&SelectLabel=OptionValue
This does not account for multi language etc.. you could easily adapt it to instead look for the select id and option id which would be more accurate replacing
$(label).parents().eq(1).find('select option:contains('+arr[k]+')').attr('selected', true);
with (untested)
$("#"+k+" option[id='"+arr[k]+"']").attr("selected", "selected");
<script>
require(['jquery'],function($){
$(document).ready(function(){
function getJsonFromUrl() {
var query = location.search.substr(1);
var result = {};
query.split("&").forEach(function(part) {
var item = part.split("=");
result[item[0]] = decodeURIComponent(item[1]);
});
return result;
}
var arr = getJsonFromUrl();
for (var k in arr){
if (arr.hasOwnProperty(k)) {
//alert("Key is " + k + ", value is" + arr[k]);
var label = $('.product-options-wrapper').find("span:contains('"+k+"')");
$(label).parents().eq(1).find('select option:contains('+arr[k]+')').attr('selected', true);
}
}
});
});
</script>

Get Joomla! category from an URL

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');
?>

Resources