0

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

3 Answers 3

0

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;
}
2
  • 1
    hi @tiago Thank you for your reply. I have done it. And yes it displays the entire category. But what I want is to show only sub-child categories and i dont know how i do.
    – carlozsan
    Oct 10, 2021 at 2:14
  • with loops feshing the info that is there. check the next answer Oct 10, 2021 at 18:50
0

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();
5
  • hi @tiago. I really appreciate your help. But the code still doesn't work properly. The code results like this. Please let me know if you still need information. Link : thank you very much
    – carlozsan
    Oct 10, 2021 at 19:22
  • and when you use this code $cats = $product->get_categories(); echo '<pre>'; print_r($cats); echo '</pre>'; die(); Oct 11, 2021 at 1:26
  • Please see now here @tiago...
    – carlozsan
    Oct 11, 2021 at 1:32
  • yes. right. I tested the next code on my single-product page with categories with 3 levels, assuming your city(sub-child) is a third level category. the code in the next answer should give you only the city. Oct 14, 2021 at 0:05
  • I change the approach because had tested $product->get_categories(); on single product page and only gives back the categories names associated, and doesnt have terms id and parents. also is outdated. the next with get_the_terms() already is updated with wordpress standards and if you run print_r you can view all the info that is fetch in the next answer. the next answer should resolve the issue and return only the city or cities if thats the case. Oct 14, 2021 at 0:30
0

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;
          }
        }
      }
    }
  }
}
3
  • hi @tiago You have done something very useful for me. I am very grateful for that. I've used the code snippet you provided and it looks like this. Or please take a look at my question again because I have updated it and hopefully it can explain what I want, Once again, thank you very much
    – carlozsan
    Oct 14, 2021 at 6:42
  • 1
    hi @tiago..thank you...i have invited you..
    – carlozsan
    Oct 14, 2021 at 9:36
  • accepted. my time is UTC+1, and yours? Oct 14, 2021 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.