Joomla 2.5 -- get sef url from itemid - joomla

How can I find the correct SEF url of an Menuitem, based on its ID?
$link = JRoute::_( $menus->getItem( $id )->link.'&lang='.$languages[ $code ]->sef, true );
delivers something like:
http://localhost/<path>/component/content/?view=featured
but what i want is the url generated from the menu, which is:
http://localhost/<path>/<lang-sef-code>/<path>
How can I achive this?

I finally found the solution for the problem I had.
$languages = JLanguageHelper::getLanguages('lang_code');
foreach ($associations as $code => $id ) {
item = $menus->getItem( $id );
$link = JRoute::_( 'index.php?lang='.$languages[ $code ]->sef.'&Itemid='.$item->id );
}
this delivers:
http://localhost/<lang>/<path-to-link>
I noticed that when dealing with SEF URLs than the order in which vars are given to the JRoute::_() method are decisive. I hope to help somebody who got some problems with this too.
Greetings
philipp

Related

Magento: Get Customer ID in Admin

In Magento 1.9 I want to get the customerId of opened customer in Mage_Adminhtml_Block_Customer_Edit_Tab_View.
I tried like this:
$customer_session = Mage::getSingleton('customer/session'); //I saw here in Stackoverflow this
$customer = $customer_session->getCustomer();
$customerID = $customer->getId();
But I got a null.
I also tried with $this->getId() and Mage::helper('customer')->getId(), but neither worked.
How do achieve this?
For admin we should use 'admin/session' for 'customer/session'
$user = Mage::getSingleton('admin/session');
$userId = $user->getUser()->getUserId();
$userEmail = $user->getUser()->getEmail();
I figured out that the correct way is:
$customerID = $this->getCustomer()->getId();

Add SKU to end of Magento URL

I have searched for days trying to find a way of doing this but cannot find an answer, hopefully someone here can help me.
Essentially we want to insert the SKU into the product page url
Example
SKU = ST500DM002
So
domain.co.uk/seagate-barracuda-500gb-sata3.html
Would become
domain.co.uk/seagate-barracuda-500gb-sata3-ST500DM002.html
I had a look at this How to customize product URL?
It kind of works but it creates a path like this http://www.domain. co.uk/catalog/product/ST500DM002/seagate-barracuda-500gb-sata3.html rather than add it onto the end
Also we have a lot of SKUs with # in them which causes problems and throws 404
Is there a way to programmatically add the SKU and strip out #
Thanks
Copy the code and paste it to the file "script.php" and move it to magento root folder and try to run it in browser by using the following url,
http://localhost.com/script.php
<?php
require 'app/Mage.php';
Mage::app();
$amount = 0;
$model = Mage::getModel('catalog/product');
$products = $model->getCollection();
foreach ($products as $product) {
$model->load($product->getId());
$urlkey = $product->getUrlKey();
$sku = $product->getSku();
$product->setUrlKey($urlkey."/".$sku)->save();
set_time_limit();
$amount++;
}
When you run the file, All the urls will be rewritten with the new url keys.
You can find updated keys everywhere.
Create observer to catch event catalog_product_save_after_handler
$product = $observer->getProduct();
$urlkey = $product->getUrlKey();
$sku = $product->getSku();
if(substr($urlkey, strlen($sku) * (-1)) != $sku){
$product->setUrlKey($urlkey."/".$sku)->save();
}

Customize dynamic URL.change ?id to name

I'm using codeigniter for make dynamic website. In news page, I use url method like: http://test.com/test/test/news_details?id=27
how can i change my url to put news title within ?id=27
example:
http://test.com/test/test/news_details/pass-this-url
"pass-this-url" refer to id=27.
Best Regards.
Use routes : http://www.codeigniter.com/user_guide/general/routing.html
In your case it will be something like this :
$route['pass-this-url'] = "test/test/news_details?id=27";
So http://www.example.com/pass-this-url will be understand by codeigniter as http://www.example.com/test/test/news_details?id=27
however, if you want to make it dynamic, you will have to call your db :
require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get( 'news' );
$result = $query->result();
foreach( $result as $row )
{
$route[$row->title] = "test/test/news_details?id=". $row->id;
//We suppose here that your title is URL friendly.
}
Go to application/config/config.php and check this:
$config['enable_query_strings'] = FALSE;

magento - How to get product meta title?

I use following code to get the current product meta title, but it doesn't work.
$curr_prod = Mage::registry('current_product');
$meta_title = $curr_prod->getMetaTitle();
echo $meta_title;
I can get the correct $curr_prod object, but I don't know which method I should use to get the meta title of current product, anyone can help on this?
Thanks!
$title = $product->getMetaTitle();
if ($title) {
$headBlock->setTitle($title.' - '. $product->getFinalPrice());
}
try the code it might work
Set Meta information(Title, Keyword, Description) on Controller Action. Fetch meta information from product data and set the values for layout head block.
$_product = Mage::getModel('catalog/product')->load($id);
if($_product){
$title = $_product->getMetaTitle();
$keyword = $_product->getMetaKeyword();
$description = $_product->getMetaDescription();
}
$this->loadlayout();
$title = $title!=""?$title:$this->getLayout()->getBlock('head')->getTitle();
$keyword = $keyword!=""?$keyword:$this->getLayout()->getBlock('head')->getKeywords();
$description = $description!=""?$description:$this->getLayout()->getBlock('head')->getDescription();
$this->getLayout()->getBlock('head')->setTitle($title )
->setKeywords($keyword)
->setDescription($description);
$this->renderlayout();

Code to display articles from category ID?

What would be the code to display articles from category (specified by ID)?
In Wordpress this is fairly easy by doing:
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_title();
endwhile;
?>
I'm looking for similar code for Joomla.
$db = JFactory::getDbo();
$id = 50;//example
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__content');
$query->where('catid="'.$id.'"');
$db->setQuery((string)$query);
$res = $db->loadObjectList();
foreach($res as $r){
echo '<h3>'.$r->title.'</h3>';
echo $r->introtext;
}
You can use next code template:
$categoryId = $[category id];
require_once("components/com_content/models/category.php");
$category = new ContentModelCategory();
$category->hit($categoryId);
$articles = $category->getItems();
print_r($articles);
There is no direct code for getting this in joomla like word press.
If you want to check the code you can check the code for achieving this by following path.
components/com_content/view/category/view.html.php
and
components/com_content/view/category/tmpl/blog.php
According to my Guess your requirement is to display the articles from same category.
then in joomla you dont need to edit in any code.
for achieving this you can simply create a menu.
and menu layout type should be category blog and Choose your category from the right side category options.
This will get the complete article from that category.
If you want to manage it in your style you can add style based on blog.php file .
Hope this will help you..
Try this:
$articles = JFactory::getDBO()->setQuery("SELECT * FROM #__content WHERE catid = '21'")->loadObjectList();
Of course replace '21' with id of your category.
This is quite easy in Joomla. You can find the code in /components/com_content/models/category.php under getItems()
Below is an example
$model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
$model->setState('params', JFactory::getApplication()->getParams());
$model->setState('filter.category_id', $category_id);
$articles = $model->getItems();

Resources