LemonStand Forum: $product->category_list[0]->get_parents(true) - LemonStand Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

$product->category_list[0]->get_parents(true)

#1 User is offline   infradawn 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 85
  • Joined: 19-April 10

Posted 04 September 2010 - 02:49 PM

Hi,

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
0

#2 User is offline   infradawn 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 85
  • Joined: 19-April 10

Posted 04 September 2010 - 09:52 PM

Hi,

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.
0

#3 User is offline   infradawn 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 85
  • Joined: 19-April 10

Posted 04 September 2010 - 10:38 PM

Another complication of the existing behaviour is: if a product belongs to a hidden category like 'Discount 10%' and it also belongs to a non-hidden category, say 'Miscellaneous', then if category_list[0]->get_parents(true) is used to generate breadcrumbs the trail will show 'Discount 10%' because 'D' is before 'M'. I currently have to prefix my hidden discount categories 'Z-' so they're not considered the default category. Much better I think to have some control of the category the method considers 'default'?

Thanks

iD
0

#4 User is online   Aleksey 

  • Co-Founder
  • Group: +Administrators
  • Posts: 3,633
  • Joined: 31-October 09

Posted 05 September 2010 - 02:05 AM

Hi, iD!

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 User is offline   infradawn 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 85
  • Joined: 19-April 10

Posted 05 September 2010 - 03:54 AM

By front end sort order will do nicely :). In case it's useful to others:

/* 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
0

#6 User is offline   Jez-Timms 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 147
  • Joined: 01-November 09

Posted 07 September 2011 - 02:06 PM

View Postinfradawn, on 05 September 2010 - 03:54 AM, said:

By front end sort order will do nicely :). In case it's useful to others:

/* 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
0

#7 User is offline   jaybee 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 60
  • Joined: 11-January 10
  • LocationCheltenham, UK

Posted 07 January 2012 - 04:01 AM

View PostJez-Timms, on 07 September 2011 - 02:06 PM, said:

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


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.
0

#8 User is offline   gcf 

  • Member
  • Group: Members
  • Posts: 27
  • Joined: 28-November 11

Posted 07 January 2012 - 06:59 AM

Hi guys—

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; ?>

0

#9 User is offline   jaybee 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 60
  • Joined: 11-January 10
  • LocationCheltenham, UK

Posted 07 January 2012 - 07:41 AM

Okay, I have managed to solve the problem I was having.

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);

B)
1

Share this topic:


Page 1 of 1

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users