i have categories list like
Designtype
Multi
Sub cat multi 1
sub cat multi 2
Borring
Jall
Daman
Dupatta
top parsi
Sequence
Trapping
i want to display onl last post of the jall damna dupatta and top parsi
<?php
$cat = get_categories();
echo “<pre>”;
//print_r($cat);
for each ($cat as $c)
{
if($c->cat_name == “Boring”)
{
$sub_cat = $c->term_id;
$cat2 = get_categories();
for each ($cat2 as $c2)
{
if($c2->parent == $sub_cat)
{
echo “<pre>”;
echo $c2->cat_name;
echo “<br/>”;
$aaaaaaaaaaaaaa = query_posts(array(‘category_name’ => $c2->cat_name, ‘posts_per_page’ => 1 ));
print_r($aaaaaaaaaaaaaa);
} } } }
?>
Related
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$pro = $product->getCategoryName(); //category id is fetched here
$category = Mage::getModel('catalog/category')->load($pro);
Root Catalog
Furniture
Electronics
Computer
Processor
Apparel
So when i get processor as category name, i want to display its level 2 category name that is Electronics
got the solution, The code below will get the current category and then keep getting the parent category until it gets the highest category (but not the root category)
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$pro = $product->getCategoryName(); //category id is fetched here
$category = Mage::getModel('catalog/category')->load($pro);
if ($category)
{
while($category->getLevel() != 2)
{
$category = $category->getParentCategory();
if (!$category)
{
break;
}
}
if ($category)
{
echo $category->getName();
}
else
{
echo 'Cannot find parent category';
}
}
$CategoryCollection=Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('level',2);
You can use this
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter()
->addAttributeToFilter('level',2)
OR
<?php
$_category = Mage::registry('current_category');
$_category = $this->getCurrentCategory();
// Category Name
echo $_category->getName();
// Category Level
echo $_category->getLevel();
?>
Yo can use something like this
public function get_cat_from_product($prod_id,$level){
$categoryIds = Mage::getModel('catalog/product')->load($prod_id)->getCategoryIds();
if(count($categoryIds) ){
$firstCategoryId = $categoryIds[$level-1]; //level 2 -> pos 1
$_category = Mage::getModel('catalog/category')->load($firstCategoryId);
echo $_category->getName();
}else{
return null;
}
}
I am playing around with the below code, I am making my personal "music database". In my mysql I have the following tables:
Music Categories
Music Artists
Music Tracks
The code below works fine except it doesn't display the first track of the database, it skips it, but it displays all the rest just fine. If I take the below example, the data displayed is as follows:
Category: 70's
artist2 trackname2
Category: 90's
artist3 trackname3
So, the first track get's skipped. I have been playing around with the code, but I cannot get it to work. It should be displayed like this:
Category: 70's
artist1 trackname1
artist2 trackname2
Category: 90's
artist3 trackname3
Example of how it looks like in the database:
music_categories:
musicID name
1 70s
2 80s
3 90s
music_artists:
artistID name
1 artist1
2 artist2
3 artist3
music_tracks:
id musicID artistID track
1 1 1 trackname1
2 1 2 trackname2
3 3 1 trackname3
Each music track has one artist and one category. I am using the code below in my classes.php file:
public function grabResults($table)
{
$result = 'SELECT * FROM '.$table;
$query = $this->mysqli->query($result);
if($query)
{
while($row = $query->fetch_assoc())
{
$rows[] = $row;
}
return $rows;
}
else {
return false;
}
}
And this is the code itself:
$categories = $data->grabResults(music_categories);
$artists = $data->grabResults(music_artists);
$tracks = $data->grabResults(music_tracks);
$catName = '';
$counter = 0;
foreach ($categories as $category)
{
foreach ($tracks as $track)
{
if($category['name'] != $catName)
{
$countID = $category['id'];
$total = $data->mysqli->query("SELECT `musicID` FROM `music_tracks` WHERE `musicID` = '$countID' ");
$totalCount = $total->num_rows;
if($totalCount > 0)
{
echo $category['name'];
}
$catName = $category['name'];
$counter = 0;
}
if($counter > 0)
{
if($category['id'] == $track['mid'])
{
foreach ($artists as $artist)
{
if($track['artistID'] == $artist['id'])
{
echo $artist['name'];
}
}
echo $track['track'];
echo "<br>";
}
}
$counter++;
}
}
I'd like to show different content on single product page for different categories.
Sample code
<?php if(in_category('62')){ ?>Text for category 62<?php }?>
<?php if(in_category('21')){ ?>Text for category 21<?php }?>
Make the new template and name it: taxonomy-product_cat-Your_category_product_slug.php
Path: /wp-content/your-theme/woocommerce/taxonomy-product_cat-Your_category_product_slug.php
Another way to do this:
$cat1 = 62;
$cat2 = 21;
$terms = wp_get_post_terms($post->ID, 'product_cat');
if (!empty($terms)) {
foreach ($terms as $t) {
if ($t->term_id == $cat1) {
woocommerce_get_template_part('single-product-product1');
} else if ($t->term_id == $cat2) {
woocommerce_get_template_part('single-product-product2');
}
}
}
I created a code to filter the product collection by categories. Making its own left side block using the following code.
I have it to work perfect for filtering, I am just having a hard time rendering the display.
The Level 2 categories work fine and only show
Top Level Category 1
Top Level Category 2
Once selected it then shows sub categories and their sub categories..
Sub Category of 1
Sub sub category of ^^^
Sub sub category of ^^^
Sub sub category of ^^^
Sub sub sub category of ^^^
Sub Category of 1
Sub sub category of ^^^
Sub sub category of ^^^
Sub sub category of ^^^
Sub sub sub category of ^^^
I need it to break down like the first level did. What would be the best method?
<?php
$root_category_id = Mage::app()->getStore()->getRootCategoryId();
$filterCategoryId = Mage::app()->getRequest()->getParam('cat');
$products = Mage::getSingleton('catalogsearch/advanced')->getProductCollection();
$catIds = array();
if (isset($products)) foreach ($products as $product) {
if ($product->isSaleable()) {
$ids = $product->getCategoryIds();
foreach ($ids as $id) $catIds[$id] = 1;
}
}
if (!isset($catIds)) return false;
$categories = array();
// Filters rest of categories
if ($filterCategoryId) {
$filterCategory = Mage::getModel('catalog/category')->load($filterCategoryId);
$filterChildren = $filterCategory->getAllChildren(true);
foreach ($catIds as $id => $x) {
$category = Mage::getModel('catalog/category')->load($id);
if (in_array($root_category_id, $category->getParentIds()) && in_array($id, $filterChildren) && $id != $filterCategoryId) {
$categories[$id] = $category->getName();
} else {
unset($catIds[$id]);
}
}
} else {
// Filters the first level categoires
foreach ($catIds as $id => $x) {
$category = Mage::getModel('catalog/category')->load($id);
if (in_array($root_category_id, $category->getParentIds()) && $category->getLevel() == 2) {
$categories[$id] = $category->getName();
} else {
unset($catIds[$id]);
}
}
}
if(isset($categories) && sizeof($categories) > 0):
$url = Mage::app()->getRequest()->getRequesturi();
$url = Mage::helper("core/url")->removeRequestParam($url, 'cat');
?>
<div class="page-subtitle" style="margin:6px auto 14px 6px;"><h2>Shop By Category</h2></div>
<ul class="brandsnav">
<?php foreach($categories as $id=>$name): ?>
<li class="cf">
<span class="subnav_trigger"></span>
<a href="<?php
echo Mage::helper("core/url")->addRequestParam($url, array("cat"=>$id)); ?>"><?php echo $name;?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I just had to add conditions to the foreach statements. Got it working.
I am using the following code to display the sub categories of the current category you are own.
However I have 2 levels of sub cats so I need to be able to modify the code somehow to only show the 1st level of sub categories for the current category you are own instead of both levels?
<?php
/* Load category by id */
$cat = Mage::registry('current_category');
/*Returns comma separated ids*/
$subcats = $cat->getChildren();
foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
echo '<li>'.$_category->getName().'</li>';
/* Load category by id */
$sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
/*Returns comma separated ids*/
$sub_subcats = $sub_cat->getChildren();
foreach(explode(',',$sub_subcats) as $sub_subCatid)
{
$_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
if($_sub_category->getIsActive()) {
echo '<li class="sub_cat">'.$_sub_category->getName().'</li>';
}
}
}
}
?> `
If you just want to show the first level subcategories, then just get rid of
/*Returns comma separated ids*/
$sub_subcats = $sub_cat->getChildren();
foreach(explode(',',$sub_subcats) as $sub_subCatid)
{
$_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
if($_sub_category->getIsActive()) {
echo '<li class="sub_cat">'.$_sub_category->getName().'</li>';
}
}
Since you're now loading the current category through $cat = Mage::registry('current_category'); and fetches is subcategories that you loop through (first level).
You may use the $_sub_category->getLevel() to check the current level.
Try re-writing the below code
if($_sub_category->getIsActive()) {
to
if($_sub_category->getIsActive() && $_sub_category->getLevel() == 2) {
Please make sure getLevel() returns the appropriate level (can't remember it is 1,2 or 3).