Show subChild categories


Targets : Show Locations (sub-districts cities) on the customized shop page on tab additional information - Show on line 5

I'm building a property site using wordpress and woocommerce.

In my country has 7 islands, 34 provinces, 514 Cities, 7041 Districts, 82194 Sub-districts.

Then I grouped the data and added it to :

On category

  • Verified (parent)
  • Sold (parent)
  • Rent (parent)
  • Island #1 (parent)
    • province #1 (child)
      • City #1(sub-child)
  • Island #2 (parent)
    • province #2 (child)
      • City #2(sub-child)
  • Island #7 (parent)
    • province #34 (child)
      • City #514(sub-child)

and on product attribute :

pa_area : city (514)

pa_kecamatan : districts (7041)

pa_lokasi : sub-districts (82194)

So, I can customize the shop page as desired/needed and looks like this.

In the Additional Information tab, I also added the sub-district and the city looks like this.

I also display links using permalinks, for example:

https://mydomain/product-categories/island1/provinces3/cities8 (categories)

https://mydomain/product-categories/island5/provinces1/cities12 (categories)

https://mydomain/districts1492 (pa_attributtes)

https://mydomain/districts5630 (pa_attributtes)

https://mydomain/subdistricts11596 (pa_attributtes)

https://mydomain/subdistricts52630 (pa_attributtes)

So far everything seems fine.


My mind changed when I created a demo site (with current product counts at 10k.)

Loading slowly on entering/selecting attributes is starting to feel.
(I know one of the causes is CPU, RAM/Hosting).

This made me think whether display the city only in one place.
(eg city in category or city in product attribute) can help reduce server load?

Please correct me if I misunderstood this.

But if it's true, i using the first way is to delete the city in the category and save the city in the product attribute.

However this will cut the scope, for example :

Previously :

https://mydomain/product-categories/island1/provinces3/cities8 (categories)

https://mydomain/product-categories/island5/provinces1/cities12 (categories)

will be :

https://mydomain/product-categories/island1/provinces3/ (categories)

https://mydomain/product-categories/island5/provinces1/ (categories)

To keep showing the same thing, i can just edit the permalink to:

https://mydomain/product-categories/island1/provinces3/cities8 (categories pa_area)

https://mydomain/product-categories/island5/provinces1/cities12 (categories pa_area)

But is that way I'm already on the right track?


Meanwhile, if I use the second method, namely removing the city from the product attribute, I have trouble displaying city information on the shop page and additional information tab.

The achievement so far is to add the code snippet below with a view like this.

When the product only has the category :

  • Island (parent)
    • Province (child)
      • City (subChild)

View works well (Rumah Cluster Pondok Aren).

However, when the product has the category :

  • Verified (parent)
  • Island (parent)
    • Province (child)
      • City (subChild)

What is displayed is the Verified category, not cities (Townhouse Premium Daerah Cileungsi).

What have I missed?


Besides that, I also don't know how to display these subchild categories on the additional information tab.
I'm stuck here for days, so please help me out of this hassle...

Thanks a lot

Topic get-children hierarchical taxonomy categories Wordpress

Category Web


this code works on the single-product.php (single product page). I had build the code following your diagram

  • Island (parent)
    • province (child)
      • City (sub-child)

this means supposing the cities are a 3rd level category.

global $post;
$cats = get_the_terms( $post->ID, 'product_cat' );
$count = 0;
foreach ($cats as $parent){
  if($parent->parent == 0){
    foreach($cats as $child){
      if($child->parent == $parent->term_id){
        foreach($cats as $subChild){
          if($subChild->parent == $child->term_id){
            if($count){echo ', ';}
            echo $subChild->name;
            $count += 1;
          }
        }
      }
    }
  }
}

the global $post; is just to make sure it works, but try with out it, probably will work as well once where your code is working probably already have it.

if you are sure the city is always one doesnt need the coma between, so it can be use without that if statment.

$cats = get_the_terms( $post->ID, 'product_cat' );
foreach ($cats as $parent){
  if($parent->parent == 0){
    foreach($cats as $child){
      if($child->parent == $parent->term_id){
        foreach($cats as $subChild){
          if($subChild->parent == $child->term_id){
            echo $subChild->name;
          }
        }
      }
    }
  }
}

Check if this code helps you

$cats = $product->get_categories();
$cities = [];
foreach ($cats as $key => $cat):
    if ($cat->parent == 0):
        foreach ($cats as $key => $cat2):
            if ($cat2->parent == $cat->term_id):
                foreach ($cats as $key => $cat3):
                    if ($cat3->parent == $cat2->term_id):
                        array_push($cities, $cat3->name);
                    endif;
                endforeach;
            endif;
        endforeach;
    endif;
endforeach;

foreach($cities as $city){
  echo $city.'<br>';
} 

Probably the code above will answer your question, however if not I'll need the result of this code

$cats = $product->get_categories();
echo '<pre>';
print_r($cats);
echo '</pre>';
die();

only getting deep in the code, but this should give the categories associated with the product

$sub_child = $product->get_categories();
foreach($sub_child as $a){
   echo $a;
}

if this gives the sub-child, child and parents, then must be built some kind of filter to show only the sub-child.

to the sub-districts I imagine something like:

$district = $product->get_attribute('pa_district');
$sub_district = pa_sub-district($district);
foreach($sub_district as $a){
   echo $a;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.