i want to display sub-categories (if any) of a current category on listing page, but if there is no sub-category and also the current category is not the main category, then display the same level categories.
<?php
//print_r($_category); exit;
$Curr_cat_id = $_category->getEntityId();
$children = Mage::getModel('catalog/category')->getCategories($Curr_cat_id);
if($children){
foreach($children as $sub_cat)
{
$subCat = Mage::getModel('catalog/category')->load($sub_cat->getId());
?>
<a href="<?php echo $this->getUrl().$sub_cat->getRequestPath(); ?>" >
<?php echo $sub_cat->getName(); ?>
</a><br />
<?php
} // end of foreach
} // end of if
//else{
// this is where i want the same level categories if in case there are no sub-categories and also the current category is not a main category.
//}
?>
You need to get the parent category id of your current category first. And then load the products ( like you are doing in your script ) with the parent category id instead.
To achieve that the following post should be of help
http://www.magentocommerce.com/boards/viewthread/16357/
Related
I'm trying to to switch the price for a 'Sold' label on the product page of products that are out of stock.
If a product is sold out, the price should be hidden and in its stead should be a 'Sold' label.
I figured out that the price is placed in catalog_product_view.xml and it calls upon the vendor/magento/module-catalog/view/base/templates/product/price/final_price.phtml file, but I could not figure out where and how to bring in a condition to check whether product is sold out or not.
Can someone help here?
Thanks in advance.
Yuan
As I understand you have two parts to this issue
1) Hide price on Product-detail page if product is Out-of-stock
Prices are defined in vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml
called at class: Magento\Catalog\Pricing\Render and method: _toHtml()
You can override the method using DI to below
protected function _toHtml()
{
/** #var PricingRender $priceRender */
$priceRender = $this->getLayout()->getBlock($this->getPriceRender());
if ($priceRender instanceof PricingRender) {
$product = $this->getProduct();
if ($product instanceof SaleableInterface && $product->isAvailable()) {
$arguments = $this->getData();
$arguments['render_block'] = $this;
return $priceRender->render($this->getPriceTypeCode(), $product, $arguments);
}
}
return parent::_toHtml();
}
$product->isAvailable() is the new condition that we have added
2) Show Sold label
outofstock label is shown by default, but if you still want to show create your block & template in vendor/magento/modul-catalog/view/frontend/layout/catalog_product_view.xml
and $product->isAvailable() function to check product's availability
Hope this helps
Hiding price for out of stock products in Magento 1.
RWD Theme
app/design/frontend/rwd/template/catalog/product/view.phtml
Change
<div class="price-info">
<?php echo $this->getPriceHtml($_product); ?>
<?php echo $this->getChildHtml('bundle_prices') ?>
<?php echo $this->getTierPriceHtml() ?>
</div>
To:
<?php if($_product->isSaleable()): ?>
<div class="price-info">
<?php echo $this->getPriceHtml($_product); ?>
<?php echo $this->getChildHtml('bundle_prices') ?>
<?php echo $this->getTierPriceHtml() ?>
</div>
<?php endif; ?>
Default Theme:
\app\design\frontend\base\default\template\catalog\product\view\type\default.phtml
Change
<?php echo $this->getPriceHtml($_product) ?>
To:
<?php if($_product->isSaleable()): ?>
<?php echo $this->getPriceHtml($_product) ?>
<?php endif; ?>
OR
Hiding price for out of stock products in Magento 2.
On admin page, Click on Stores, then under the Setting section, choose Configuration.
In this page, you find the Inventory section under Catalog. Expand the Stock Options section and you can start to set the custom status of the product.
Before coming to the Out of stock product section, you need to enter this field.
Set Items’ Status to be in Stock When Order in Canceled: You choose YES when you want to return items to your stock if an order is canceled.
Decrease Stock When Order is Placed: if you want to adjust the quantity on hand when the order is placed, you choose Yes
Then we can come to the part that allows you to display or disable the product out of stock in Magento 2.
You want to display the product out of stock, you set the Display Out of Stock Products section is Yes. In contrast, set No if you want to disable it.
I want to retrieve the parent category only in current store of magento. I googled and get the result for all parent category with subcategories included. but want only parent top categories single depth only.
if(strlen(trim($primary_category_temp)) < 1)
{
$_categories = Mage::helper('catalog/category')->getStoreCategories();
if (count($_categories) > 0):
foreach($_categories as $_category):
$primary_category[] = $_category->getId();
endforeach;
endif;
}
$category = Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('is_active',array('eq' => 1))->load();
from this i get whole category collection
foreach($category as $cat)
{
if($cat->getData('level')==2)
{
echo 'my code';
}
}
May be you need use:
Mage::app()->getStore($store)->getRootCategoryId()
or
Mage::app()->getStore()->getRootCategoryId()
for default store
Try below code
$categories=Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('level',2)
->addIsActiveFilter();
This will only retrieve parent top categories
Other than the normal navigation I get when I add subcategories to the main root category, I want to be able to make a new root category, assign subcategories to it and have it display as a separate menu.
Is this possible?
May this can help you :Link 1Link 2
To retrieve another root category
<?php
echo '<pre>';
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id); // where $id will be the known category id
if(!empty($categories))
{
echo 'category name->'.$categories->getName(); //get category name
echo '<br>';
echo 'category image url->'.$categories->getImageUrl(); //get category image url
echo '<br>';
echo 'category url path->'.$categories->getUrlPath(); //get category url path
echo '<br>';
}
?>
now $id=9; is my new root category id.
To retrieve sub categories of these new root category ($id=9;) below is the following reference code.Customize it according to your requirements.
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php foreach($categories as $category): ?>
<?php $subcategories = $category->getChildren() ?>
<?php foreach($subcategories as $subcategory): ?>
<?php $subsubcategories = $subcategory->getChildren() ?>
<?php foreach($subsubcategories as $subsubcategory): ?>
<?php endforeach; ?><!-- end foreach subsubcategories -->
<?php endforeach; ?><!-- end foreach subcategories -->
<?php endforeach; ?><!-- end foreach categories -->
In an ideal world you would have found the answer by now, but in case you didn't I modified Nikhil's answer to work for me to basically do what you describe, minus any convenience at all...
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id);
if(!empty($categories))
{
$cats = explode(",", $categories->getChildren());
foreach($cats AS $c)
{
$cat = $catagory_model->load(trim($c));
echo ''.$cat->getName().'';
}
}
I'm just pasting what I used. The reality is you will have to build the html to make this do whatever you want it to do. If you have subcategories within your loop, you will have to run another fetch in the foreach part.
I am not familiar with magento enough to know what methods you can run on the $cat object. I did a print_r($cat) to examine the object and made a lucky guess that getUrlKey would be available.
Magento... pfft! you'd think ebay would have higher standards than this.
How to sort by position products.
I am making a slider and a want products to be sorted on slider. I want to use build in sorting like best match, which is on category -> product tab
Here is how I am doing but won't work:
$product_collection = Mage::getModel('catalog/category')->getProductCollection()->setOrder('position','ASC');
$selected = 0;
foreach($product_collection as $product) {
$full_product = Mage::getModel('catalog/product')->load($product->getId());
if($full_product->getTypeId() === 'configurable'){ ?>
<li>
<img>
</li>
<? }
} ?>
You need to load a category in the category model before pulling out its product collection
$product_collection = Mage::getModel('catalog/category')->getProductCollection()->setOrder('position','ASC');
should be replaced by
$product_collection = Mage::getModel('catalog/category')->load($somecategoryid)->getProductCollection()->setOrder('position','ASC');
I have 2 category level (top level & sub category). On the top category page i need to show all the sub category with few of the sub category products for each sub categories. Also need to show some other details on the top level category page.
On the sub category page, need to show the sub category products.
As we have one template page for category and sub category product page.
How this can be handled.
Maybe this link is helpfull for displaying subcategories.
http://fishpig.co.uk/display-categories-and-subcategories-in-magento/
And for the Products you can see if the use as anchor works for you or you try
http://oggettoweb.com/blog/news/magento-extension-product-blocks/
or you modify your template to pull some products with certain attributes
$currCat = Mage::registry('current_category');
/**
* get sub categories of current category
*/
$collection = Mage::getModel('catalog/category')
->getCategories($currCat->getEntityId());
/**
* only showing active sub categories
*/
foreach($collection as $cat) {
if($cat->getIsActive()) {
$category = Mage::getModel('catalog/category')
->load($cat->getEntityId());
$prodCollection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($category);
Mage::getSingleton('catalog/product_status')
->addVisibleFilterToCollection($prodCollection);
Mage::getSingleton('catalog/product_visibility')
->addVisibleInCatalogFilterToCollection($prodCollection);
?>
<a href="<?php echo $category->getUrl() ?>">
<?php echo $category->getName() ?>
</a> (<?php echo $prodCollection->count() ?>)<br/>
<?php
}
}
Source: Get Sub Categories & Product Count