<?php
$categories_headline = mb_get_block_field("popular_categories_headline");
$categories = mb_get_block_field("popular_categories");
if ($categories) { ?>
<section <?= inoby_block_attrs($attributes, ["class" => "inoby-popular-categories"]) ?>>
  <div class="container">
    <div class="row">
      <div class="col-12">
        <?php if ($categories_headline) {
          echo "<h2>" . $categories_headline . "</h2>";
        } ?>
        <div class="category-wrap">
        <?php foreach ($categories as $category) {
          $cat_terms = get_term_by("id", reset($category), "product_cat");
          $thumbnail_id = get_term_meta($cat_terms->term_id, "thumbnail_id", true);
          $image_url = wp_get_attachment_image_url($thumbnail_id, "s-4");
          $cat_url = get_term_link($cat_terms->term_id, "product_cat");
          if ($image_url) {
            echo '<a style="background:center/cover no-repeat url(' . $image_url . ');" href="' . $cat_url . '"><span>' . $cat_terms->name . "</span></a>";
          } else {
            echo '<a style="background-color:#fff;" href="' . $cat_url . '"><span>' . $cat_terms->name . "</span></a>";
          }
        } ?>
        </div>
      </div>
    </div>
  </div>
 </section>
 <?php }
