Magento Featured Item Based on Current Category - php

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

Related

Display a certain category on a page - magento

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

Magento getting this error: Fatal error: Call to a member function getAttributeText() on a non-object

I am getting the error above in a new template I created in order to show the manufacturer's image. I am using this in the view.phtml file and it works just fine. But since this is custom, I figured there would be problems. Hopefully I can figure this one out.
Here is my current code:
<?php
$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$currentCategoryId = $category->getId();
$children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
$product = $this->getProduct();
$brand = $product->getAttributeText('manufacturer');
?>
<div class="landing-page nested-container">
<?php foreach ($children as $category): ?>
<div class="vertical-section grid12-4 mobile-grid-half">
<a href="<?php echo $category->getRequestPath(); ?>">
<img class="center-block" alt="<?php echo $category->getName(); ?>" src="<?php echo $this->getBaseUrl()."media/catalog/category/".Mage::getModel('catalog/category')->load($category->getId())->getThumbnail(); ?>" />
<div class="caption category-boxes-logo full-width">
<?php echo '<img src="'.$this->getBaseUrl().'media/wysiwyg/infortis/brands/'.str_replace(' ', '-', strtolower($brand)).'.jpg" alt="'.$brand.'"></a>' ?>
</div>
</a>
</div>
<?php endforeach; ?>
</div>
You may only call getProduct() for a block which supports it. That generally means a descendant of Mage_Catalog_Block_Product_Abstract although there are others. When creating a block you can specify any type that meets your needs.
<block type="catalog/product_view" template="path/to/your/template.phtml" />
These blocks get their product object from the registry, which is only set for product pages by the catalog controller. If you have to use another block type which does not have a getProduct() function then you can access the registry directly.
$product = Mage::registry('current_product');
It seems to me that you need the manufacturer from products in a given category, and there are several categories to inspect. I assume any product from a category will do. This does not require the block to be a catalog/product_view type.
<?php
$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$children = $category->getChildrenCategories();
?>
<div class="landing-page nested-container">
<?php foreach ($children as $category): ?>
<!-- Load a product brand per category -->
<?php $product = $category->getProductCollection()->getFirstItem() ?>
<?php $brand = $product ? $product->getAttributeText('manufacturer') : 'default' ?>
<div class="vertical-section grid12-4 mobile-grid-half">
<a href="<?php echo $category->getRequestPath(); ?>">
<img class="center-block" alt="<?php echo $category->getName(); ?>" src="<?php echo $this->getBaseUrl()."media/catalog/category/".$category->getId()->getThumbnail(); ?>" />
<div class="caption category-boxes-logo full-width">
<?php echo '<img src="'.$this->getBaseUrl().'media/wysiwyg/infortis/brands/'.str_replace(' ', '-', strtolower($brand)).'.jpg" alt="'.$brand.'"></a>' ?>
</div>
</a>
</div>
<?php endforeach; ?>
</div>
I figured out what I can do. I just used the product name as they should be matching anyway and used the same string I was trying to use for the Manufacturer. I am posting my answer for anyone who may want an answer down the road:
<?php
$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$currentCategoryId = $category->getId();
$children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
?>
<div class="landing-page nested-container">
<?php foreach ($children as $category): ?>
<div class="vertical-section grid12-4 mobile-grid-half">
<a href="<?php echo $category->getRequestPath(); ?>">
<img class="center-block" alt="<?php echo $category->getName(); ?>" src="<?php echo $this->getBaseUrl()."media/catalog/category/".Mage::getModel('catalog/category')->load($category->getId())->getThumbnail(); ?>" />
<div class="caption category-boxes-logo full-width">
<img src="<?php echo $this->getBaseUrl().'media/wysiwyg/infortis/brands/'.str_replace(' ', '_', strtolower($category->getName())).'.png' ?>" alt="<?php echo $category->getName(); ?>">
</div>
</a>
</div>
<?php endforeach; ?>
</div>
I also just used a normal block type of core/template instead of what was suggested in a previous answer. This worked like a charm for me.

how to display configurable image in wishlist sidebar

today I'm fighting with Magento again :)
how to display configurable image in wishlist sidebar
I use the code
<?php $_product = Mage::getModel('catalog/product'); ?>
<?php $_product->load($_product->getIdBySku($_item['sku'])); ?>
<img src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(70, 87); ?>" />
but is not working.
Thanks in advance.
Try the following
<?php
if($_product->getTypeId()=='simple'){
$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')
->getParentIdsByChild($_product->getId());
$parentProduct = Mage::getModel('catalog/product')->load($parentIds[0]);
?>
<img src="<?php echo Mage::helper('catalog/image')->init($parentProduct, 'small_image')->resize(70, 87); ?>" />
<?php }else{ ?>
<img src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(70, 87); ?>" />
<?php } ?>

Magento - Display Second Product Image

I am using the below code to display the main product image:
$productId = $this->getProduct_id();
$_product = Mage::getModel('catalog/product')->load($productId);
?>
<div class="single-image-large" onclick='window.open("<?php echo $_product->getProductUrl() ?>", "_self")'>
<img class="blog-image" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(400, 400) ?>" width="400" height="400" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /><span class="img-caption"><?php echo $this->htmlEscape($_product->getName()) ?></span>
</div>
I would like to edit it so that it shows the product's second image. Any ideas how to achieve this?
I feel you can fetch the other images by using
$_product->getMediaGalleryImages()->getItemByColumnValue('label', 'LABEL_NAME')->getUrl();
or fetch the whole gallery and display it as per your requirement
Mage::getModel(’catalog/product’)->load($productId)->getMediaGalleryImages();

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