what should be the code in phtml file to show one specific subcategory product on home page ?
I have created a file in catalog/product/topSection.phtml with following code-
<?php
$cat_id = 42;
$category = Mage::getModel('catalog/category')->load($cat_id);
$products = $category->getProductCollection();
echo 'total- '.$products->count();
foreach($products as $product){ ?>
<h3>
<img src="<?php echo $product->getImageUrl() ?>" alt="">
<p><?php echo $product->getName() ?></p>
</h3>
<?php }?>
and used
{{block type="core/template" name="top.section.home" template="catalog/product/topSection.phtml"}}
in home page content. but nothing is displaying. what is wrong in my code.
I checked your code it is working and also display product. First please check category id and sure category is enable ?
Related
Hy ! I'm trying to display the categories on a cms page.I've tried all the solution on the web but none is working for me. The last one i've tried is this.
1.I've added this code in content tab of my cms page :
{{block type="catalog/navigation" template="catalog/category/list.phtml"}}
2.I've created the list.phtml and put the file on app/design/theme-name/template/catalog/category.
Here is the code pf my file
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php $open = $this->isCategoryActive($_category); ?>
<?php
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
if ($immagine = $this->getCurrentCategory()->getImageUrl()):
?>
<div class="catalog-image">
<div>
<a href="<?php echo $this->getCategoryUrl($_category)?>">
<img src="<?php echo $immagine ?>" alt="<?php echo $this->htmlEscape($this->getCurrentCategory()->getName()) ?>" width="313" height="151" />
</a>
</div>
<div class="left"><h2><?php echo $_category->getName()?></h2></div>
</div>
<?php endif; ?>
<?php endforeach; ?>
What I'm doing wrong ? Thanks !
Add this code in content tab of your cms page:
{{block type="core/template" template="page/categories-list.phtml"}}
Create a file in the theme such "categories-list.phtml"
path: app/design/theme-name/template/page/categories-list.phtml
In this file, write the following code in order to get a collection of all the categories:
<?php
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter();
?>
If you want only some categories of parent, use the following code:
<?php
$parent_category_id = 5; // this is ID of parent category
$categories = Mage::getModel('catalog/category')->getCategories($parent_category_id);
?>
To display categories on the screen, use loop, look below:
<?php foreach ($categories as $category): ?>
<p><?php echo $category->getImageUrl(); ?></p>
<p><?php echo $category->getName(); ?></p>
<?php endforeach; ?>
If you use the categories of the parent category to display the image, use the following code:
Mage::getModel('catalog/category')->load($category->getId())->getImageUrl();
Create a folder under catalog with name navigation and put your list.phtml file there it will work.
{{block type="core/template" template="catalog/navigation/list.phtml" category_id="507" }}
Category Display Mode shuould be in static block in display setting.
I'm working on a magento module that shows particular product collections under a new controller and front-name.
Some of these collections get big, so I'd like to add layered navigation to the side of the page. (And hey, pagination and sort while we're at it.)
I can add the layered navigation block with
<reference name="left">
<block type="catalog/layer_view" name="catalog.leftnav" template="landing/layer.phtml"/>
</reference>
What I get with that is the layered navigation as applied to the whole catalog, with categories broken, and no interface with the on-page product collection.
How would I go about wiring up the layered navigation (and hey, pagination and sort) to this custom product collection?
No answers were forthcoming here, and I changed direction and never completed this path of development. Figured I'd post what I'd learned.
The approach above is sound. The work basically entails recreating the functionality on the catalog or catalogsearch modules. You'll have to subclass the models and blocks of catalog, modifying the product collection and current category.
This post heads vaguely in the right direction, but doesn't get there.
http://www.webdesign-gm.co.uk/news/layered-navigation-on-home-page-or-any-cms-page-magento.php
If you make more headway on this, feel free to post.
I've had a similar request from a client to include specific filterable attributes under a mega menu.
Using a static block i've added this line to everywhere i needed a listing of attributes for specific category
{{block type="core/template" attribute_code="age_specific" category_id="12" template="megamenu-custom-filter-list.phtml"}}
And then the "megamenu-custom-filter-list.phtml"
<?php if($this->getCategoryId() && is_numeric($this->getCategoryId()) && $this->getAttributeCode()): ?>
<?php
$visible_items = 12;
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
$attrCode = $this->getAttributeCode();
unset($layer);
$layer = Mage::getModel("catalog/layer");
$layer->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();
foreach ($attributes as $attribute):
if($attribute->getAttributeCode() != $attrCode){
continue;
}
if ($attribute->getAttributeCode() == 'price') {
$filterBlockName = 'catalog/layer_filter_price';
} elseif ($attribute->getBackendType() == 'decimal') {
$filterBlockName = 'catalog/layer_filter_decimal';
} else {
$filterBlockName = 'catalog/layer_filter_attribute';
}
$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
?>
<div>
<h4 class="menu-block-title"><?php echo $this->__($attribute->getFrontendLabel()) ?></h4>
<ul class="menu-attr-list">
<?php $counting = 1; ?>
<?php foreach($result->getItems() as $option): ?>
<?php $optionUrl = $category->getUrl() . "?" . $attribute->getAttributeCode() . "=" . $option->getValue(); ?>
<?php if(!$option->getCount()){ continue; } ?>
<li class="<?php echo ($counting >= $visible_items+1)?"visible-on-showmore":"" ?>" style="list-style: none;">
<a class="cube" href="<?php echo $optionUrl ?>">
<?php echo $option->getLabel() ?> <?php /* (<?php echo $option->getCount() ?>) */ ?>
</a>
</li>
<?php $counting++; ?>
<?php endforeach; ?>
<?php if($counting >= $visible_items+1): ?>
<li class="show-more-menuitem">
<a href="javascript:void(0)" onclick="showMoreMenuItems(this); return false;" class="">
<?php echo $this->__('Visa fler') ?>
</a>
</li>
<?php endif; ?>
</ul>
</div>
<?php endoreach; ?>
<?php endif; ?>
Of course, this can be extended to allow all attributes to be shown and not limit them to 12 (as i did here). Although, i've not build a feature to show this for a custom collection, but i believe that following the "catalog/layer" model and see where the collection was loaded, you can see how to overwrite it and load your own custom collection.
I am trying to customize a Blanco Magento Theme I bought in Themeforest,
I want to display some categories (not all) in the front page with an static image, does anyone have an idea how to do it?
Thanks a lot!
PS: Magento is 1.7
This can be easily done if you need to display products on home page or any CMS page then do the following:
a) Create a new category of the products which you want to display on the desired page and note down the category Id generated (suppose id = 4)
b) Go to Admin->CMS->Pages and select the page you want
For adding any image add an img tag with the below src
src="{{media url="image_path"}}"
{{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" category_id="4" template="catalog/product/list.phtml"}}
that's it
Hope this helps!!
You can put the following Code into a PHTML file and include it in your Front Page using XML or other way you want. As far as static image is concern, you can add Category image from Admin which will be display from the given code -
CODE is :
<?php
# CategoryIDs to be display on Front Page...
$_viewCategories = array(3, 4, 5);
# Categories Counter...
$counter = count($_viewCategories);
# Get Category Model
$getCategoryModel = Mage::getModel('catalog/category');
?>
<div class="categoryList">
<?php
for($i = 0; $i < $counter; $i++)
{
// Load Categories...
$getCategoryModel->load($_viewCategories[$i]);
$getParentCategoryName = $getCategoryModel->getName();
# echo "Parent Category Name : ".$getParentCategoryName;
?>
<div class="categoryListContainer">
<div class="categoryListHeading"><?php echo ucwords(strtolower($getParentCategoryName)); ?></div>
<?php
$getChildren = $getCategoryModel->getChildren();
$subCategories = explode(",", $getChildren);
$j = 0;
foreach ($subCategories as $_child) {
if($j >= 2){ break; }
?>
<div style="float:left; width:205px; <?if($j == 1):?>margin-left:25px;<?php endif; ?>" align="center">
<?php
$subCategoryDetail = Mage::getModel("catalog/category")->load($_child);
echo "<a href='".$subCategoryDetail->getUrl()."' title='".$subCategoryDetail->getName()."'>";
echo "<div class='categoryListImg'><img src='".$subCategoryDetail->getImageUrl()."' height='120px' width='120px' alt='".$subCategoryDetail->getName()."' /></div>";
echo "<div class='categoryListCaption'>";
echo "<span class='catName'><span>".$subCategoryDetail->getName()."</span></span>";
echo "</div>";
echo "</a>";
?></div>
<?php
$j++;
}
?>
</div>
<?php
}
?>
</div>
It's Style you can manage with yourself with your own Style :)
Hope it would be helpful for you!
Thanks :)
I would like to display the related product pricing and have add to cart button along with each of the related products.
Below is the code snippet from the related products page. The $field does not have any pricing available. How can I show the pricing and "add to cart" button? Thanks in advance
<?php
foreach ($this->product->customfieldsRelatedProducts as $field) {
?><div class="product-field product-field-type-<?php echo $field->field_type ?>">
<span class="product-field-display"><?php echo $field->display ?></span>
<span class="product-field-desc"><?php echo jText::_($field->custom_field_desc) ?></span>
</div>
<?php } ?>
I have found solution here and it works for me:
no need to edit core files
It requires copying the "default_relatedproducts.php", "default_showprices.php" and "default_addtocart.php" to your "template/html/com_virtuemart/productdetails" folder. Then replace all of the code in the "default_relatedproducts.php" with the following code:
<?php
// Check to ensure this file is included in Joomla!
defined ( '_JEXEC' ) or die ( 'Restricted access' );
$model = new VirtueMartModelProduct();
$calculator = calculationHelper::getInstance();
$currency = CurrencyDisplay::getInstance();
?>
<div class="product-related-products">
<h4><?php echo JText::_('COM_VIRTUEMART_RELATED_PRODUCTS'); ?></h4>
<div>
<?php
foreach ($this->product->customfieldsRelatedProducts as $field) {
?>
<div class="product-field">
<?php
$product = $model->getProductSingle($field->custom_value,true);
?>
<h2><?php echo JHTML::link ($product->link, $product->product_name); ?></h2>
<a title="<?php echo $product->product_name ?>" rel="vm-additional-images" href="<?php echo $product->link; ?>">
<?php
echo $this->product->images[0]->displayMediaThumb('class="browseProductImage"', false);
?>
</a>
<div class="short_desc"><?php echo $product->product_s_desc; ?></div>
<?php include 'default_showprices.php'; ?>
<?php include 'default_addtocart.php'; ?>
</div>
<?php } ?>
</div>
</div>
Had a same problem. But I had to show only the price.
So the fastest way is to change sql select statement in customfields.php
Path in Joomla 2.5 for Virtuemart 2.0 administrator/components/com_virtuemart/models/customfields.php
line 548 of
public function getProductCustomsFieldRelatedProducts($product)
change only
$query=
with
'SELECT C.`virtuemart_custom_id` , `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value`
AS value, `custom_field_desc` , `field_type` , `is_list` , `is_hidden` , C.`published` , field.`virtuemart_customfield_id` ,
field.`custom_value`, field.`custom_param`, price.`product_price`, field.`ordering`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
LEFT JOIN `#__virtuemart_product_prices` AS price ON
field.`custom_value` = price.`virtuemart_product_id`
Where field.`virtuemart_product_id` ='.(int)$product->virtuemart_product_id.' and `field_type` = "R"';
After all on line 559 change
$field->custom_price
to
$field->product_price
And finally...
In template view of product description insert the code below to show the prices of related products
<?php echo $field->product_price ?>
The only problem with the below solution is that it doesn't display the correct images for the related products. It's using the main products image and just repeating it.
I have the following requirement;
I want to show a products configured related products on the category listing page (list.phtml).
I figured I could make some call on the list.phtml inbetween the for each loop for each product using the [b]$_product[/b] variable but I can't seem to populate the relatedProductCollection
sorry new to all this is it even possible.
<?php foreach ($_productCollection as[b] $_product[/b]):?>
<li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?>last<?php endif; ?>" >
<div class="product-shop">
<div class="f-fix">
<?php $product->getRelatedProductCollection(); ?>
</li>
<?php endforeach; ?>
$product has been used to call the getRelatedProductCollection function.
but,
In that foreach loop you have taken it as $_product.
Can you see the difference ??
Underscore is missing. This is taken from your code. Make the variable same in both the places and try.