0

I need to create this type of relation in Wordpress:

  • A Catalog can have many Products
  • A Product can have many Catalogs
  • A Category can have many Products
  • A Product can have only one Category

So when I click on the Catalog, I need to see all the Categories of the Products that I have linked with the Catalog.

E.g:

  • Catalog 1 -> Product 1 [Category 1]
  • Catalog 1 -> Product 2 [Category 1]
  • Catalog 1 -> Product 3 [Category 2]
  • Catalog 2 -> Product 2 [Category 1]

So in the frontend, when I click on Catalog 1, I should see the Categories: - Category 1 - Category 2

Then when I click the Category 1 (of Catalog 1) I should see the Products: - Product 1 - Product 2

If I click on Catalog 2, I should see the Categories: - Category 1

Then when I click the Category 1 (of Catalog 2), I should see the Products: - Product 2

How can I rapresent these relations on Wordpress + ACF?

5
  • Is the catalog as a custom taxonomy or a custom page? or just a select input in metabox? – Manyang Feb 14 '19 at 9:31
  • @Manyang for the moment is a custom post type, but If could be useful to resolve the problem, I can change it to a taxonomy. I'm on analysis phase for the moment, so I can do any change. – Mintendo Feb 14 '19 at 9:36
  • if you dont have problem to make catalog to taxonomy, it will be easier, because you now have a relationship between category and taxonomy through the product id – Manyang Feb 14 '19 at 9:40
  • @Manyang yes, no problem to change a catalog from custom post type to taxonomy, but I haven't understood how can I get the categories that are linked to catalog throught products. – Mintendo Feb 14 '19 at 9:45
  • Wait i will give you answer – Manyang Feb 14 '19 at 9:46
0

I not try this, but I hope this working

function get_list_catalog_category_product(){
  //get all catalog
  $catalogs = get_terms( array(
    'taxonomy' => 'catalogs_term',
    'hide_empty' => false
  ));

  if ( !empty($catalogs) ) :
  //make list catalog
  $output = '<ul>';

  foreach( $catalogs as $catalog ) {

      //write all catalog
      $output.= '<li><a href="'.get_term_link( $catalog ).'">'.$catalog->name.'</a>';

      //find all product related the catalog
      $args = array(
          'post_type' => 'product_post',
          'post_status' => 'publish',
          'tax_query' => array(
              array(
                  'taxonomy' => 'catalogs_term',
                  'field'    => 'term_id',
                  'terms'    => $catalog->term_id
              )
          )
      );
      $products = get_posts( $args );

      //make array of category related the catalog
      $all_category = array();

      //make array of product divided by category related the catalog
      $product_by_cat = array();

      if ( !empty($products) ){
        foreach( $products as $product ) {

          //write every product html
          $products_html ='<li><a href="'.get_permalink($product->ID).'">'.$product->post_title.'</a></li>';

          //find all category of this product
          $product_categories = wp_get_object_terms( $product->ID,  'category' );
          if ( ! empty( $product_categories ) ) {
            foreach($product_categories as $product_category) {

                //write the product html in related category if category never created this code will create or if has been create product html just append
                if(isset($product_by_cat[$product_category->term_id])){
                  $product_by_cat[$product_category->term_id] .= $products_html;
                }else{
                  $product_by_cat[$product_category->term_id] = $products_html;
                }

                //push the category as category related to catalog
                array_push($all_category,$product_category->term_id);
            }
          }

          //remove duplicate category if a product has more than 1 category
          $all_category = array_unique($all_category);
        }
      }

      if ( !empty($all_category) ){
        $output.= '<ul>';
          foreach( $all_category as $the_category ) {
            //write category html in catalog
            $output.='<li><a href="'.get_term_link($the_category).'">'.get_term( $term_id, 'category' )->name.'</a></li>';

            //write all product html to the category
            $output.='<ul>'.$product_by_cat[$the_category].'</ul>';
          }
        $output.= '</ul>';
      }

      $output.= '</li>';
  }
  $output.='</ul>';
  echo $output;
  endif;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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