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 :)
Related
I am very new to Magento. I have an old Magento 1 site that someone else has built and I am trying to edit content that is on a particular page, called 'Trade'.
In the backend, I have found the page: CMS > PAGES > TRADE.
The 'CONTENT' tab has some of the pages content written in it, but not the part I want to edit.
The 'DESIGN' tab has the following code, which I believe is where the content I want to edit is located:
<reference name="content">
<block type="core/template" name="boards" template="cms/trade.phtml" />
</reference>
When I go to 'TRADE.PHTML' in my files, I have the following code:
<?php
$page = Mage::getSingleton('cms/page');
$advanced_fields = Mage::helper('core')->jsonDecode($page->getAdvancedFields());
$cms_fields = array();
foreach($advanced_fields as $field){
$name = $field['name'];
$content = $field['content'];
$cms_fields[$name] = $content;
}
$filter = new Mage_Widget_Model_Template_Filter();
?>
<div class="board">
<?php if(isset($cms_fields['board_1'])){ ?>
<div class="board_left board_inner">
<?php echo $filter->filter($cms_fields['board_1']); ?>
</div>
<?php } ?>
<?php if(isset($cms_fields['board_2'])){ ?>
<div class="board_right board_inner">
<?php echo $filter->filter($cms_fields['board_2']); ?>
</div>
<?php } ?>
</div>
I can not for the life of me figure out where to edit this little bit of content, and it's taking up way too much time.
A point in the right direction would be a great help, as I'm sure this is pretty simple and obvious to you Magento devs! Help a newbie out :)
In Magento 1 I've always used
$_product->getMediaGallery('images')
But in the source from Magento 2 I see
$productImage = $block->getImage($_product, $image);
echo $productImage->toHtml();
It's only getting the first product image.
How do I get the second or third image (not only the base one)?
GetMediaGallery function doesn't exists?
Step 1 : open list.phtml from Your Theme\Magento_Catalog\templates\product
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
Add above in your phtml file
then add below code in product loop where you want gallery images
<div class="left-carousel">
<div class="product-small-thumbs">
<?php $product = $objectManager->create('Magento\Catalog\Model\Product')->load($_product->getId());
$images = $product->getMediaGalleryImages();
if($images->count()>0){?>
<div class="carousel carousel-<?php $_product->getId()?>">
<?php
$i = 0;
foreach($images as $child){
$i++;
$productImage = $_imagehelper->init($product, 'product_page_image_large')
->setImageFile($child->getFile())->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(TRUE)->resize(81,53)
->getUrl();
$productImagedata = $_imagehelper->init($product, 'product_page_image_large')
->setImageFile($child->getFile())->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(TRUE)->resize(285,240)
->getUrl();
if($i==1) continue;
?>
<div class="slide">
<img data-id="smallthumbs-<?php echo $_product->getId();?>" data-img="<?php echo $productImagedata; ?>" src="<?php echo $productImage; ?>"/>
</div>
<?php
}
?>
</div>
<?php
}
?>
</div>
</div>
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.
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 ?
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.