Display a certain category on a page - magento - php

I would like to know how to show a certain gallery in magento, on a page.
I created a static block with name and identifier "category_listing" and put this code below:
{{block type="catalog/product_list" column_count="4" category_id="366" template="catalog/product/list.phtml"}}
Note that I want to show only the category 366.
And on the "mypage.phtml" I put this:
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('category_listing')->toHtml(); ?>
Now when I enter the page appear several categories, not just the category 366 (would be 3 products):
http://s24.postimg.org/q9o65red1/Captura_de_tela_2015_05_22_08_14.jpg
thank you

<?php
print $this->getLayout()
->createBlock("blockname")
->setTemplate("yourtemplate.phtml")
->toHtml();
?>
and in your phtml file you define the function to get the product from a particular id
<?php
$categoryid = 366;
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $_product) { ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /> <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?>
</a>
<?php
}
?>

you want to show only the category with id 366 but in your xml you defined the id 7?
-David

Related

how can i display only one recent product with all subcategories magento?

On the category view page I'm getting id of all child categories.
I can display all the products of that category using category id. i have successfully got all product list using link below.
but i want to first recent product with subcategory.
Go through image to get better understanding.
here its view page of makeup category.and Concealers and foundation are chils category.
i want first most recent product of child category.
here is code of view page.
<?php
$_helper = $this->helper('catalog/output');
$_category = $this->getCurrentCategory();
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
?>
<div class="grid_24">
<?php if($this->IsRssCatalogEnable() && $this->IsTopCategory()): ?>
<?php echo $this->__('Subscribe to RSS Feed') ?>
<?php endif; ?>
<h1 class="main-title"><?php echo $_helper->categoryAttribute($_category, $_category->getName(), 'name') ?></h1>
</div>
<?php foreach ($this->getCurrentCategory()->getChildrenCategories() as $_subcat){
foreach($_subcat->getChildrenCategories() as $child){
$cat_id=$child->getId();
?>
<div class="grid_12">
<div class="category-wrapper">
<div class="ribbon">
<h2><?php echo Mage::helper('catalog/output')->categoryAttribute($child, $child->getName(), 'name') ?></h2>
</div>
</div>
</div>
<?php
$category = new Mage_Catalog_Model_Category();
$category->load($cat_id);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $_product) { ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /> <?php echo $_product->getName(); ?>
<?php } ?>
<?php echo Mage::helper('catalog/output')->categoryAttribute($child, $child->getName(), 'name') ?>
<?php }}?>
You need to alter the product collection statement so it does what you want it to. By adding a limit, it will only bring out one product for that category (improve performance as well because you are loading one product and not all of them). You could then use the order statement maybe to pull out the newest product for that category.
$collection = $category->getProductCollection();
$collection->getSelect()->order('catalog_product_entity.entity_id DESC')->limit(1);
Basically with magento collections, providing you don't actually load the collection by calling the load method or start to loop through it, you can alter the sql to make it give you what you want. We did a similar thing to this years ago but by pulling out the most sold product within the category. Might want to add in block caching to your block and template if you have a large database of either categories or products.
A handy thing to do sometimes is;
var_dump($collection->getSelect()->__toString()); die();
Which will output the generated sql that will be executed and you can debug complex sql queries this way.

Get Subcategory in Magento

