fetch category details query in magento - magento

I need a query which displays Category Name,Category url & value of the Custom catgory attribute in Magento
any Guess...
Thanks

$category = Mage::getModel('catalog/category')->load($my_category_id);
echo $category->getName();
echo $category->getUrlKey();
Something like this? I am not sure what you mean by custom category attribute and I don't know what a category value is though.

$categories = $_product->getCategoryIds();
foreach($categories as $k => $_category_id):
$_category = Mage::getModel('catalog/category')->load($_category_id)
echo $_category->getUrl();
echo $_category->getName();
echo $category->getUrlKey();
endforeach;

lets say your custom attribute is "custom_attribute", then do this:
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToFilter('custom_attribute', 'value_here');
foreach ($categories as $category){
echo $category->getUrl();
echo $category->getName();
}
Hope this helps.

Related

Magento - Display product count views?

I am using magento 1.9 and i am trying to display the views of the product in the product view page.
Here is what code i am using right now but it's not displaying anything:
<?php $_helper = $this->helper('catalog/output'); ?>
<?php $_product = $this->getProduct(); ?>
<?PHP echo $_product->getViews();?>
Is it possible to display the number of the views of the displayed product or where i am doing wrong.
Thanks in advance!
You could create an observer for catalog_product_load_after and add views to the $_data array with
$product = $observer->getEvent()->getProduct();
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('core_read');
$select = $read->select()
->from(
$resource->getTableName('log/url_info_table'),
'COUNT(*) views'
)
->where('url LIKE "%catalog/product/view/id/' . $product->getId() . '"');
$result = $read->query($select)->fetch();
$product->setData('views', $result['views']);

How to get correct product url for multistore magento?

I am running following code in Magento root to get the product urls
<?php
require_once('app/Mage.php');
umask(0);
Mage::app(1);
$collection = Mage::getModel('catalog/product')
->setStoreId(1)
->getCollection();
foreach( $collection as $product )
{
echo $product->getProductUrl();
echo "<br>";
}
?>
I am getting product urls like http://example.com/catalog/product/view/id/5/ , But these urls are invalid.
The product urls are as following in front end http://example.com/product.html
How do I get the correct product urls? I have multi store Magento set-up.
You need to get store url for each product separetly. In other words, you need to use something like this:
$collection = Mage::getModel('catalog/product')
->getCollection();
foreach( $collection as $product )
{
echo $product->setStoreId(5)->getProductUrl();
echo "<br>";
}

Product collection by attriubte base in magento

I want to get the product collection in magento.For that i use some code but i think this code is not what i need.I want to get collection on attribute base.I got some products but it didn't match to those products that are filters from advance results for that attribute.Means different results from my collection and advance search results.Also the product url is not valid one.May be someone know where is the problem ? thanks in advance.My code is :
<?php $collection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToSelect('*')
->addFieldToFilter(array(
array('attribute'=>'manufacturer','eq'=>'23'),
));
foreach ($collection as $product) {
?>
<div class="brand_name">
<p>Audi</p>
<?php echo substr($product->getName(),0,10);?>
</div>
<?php } ?>
You have used 2 array in the field to filter. Try with one.
<?php $collection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToSelect('*')
->addFieldToFilter('attribute'=>'manufacturer','eq'=>'23');
It should work correctly:
$attrToSelect = '*'; // or Mage::getSingleton('catalog/config')->getProductAttributes();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect($attrToSelect)
->addAttributeToFilter('manufacturer', 23)
;
foreach ($collection as $product) {
echo $product->getProductUrl();
}
Also check in admin - Catalog->Attributes->Manage Attributes - Used in Product Listing set Yes.
please use addAttributeToFilter('manufucture',23)
instead of
adfieldtofilter

How to make second root category navigation in magento?

Other than the normal navigation I get when I add subcategories to the main root category, I want to be able to make a new root category, assign subcategories to it and have it display as a separate menu.
Is this possible?
May this can help you :Link 1Link 2
To retrieve another root category
<?php
echo '<pre>';
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id); // where $id will be the known category id
if(!empty($categories))
{
echo 'category name->'.$categories->getName(); //get category name
echo '<br>';
echo 'category image url->'.$categories->getImageUrl(); //get category image url
echo '<br>';
echo 'category url path->'.$categories->getUrlPath(); //get category url path
echo '<br>';
}
?>
now $id=9; is my new root category id.
To retrieve sub categories of these new root category ($id=9;) below is the following reference code.Customize it according to your requirements.
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php foreach($categories as $category): ?>
<?php $subcategories = $category->getChildren() ?>
<?php foreach($subcategories as $subcategory): ?>
<?php $subsubcategories = $subcategory->getChildren() ?>
<?php foreach($subsubcategories as $subsubcategory): ?>
<?php endforeach; ?><!-- end foreach subsubcategories -->
<?php endforeach; ?><!-- end foreach subcategories -->
<?php endforeach; ?><!-- end foreach categories -->
In an ideal world you would have found the answer by now, but in case you didn't I modified Nikhil's answer to work for me to basically do what you describe, minus any convenience at all...
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id);
if(!empty($categories))
{
$cats = explode(",", $categories->getChildren());
foreach($cats AS $c)
{
$cat = $catagory_model->load(trim($c));
echo ''.$cat->getName().'';
}
}
I'm just pasting what I used. The reality is you will have to build the html to make this do whatever you want it to do. If you have subcategories within your loop, you will have to run another fetch in the foreach part.
I am not familiar with magento enough to know what methods you can run on the $cat object. I did a print_r($cat) to examine the object and made a lucky guess that getUrlKey would be available.
Magento... pfft! you'd think ebay would have higher standards than this.

show count of only configurable products of current category

i want to show count of only configurable products of current category on category page. for that I have written following code...
<?php $cate = Mage::registry('current_category')->getName();
$total=0;
$category = Mage::registry('current_category');
$products = $category->getProductCollection();
foreach ( $products as $_product )
if ($_product->isConfigurable())
{
$total++;
}
echo $cate."(".$total.")"; ?>
my problem is the code is showing the total count of configurable products of all child categories... can anyone help me with this?
try this
<?php
$cate = Mage::registry('current_category')->getName();
$total = 0;
$category = Mage::registry('current_category');
$products = $category->getProductCollection();
foreach ( $products as $_product ){
if ($_product->getType_id()=="configurable"){
$total++;
}
}
echo $cate."(".$total.")";
?>
$category = Mage::registry('current_category');
$products = $category->getProductCollection()
->addAttributeToFilter('type_id', 'configurable');
$total = $products->getSize();
echo $this->__('%s (%d)', $category->getName(), $total);

Resources