magento - How to get product meta title? - magento

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();

Related

Magento getAttribute not working with listing product

echo $_product->getResource()->getAttribute($attribute)->getFrontend()->getValue($_product);
This code is not working fine in view.phtml it is not return first attribute code value.
when i write this code on view page, it is not showing first product attribute and all after first in loop are showing fine.
This is my all code
<?php
$productAttributeTh = array('Color','Item','Size');
$configurableProduct = Mage::getModel('catalog/product')->load($_product->getId());
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$configurableProduct);
foreach($childProducts as $child) {
$product_id = $child->getId();
$obj = Mage::getModel('catalog/product');
$_childProduct = $obj->load($product_id); // Enter your Product Id in $product_id
foreach ($productAttributeTh as $key => $productAttributeValue){
$productAttribute = $_childProduct->getResource()->getAttribute($productAttributeValue)->getFrontend()->getValue($_childProduct);
echo $productAttribute;
}
} ?>
You need to make sure your attribute is set to be used in list. Go to;
Catalog > Attributes > Manage Attributes
Find your attribute, and open it. Scroll down to the option 'used in product listing' and set it 'yes'. Save and then reindex attributes.
Try this.
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
Or
$attribute_value = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);
i hope this will help you.

Magento - getting category custom attribute value

How to get current custom category attribute value in product list view?
I'm trying like this
$attribute = Mage::getModel('catalog/category')->getAttributes();
And I see it's there but how to get it?
My custom attribue name is catalog_pdf
Also tryed in this way, but get nothing:
$attribute = Mage::getModel('catalog/category')->getAttribute('catalog_category','catalog_pdf');
This should work.
If you are int the product list then you should have the current category in
Mage::registry('current_category');
So do like this:
$category = Mage::registry('current_category');
if ($category){ //this is necessary in case you are in a product listing that is's not a category
$value = $category->getData('catalog_pdf');//catalog_pdf is the attribute code
//or
//$value = $category->getCatalogPdf();
}
This should work:
$id = $this->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getCode()->getId())->load($id);
echo $category->getData('catalog_pdf');
//or
echo $category->getCatalogPdf();
Edited to include missing get

Get custom Attribute

I'm wonder how can I get custom attribute,
My custom attribute call "tim_color"
I was try get by $_product->getAttributeText('tim_color');
after execute I get fatal error Call to a member function getAttributeText() on a non-object
when I'm used
$data['color'] = $product->getTim_color();
in the result i'm get id, but I need the name of atrribute, how can I resolve this problem
My code of script:
$mage_csv = new Varien_File_Csv(); //mage CSV
$products_model = Mage::getModel('catalog/product')->getCollection();; //get products model
$products_model ->addAttributeToSelect('*');
$products_row = array();
foreach ($products_model as $prod)
{
#print_r($prod);
$product = Mage::getModel('catalog/product')->load($prod->getId());
$data = array();
$data['id_product'] = $product->getId();
$data['color'] = $product->getTim_color();
$data['sku'] = $product->getSku();
$data['name'] = strip_tags($product->getName());
$data['description'] = trim(preg_replace('/\s+/', ' ', strip_tags($product->getDescription())));
$data['price'] = $product->getPrice();
$products_row[] = $data;
}
thx for help
Try,
$_product->getData(’tim_color’);
I hope you can get the attribute value by getAttributeText() also. Check out,
$_product = Mage::getModel('catalog/product')->load($item->getId()); //getting product
$_product->getAttributeText('tim_color'); //getting custom attribute value
Detailed discussions are here and here.

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();

Display category name in template

I am trying to display a category name in a module position.
I tried:
<?php echo $listing['Category']['title'];?>
It did not work.
I followed this link, but it shows the article title and I need the category one.
I'm working on Joomla 1.7.
Much more simple answer:
<?php echo $this->escape($this->item->category_title);?>
As per the posters comment in the OP:
<?php
$db = &JFactory::getDBO();
$id = JRequest::getString('id');
$db->setQuery('SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = '.$id);
$category = $db->loadResult();
echo $category;
?>
Travega is really close, his code works on pages, but not on category pages.
When you use $id = JRequest::getString('id'); on a category page (such as a category blog or list page) the id of the category is returned. This means we need more context of the id variable, in this case the 'view'.
Here is my modified version of travega's code:
function getCategoryName() {
//Modified from: http://stackoverflow.com/questions/8928967/joomla-display-catagory-name-in-template
$db = &JFactory::getDBO();
$id = JRequest::getString('id');
$view = JRequest::getString('view');
if ($view == 'category') {
$sql = "SELECT title FROM #__categories WHERE #__categories.id = $id";
} else {
$sql = "SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = $id";
}
$db->setQuery($sql);
$category = $db->loadResult();
return $category;
}
Other relevant info:
I've only tested this on Joomla 2.5.3 on cat blog and cat list pages. I've not tested it on anything other than com_content component. This means it probably won't work on weblink, contact, etc pages as you may again loose the context.

Resources