I need help. I use this code to get the category link under products info on a few pages in Magento:
<?php $categories = $_product->getCategoryIds(); ?>
<span>In </span>
<?php $i=1; foreach($categories as $k => $_category_id): ?>
<?php if($i>1) {break;} ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<a class="in-category" href="<?php echo $_category->getUrl() ?>"><?php echo $_category->getName() ?></a>
<?php $i++; endforeach; ?>
You can see it here: http://192.241.178.130/new_arrivals
Problem I'm having is that I want the script to display the category closest to the product but it's instead displaying the root category (Default category of the site)
M
Thanks.
Try something like this:
<?php
$categoryIds = $_product->getCategoryIds();
$categories = Mage::getModel('catalog/category')
->addAttributeToSelect('url_key')//add url key to select
->addAttributeToSelect('name')//add name to select
->getCollection() //get categories as collection
->addAttributeToFilter('entity_id', $categoryIds)//filter only by selected ids
->addAttributeToFilter('is_active', 1)//get only active categories
->addAttributeToFilter('level', array('gte'=>2))//ignore root category and 'root of roots'
->setOrder('level', 'desc');//sort by level descending
$mainCategory = $categories->getFirstItem();//get only the category lowest in the tree
if ($mainCategory->getId()) : ?>
<a class="in-category" href="<?php echo $mainCategory->getUrl() ?>"><?php echo $mainCategory->getName() ?></a>
<?php endif;?>
Instead of using a foreach to walk one time through the array you can use array_pop.
Anyhow the function getCategoryIds() will return an array of all categories the product is in. This also include parent categories and are in logical order. The category with the lowest id will show up first.
Perhaps something like this will work for you:
<?php $_category_id = array_pop($_product->getCategoryIds()); ?>
<span>In </span>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<a class="in-category" href="<?php echo $_category->getUrl() ?>">
<?php echo $_category->getName() ?>
</a>

Unable to retrieve price information from a Magento product collection

I am trying to output products from some category on an arbitrary page in a fashion similar to that of list.phtml's grid format.
I have the following snippet:
$category = Mage::getModel('catalog/category');
$category->load(17);
$_productCollection = $category->getProductCollection()
->addAttributeToSelect('name');
$_helper = Mage::helper('catalog/output');
That gives me a product collection which I then iterate over with:
foreach ($_productCollection as $_product):
<!-- This works -->
<h2 class="product-name">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">
<?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
</a>
</h2>
<!-- This does not -->
<?php echo $this->getPriceHtml($_product, true) ?>
<!-- This just returns out of stock -->
<div class="actions">
<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')">
<span><span><?php echo $this->__('Add to Cart') ?></span></span>
</button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
endforeach;
The above code except for the call to get the product collection at the top is just borrowed from list.phtml.
Can anyone tell me why the price and is saleable information is not available, hence why the item appears out of stock? Previously when the product name was unavailable, I had to add ->addAttributeToSelect('name'), would I need to add something along those lines?
Please try the code below in your phtml file.
$category = Mage::getModel('catalog/category')->load(3);
$_productCollection = $category->getProductCollection()->addAttributeToSelect('*');
$productBlock=$this->getLayout()->createBlock("catalog/product");
foreach($_productCollection as $_product)
{
//for get the price of product
if($_product->isSaleable()) //this will check if product is in stock
echo $productBlock->getPriceHtml($_product,true);
}
So you're on the right path to look into core, if you want to copy some functional that is similar to the one from the basic Magento - like product listing.
the price function getPriceHtml is a method defined in the abstract class Mage_Catalog_Block_Product_Abstract. So to use it, you need to extend your block from the Mage_Catalog_Block_Product_Abstract one.
isSaleable returned false because you didn't have some of the attributes joined to your collection.
Here's how you should accomplish your goal, if you want to follow Magento's logic.
Create your own module, or just block in local/Mage/Catalog/Block/YourBlock.php. This block should extend Mage_Catalog_Block_Product_Abstract. After that create a method in this block getCustomProductCollection():
pubcli funciton getCustomProductCollection()
{
if (is_null($this->_productCollection)) {
$category = Mage::getModel('catalog/category')->load(17);
$layer = $this->getLayer();
$layer->setCurrentCategory($category);
$this->_productCollection = $layer->getProductCollection();
}
return $this->_productCollection;
}
Now in your phtml file you'll just call for this method:
$productCollection = $this->getCustomProductCollection();
And the rest of the code will work.
$productBlock=$this->getLayout()->createBlock("catalog/product");
echo $productBlock->getPriceHtml($_product,true);
Try this

Magento Featured Item Based on Current Category

