<?php
if (WC_ENABLED) {
  function confirm_order_cancel()
  {
?>
    <script type="text/javascript">
      (function($) {
        $('.order-item .link.cancel').click(function() {
          var txt;
          var r = confirm("Naozaj chcete zrušiť objednávku?!");
          if (r == true) {
            txt = "Stlačili ste tlačidlo OK.";
          } else {
            return false;
          }
          $(this).innerHTML = txt;
        });
      })(jQuery);
    </script>
<?php
  }
  add_action('wp_footer', 'confirm_order_cancel');


  /**
   * @snippet       Remove Dashboard from My Account
   * @author        Misha Rudrastyh
   * @link          https://rudrastyh.com/woocommerce/remove-dashboard-from-my-account-menu.html
   */
  // remove menu link
  add_filter('woocommerce_account_menu_items', 'misha_remove_my_account_dashboard');
  function misha_remove_my_account_dashboard($menu_links)
  {

    unset($menu_links['dashboard']);
    return $menu_links;
  }
  // perform a redirect
  add_action('template_redirect', 'misha_redirect_to_orders_from_dashboard');
  function misha_redirect_to_orders_from_dashboard()
  {
    // Skontrolujeme, či sa užívateľ nachádza na stránke účtu, endpoint je prázdny a užívateľ nie je prihlásený
    if (is_account_page() && empty(WC()->query->get_current_endpoint()) && !is_user_logged_in()) {
      // Ak URL obsahuje parameter `show-reset-form`, presmerovanie ignorujeme
      if (isset($_GET['show-reset-form']) && $_GET['show-reset-form'] === 'true') {
        return;
      }

      // Inak presmerujeme na stránku objednávok
      wp_safe_redirect(wc_get_account_endpoint_url('orders'));
      exit;
    }
  }
}

// my account orders per page to an unlimited value
add_filter('woocommerce_my_account_my_orders_query', 'my_account_orders_query', 20, 1);
function my_account_orders_query($args)
{
  $args['limit'] = -1;
  return $args;
}

// Display the product thumbnail in order view pages
add_filter('woocommerce_order_item_name', 'display_product_image_in_order_item', 20, 3);
function display_product_image_in_order_item($item_name, $item, $is_visible)
{
  // Targeting view order pages only
  if (is_wc_endpoint_url('view-order')) {
    $product       = $item->get_product(); // Get the WC_Product object (from order item)
    if ($product && !is_wp_error($product)) {
      $product_image = $product->get_image(array(36, 36)); // Get the product thumbnail (from product object)
      $item_name     = '<div class="item-thumbnail">' . $product_image . '</div>' . $item_name;
    }
  }
  return $item_name;
}
