$product->category_list[0]->get_parents(true)
#1
Posted 04 September 2010 - 02:49 PM
I have two root categories each with a child category. Products belonging to a root category also belong to the corresponding child category. Using category_list[0]->get_parents(true) method returns both the root and child category for products in 1 of the root categories but only the root category for a product in the other.
Is category_list[0] controlled by the category sort order? If so I don't see how to change the sort index of child categories with respect to the parent category, it's only possible with respect to siblings.
What I'm trying to get is consistency. If breadcrumbs to product 1 show it in the root category even though it's also belongs to the child, then the same needs to be true of product 2 in the other root category. I could enumerate all parent branches and apply some decision logic but that seems a bit of a sledgehammer approach. Is their an easier way?
Thanks
iD
#2
Posted 04 September 2010 - 09:52 PM
Just discovered it's alphabetical. The category_list[0]->get_parents(true) method regards the 'default' category product to be the one highest in alphabetical name sort order. For example, prefixing the root category with 'AA' changes the 'default' to the root where previously it was a child.
Would it be preferable for the method to default to the category hierarchy rather than the category name?
Thanks
iD
BTW, I did manage to change the sort order index of a child category with respect to it's parent by moving the child up to the root level, re-ordering, then moving it down to child again.
#3
Posted 04 September 2010 - 10:38 PM
Thanks
iD
#4
Posted 05 September 2010 - 02:05 AM
Thanks for sharing. :-) Yes, the Shop_Product::category_list field is a list of all categories the product belongs to, sorted alphabetically. We can add other methods of fetching the list of product's categories in the future, but right now I recommend you to develop a custom function which will check category visibility and sort categories in an order you need.
#5
Posted 05 September 2010 - 03:54 AM
/* No active category so get all product's categories and sort by front end sort order */
$product_categories = $product->category_list->objectArray;
usort ($product_categories, function($a, $b, $c='front_end_sort_order') {
if ($a->$c == $b->$c) {
return 0;
}
return ($a->$c < $b->$c) ? -1 : 1;
}
);
$parent_categories = $product_categories[0]->get_parents(true);iD
#6
Posted 07 September 2011 - 02:06 PM
infradawn, on 05 September 2010 - 03:54 AM, said:
/* No active category so get all product's categories and sort by front end sort order */
$product_categories = $product->category_list->objectArray;
usort ($product_categories, function($a, $b, $c='front_end_sort_order') {
if ($a->$c == $b->$c) {
return 0;
}
return ($a->$c < $b->$c) ? -1 : 1;
}
);
$parent_categories = $product_categories[0]->get_parents(true);iD
Yes it seems LS breadcrumbs function with subcategories is borked at present - the Simplicty theme, the breadcrumbs do not work as they should:
<? if (isset($category) && $category instanceof Shop_Category):
$parent_categories = $category->get_parents(true);
?>
<ul class="clearfix" id="breadcrumb">
<li><a href="<?= root_url('/') ?>">Home</a></li>
<? foreach ($parent_categories as $parent_category): ?>
<li class="<?= $parent_category->id == $category->id ? 'last' : null ?>">
<? if ($parent_category->id != $category->id): ?><a href="<?= $parent_category->page_url('/category') ?>"><? endif ?><?= h($parent_category->name) ?><? if ($parent_category->id != $category->id): ?></a><? endif ?>
</li>
<? endforeach ?>
</ul>
<? elseif (isset($category) && $category instanceof Blog_Category): ?>
<ul class="clearfix" id="breadcrumb">
<li><a href="<?= root_url('News') ?>">News</a></li>
<li class="last"><?= h($category->name) ?></li>
</ul>
<? elseif (isset($product)):
$parent_categories = $product->category_list[0]->get_parents(true);
?>
<ul class="clearfix" id="breadcrumb">
<li><a href="<?= root_url('/') ?>" class="home">Home</a></li>
<? foreach ($parent_categories as $parent_category): ?>
<li>
<a href="<?= $parent_category->page_url('/category') ?>"><?= h($parent_category->name) ?></a>
</li>
<? endforeach ?>
<li class="last"><?= h($product->name) ?></li>
</ul>
<? endif ?>Any chance LS devs of providing a sample code for how this should be done to display subcategories when products belong to a parent and a subcategory - which is very common...
TIA
#7
Posted 07 January 2012 - 04:01 AM
Jez-Timms, on 07 September 2011 - 02:06 PM, said:
<? if (isset($category) && $category instanceof Shop_Category):
$parent_categories = $category->get_parents(true);
?>
<ul class="clearfix" id="breadcrumb">
<li><a href="<?= root_url('/') ?>">Home</a></li>
<? foreach ($parent_categories as $parent_category): ?>
<li class="<?= $parent_category->id == $category->id ? 'last' : null ?>">
<? if ($parent_category->id != $category->id): ?><a href="<?= $parent_category->page_url('/category') ?>"><? endif ?><?= h($parent_category->name) ?><? if ($parent_category->id != $category->id): ?></a><? endif ?>
</li>
<? endforeach ?>
</ul>
<? elseif (isset($category) && $category instanceof Blog_Category): ?>
<ul class="clearfix" id="breadcrumb">
<li><a href="<?= root_url('News') ?>">News</a></li>
<li class="last"><?= h($category->name) ?></li>
</ul>
<? elseif (isset($product)):
$parent_categories = $product->category_list[0]->get_parents(true);
?>
<ul class="clearfix" id="breadcrumb">
<li><a href="<?= root_url('/') ?>" class="home">Home</a></li>
<? foreach ($parent_categories as $parent_category): ?>
<li>
<a href="<?= $parent_category->page_url('/category') ?>"><?= h($parent_category->name) ?></a>
</li>
<? endforeach ?>
<li class="last"><?= h($product->name) ?></li>
</ul>
<? endif ?>Any chance LS devs of providing a sample code for how this should be done to display subcategories when products belong to a parent and a subcategory - which is very common...
TIA
I am having a similar issue with nested categories.
When using the breadcrumbs code from the Simplicty theme, and on a product page, my breadcrumbs don't display the immediate category the item is in...
i.e, Home > Men > Clothing > Product Name (should be): Home > Men > Clothing > T-Shirts > Product Name
All works as expected on product list pages.
#8
Posted 07 January 2012 - 06:59 AM
I partly solved this problem for a navigation list by iterating through the list of all categories for a product and determining which were top-level and which were children.
I say 'partly' because it doesn't quite work if a product belongs to multiple children categories (I just break the foreach loop after it finds the first child category).
You should be able to apply the same logic to making breadcrumbs.
Here's the code I used:
<?
$categories = $product->category_list;
$catnum = $categories->count;
if ($catnum != 1):
foreach ($categories as $category):
if ($category->category_id != null) :
$category_url_name = $category->url_name; break;
endif;
endforeach;
else:
foreach ($categories as $category):
$category_url_name = $category->url_name; break;
endforeach;
endif; ?>
#9
Posted 07 January 2012 - 07:41 AM
If you have nested categories and want to show breadcrumbs for product lists and product pages, use the modified breadcrumbs code below taken from the Simplicity theme:
<? if (isset($category) && $category instanceof Shop_Category):
$parent_categories = $category->get_parents(true);
?>
<ul class="clearfix">
<li><a href="<?= root_url() ?>">Home</a></li>
<? foreach ($parent_categories as $parent_category): ?>
<li class="<?= $parent_category->id == $category->id ? 'last' : null ?>">
<? if ($parent_category->id != $category->id): ?><a href="<?= $parent_category->page_url('/category') ?>"><? endif ?>
<?= h($parent_category->name) ?>
<? if ($parent_category->id != $category->id): ?></a><? endif ?>
</li>
<? endforeach ?>
</ul>
<? elseif (isset($category) && $category instanceof Blog_Category): ?>
<ul class="clearfix">
<li><a href="<?= root_url('News') ?>">News</a></li>
<li class="last"><?= h($category->name) ?></li>
</ul>
<? elseif (isset($product)):
$product_categories = $product->category_list->objectArray;
function sort_product_categories($a, $b, $c='front_end_sort_order')
{
if ($a->$c == $b->$c) return 0;
return ($a->$c < $b->$c) ? 1 : -1;
}
usort ($product_categories, 'sort_product_categories');
$parent_categories = $product_categories[1]->get_parents(true);
?>
<ul class="clearfix">
<li><a href="<?= root_url() ?>">Home</a></li>
<? foreach ($parent_categories as $parent_category): ?>
<li>
<a href="<?= $parent_category->page_url('/category') ?>"><?= h($parent_category->name) ?></a>
</li>
<? endforeach ?>
<li class="last"><?= h($product->name) ?></li>
</ul>
<? endif ?>
I just changed this line to have a 1 instread of a 0 :
$parent_categories = $product_categories[1]->get_parents(true);

Help