I'm using the following code in my view.phtml file to display a random featured product:
<?php
$catId = $this->getCat_id();
$cat=Mage::getModel("catalog/category")->load($catId);
$prodCollection = $cat->getProductCollection();
$pids=array();
foreach($prodCollection as $product)
{
array_push($pids,$product->getId());
}
$randProductId=array_rand($pids);
$product = Mage::getModel('catalog/product')->load($randProductId);
$product->getName();
?>
<div class="catalog-h-price">
<img src="<?php echo $product->getImageUrl();?>" height="64" width="64" /><?php echo $product->getName();?><br /><span class="price"><sup>$</sup><?php echo number_format($product->getData('price'), 0); ?></span> <span class="msrp">U.S. MSRP</span><a href="#"><a href="<?php echo $product->getProductUrl();?>">
<img src="http://coloresg.com/skin/frontend/default/modern/images/view-now.gif" width="36" height="28" /></a>
</div>
Currently it shows a random product from the entire catalog; I would like to know how adjust to pull only from current category.
Thanks,
-Sam
Answered the mistake in the other thread
Replace
$product = Mage::getModel('catalog/product')->load($randProductId);
by
$product = Mage::getModel('catalog/product')->load($pids[$randProductId]);

Simple Call to Get Category Images & Display in List

I'm displaying a list of sub categories by parent category ID and am wanting to display the category image in place of a category name.
Here is what I have so far...
<div id="menu_brands">
<div class="brand_head">
<h3><?php echo $this->__('Browse By Brand') ?></h3>
</div>
<div class="brand_list">
<?php
$cats = Mage::getModel('catalog/category')->load(6)->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = $category->getUrl();
$img = $category->getImageUrl(); //I suspect this line is wrong
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php foreach($categories as $name => $url): ?>
<li>
<!--<?php echo $name; ?>-->
<a href="<?php echo $url; ?>" title="<?php echo $name; ?>">
<img src="<?php echo $img; ?>" width="auto" alt="<?php echo $name; ?>" /> <!--I suspect this line is wrong-->
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
I've tried countless ways to display the images in place of the category names but nothing seems to make the images appear. Currently with the above, the output is an empty 'img src' so there is clearly an error with what I'm trying (and probably a better way of achieving what I'm after).
Please could someone kindly point out what the problem is?
If it's of any relevance, what I intend to do afterwards is then display the category images in a grid format (3 or 4 per line).
Many thanks in advance.
The solution by zigojacko is not ideal because it separately loads in models in a loop. This does not scale well, and with many categories will thrash your database. Ideally, you'd add the images to the child collection.
A faster solution is to add the image attribute to a collection with an ID filter like B00MER:
// Gets all sub categories of parent category 'Brands'
$parent = Mage::getModel('catalog/category')->load(6);
// Create category collection for children
$childrenCollection = $parent->getCollection();
// Only get child categories of parent cat
$childrenCollection->addIdFilter($parent->getChildren());
// Only get active categories
$childrenCollection->addAttributeToFilter('is_active', 1);
// Add base attributes
$childrenCollection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('all_children')
->addAttributeToSelect('is_anchor')
->setOrder('position', Varien_Db_Select::SQL_ASC)
->joinUrlRewrite();
// ADD IMAGE ATTRIBUTE
$childrenCollection->addAttributeToSelect('image');
?>
<ul>
<?php foreach($childrenCollection as $cat): ?>
<li>
<a href="<?php echo $cat->getURL(); ?>" title="<?php echo $cat->getName(); ?>">
<img class="cat-image" src="<?php echo $cat->getImageUrl(); ?>" />
</a>
</li>
<?php endforeach; ?>
</ul>
We managed to resolve this ourselves - see fix below.
<?php
//gets all sub categories of parent category 'Brands'
$cats = Mage::getModel('catalog/category')->load(6)->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php foreach($categories as $name => $data): ?>
<li>
<a href="<?php echo $data['url']; ?>" title="<?php echo $name; ?>">
<img class="cat-image" src="<?php echo $data['img']; ?>" />
</a>
</li>
<?php endforeach; ?>
</ul>
Surprised we didn't get more support on this with it being a relatively simple Magento issue. Thanks B00MER for your answer though.
Give this method a try instead of your $img = $category->getImageUrl(); to $img = getCategoryImage($category);
function getCategoryImage($category) {
$categoryCollection = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->getCollection()->addAttributeToSelect('image')->addIdFilter($category->getId());
foreach($categoryCollection as $category) {
return $category->getImageUrl();
}
}
(now defunct) Reference: http://www.magentocommerce.com/boards/viewthread/5026/P45/

Categories