magento get all products by attribute - php

I have displayed attribute filters in my catalog page. My client Requirement is, In some categories, attribute filter has no products that time i need to hide the empty attribute label. How can i achieve this Please help me.
"I need Product count for particular attribute that is shape,color and watts like that how can i get all products count by attribute, if i get count i will control the attribute label using that."
My code in catalog/layer/view.phtml:
<?php if($this->canShowBlock()): ?>
<?php // echo $this->getStateHtml() ?>
<?php if($this->canShowOptions()): ?>
<?php $_filters = $this->getFilters() ?>
<?php if($_filter->getItemsCount()):
<?php echo $this->__($_filter->getName()) ?>
<?php echo $_filter->getCount();?>
<?php echo $_filter->getHtml(); ?>
<?php endif;?>
<?php endif;?>
<?php endforeach; ?>

//Get product collection from "catalog/product" model
$collection = Mage::getModel('catalog/product')->getCollection();
//Fetch Most popular Featured products
$collection->addAttributeToSelect('most_popular');
$collection->addAttributeToSelect('featured_prod');
//You can check your query by:
$collection->getSelect();
Hope this may help you.... :)

Related

Magento get weigth of configurable product

good afternoon,
How can I recover the value in phtml from the weight of the product? this product is configurable, so it has variations, it needed to recover the weight of the variation
i use this for Default products:
$_product->getWeight()
Lets assume that the product model is loaded.
You need to load the model of the configurable in order to access its child products (variations).
Here is an example
<?php $configurable= Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$simpleCollection = $configurable->getUsedProductCollection()->addAttributeToSelect('*'); // selecting all attributes?>
<?php foreach($simpleCollection as $simple): ?>
<?php echo $simple->getWeight(); ?>
<?php // or?>
<?php echo $simple->getAttributeText('weight'); ?>
<?php endforeach; ?>
Hope this helps

Magento - Conditionally Display Attribute Based on Product Category

I am trying to display an attribute ('release') if a product has a certain category ID. This is what I have so far:
<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
<?php if($category->getId() == 2): ?>
<h5 style="color:#f26522" >
<?php echo $_product->getData('release') ?></h5>
<?php else: ?>
This works well, but only allows me to limit to products categorized under the default category, in this case 'Id() == 2'.
I am trying to limit the function to display only for a subcategory of this root category, but it does not respond to its corresponding category ID.
How can this be accomplished? All help is appreciated!
For reference: Conditional loading content based on category id magento for product page
Edit for Solution:
<?php
$categoryIds = $_product->getCategoryIds();
if(in_array(20,$categoryIds)):
?>
<h5 style="color:#f26522" ><b>
<?php echo $_product->getData('release'); ?></h5></b>
<?php endif; ?>
Thanks to #R.S and #ndlinh for their input and support.
Special thanks to #programmer_rkt whose response here helped clarify this concept.
For Further Reference: Changing Display According to Product Category
Take a look # Magento category ID from product ID
$product = Mage::registry('current_product');
if ($product->getId()) {
$categoryIds = $product->getCategoryIds();
if (is_array($categoryIds) && in_array(2 , $categoryIds)
...
echo $_product->getData('release')
...
Assuming that you are on the product detail page
Here is my solution:
<?php
$category = Mage::getModel('catalog/layer')->getCurrentCategory();
$parentIds = $category->getParentIds();
?>
<?php if(in_array(2, $parentIds): ?>
<h5 style="color:#f26522" >
<?php echo $_product->getData('release') ?></h5>
<?php else: ?>

Magento: List product categories EXCEPT a select few

I am looking to list on the product page the categories that a particular product belongs to; HOWEVER, I would like to be able to say do NOT list some specific categories.
A solution to outputting the list of categories a product belonged to was posted by a fellow stackoverflow user here: https://stackoverflow.com/a/9720480/99112 and it works great to output the results. How could the above code be modified to get what we are looking for?
Just to draw up an example of what I mean:
Say a Product A is a member of Category IDs: 4, 7, 9, 14, 92
On the product page, I want to output the names of the above categories MINUS category IDs: 7, 92
So, the output would only be Category ID names for: 4, 9, 14
The categories we would want to exempt would apply to all products. So in the above example and looking at a Product B, it would also output Category ID names EXCEPT for the ones we don't want (i.e. 7, 92).
Here is the code in question from the above thread (thanks to user "Sarath Tomy"):
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<?php echo $_category->getName() ?>
<?php endforeach; ?>
How would we modify this to check a list of Category IDs that we do NOT want outputted, please?
Many thanks.
Here's a solution that worked (no idea if it's the most efficient way of doing it or not), but here it is:
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<?php if (!in_array($_category->getId(), array(XX,YY))) : ?>
<?php echo $_category->getName() ?>
<?php endif; ?>
<?php endforeach; ?>
where you put in whatever Category IDs you want to exclude in place of XX,YY above.
You could make a System - Configuration setting that contains a multiple select with all the categories.
The categories selected would be the ones excluded.
Now, the best practice would be to extend Mage_Catalog_Model_Product, and add another method getVisibleCategoryIds()
Here's how it would look (not tested):
public function getVisibleCategoryIds() {
return array_diff($this->getCategoryIds(), Mage::getStoreConfig('section_name/group/field'));
}
Or you can extend getCategoryIds() directly and save the hassle of modifying through templates. This would look like this:
public function getCategoryIds() {
return array_diff(parent::getCategoryIds(), Mage::getStoreConfig('section_name/group/field'));
}

I am trying to add related categories link to a product page in Magento via view.phtml

The functionality I am trying to add is due to the fact that there are links floating around there that could potentially land on a page that the product is out of stock, discontinued, etc. These types products are automatically set to a "catalog" view and "Not Visible Individually." we have overcome the fact they are no longer just getting a 404 error page, but now I would like to add the option to view other products from that same category?
I have this:
<?php $count =0; ?>
<?php $categories = $_product->getCategoryIds();?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<li> <?php echo $_category->getName() ?> </li>
<?php $count++;
if($count== 10) break; ?>
<?php endforeach; ?>
It pulls back the categories correctly, but there are categories that I need to filter out and I can't figure out how to do that? Any help would be GREATLY appreciated?
One way to do this, is to filter by id's:
<?php
foreach($categories as $k => $_category_id):
if($_category_id == $idToFilter):
continue;
else:
// show the category etc.
endif;
endforeach;
?>

Get Magento category attribute in frontend

I have created a Category's attribute that I want to use on the frontend. I tried to access it the same way as we do for products but it doesn't seem to be working. How can I show the custom attribute on the frontend? Any guesses?
Thanks
Try this:
$cat_attr = $this->getCurrentCategory()->getAttributes();
if(array_key_exists('short_description', $cat_attr)):
$_shortDescription=$cat_attr['short_description']->getFrontend()->getValue($_category);
endif;
First thing I would do is do the following code on your category object
print_r($category->debug());
This will show you if that attribute is being loaded. If you don't see your attribute, you can go back to the controller where the object is being loaded and add it to your select by adding one of these lines:
->addAttributeToSelect('your_attribute')
That should load your attribute for you.
I had a custom attribute and got it shown in frontend this way;
$category_id = '10';
$attribute_code = 'category_categorycolor';
$category = Mage::getModel('catalog/category')->load($category_id);
echo $category->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($category);
Quite Simple
<?php foreach ($this->getStoreCategories() as $cat): ?>
<?php $_category=Mage::getModel("catalog/category")->load($cat->getId()); ?>
now you should use getImage method to retrive your img attribute
<img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/category/' . $_category->getImage() ?>" />
<?php endforeach ?>

Categories