Sync de product categorieën en maak hiervan custom post types. De afbeelding van de categorie wordt de uitgelichte afbeelding.

In functions.php (en draai elke 10 minuten automatisch)

 function wbmz_update_collecties_dmv_product_cat() {
      $taxonomies = get_terms( array(
        'taxonomy' => 'product_cat',
        'hide_empty' => false
      ) );
      if ( !empty($taxonomies) ) :
        foreach( $taxonomies as $categorie ) {
          if( $categorie->parent == 0 ) {
            $collectie = get_field('collectie', $categorie);
            $pagina = get_field('pagina', $categorie);
            $thumbnail_id = get_term_meta( $categorie->term_id, 'thumbnail_id', true ); 
            $collectie_bestaat = wbmz_post_exists_by_slug($categorie->name,'collecties');
            // indien de collectie niet is gevuld bij de product categorie niets doen
            if( $categorie->slug == 'geen-categorie'){
               continue;
            }
            if(!empty($collectie_bestaat)):
               //echo "bestaat wel id={$collectie_bestaat} dus GEEN post maken maar UPDATE.";
               //echo 'update collectie';
               update_field( 'product_categorie', $categorie->term_id, $post_id );
               set_post_thumbnail( $post_id, $thumbnail_id );
               if(!empty($pagina)):
                  update_field( 'pagina', $pagina[0], $post_id );
               endif;
            else:
               //echo "bestaat niet dus WEL post aanmaken. ";
               $new_post = array(
                'post_title' => $categorie->name,
                'post_content' => '',
                'post_status' => 'publish',
                'post_type' => 'collecties'
               );
               $post_id = wp_insert_post( $new_post );
               if($post_id):
                  // als post is toegevoegd
                  update_field( 'product_categorie', $categorie->term_id, $post_id );
                  update_field( 'pagina', $pagina, $post_id );
                  set_post_thumbnail( $post_id, $thumbnail_id );
               else:
                 // niet gelukt om aan te maken
               endif;
      
            endif;
            
          }
        }
      endif;      
}
    if (!wp_next_scheduled("update_update_collecties")) {
            wp_schedule_event(1, "wbmz_10_minuten", "update_update_collecties");
        }
   add_action( 'update_update_collecties', 'wbmz_update_collecties_dmv_product_cat' );
Geef een reactie 0