Skip to content

Instantly share code, notes, and snippets.

@runezero
Created October 17, 2022 18:56
Show Gist options
  • Save runezero/518c623d02a5ded7e746857d66b24e88 to your computer and use it in GitHub Desktop.
Save runezero/518c623d02a5ded7e746857d66b24e88 to your computer and use it in GitHub Desktop.
[Intrax search bar] The custom search bar for Intrax for searching on car brand/models #woocommerce
add_shortcode('dsm_custom_sub_header_bar', 'dsm_custom_sub_header_bar_func');
function dsm_custom_sub_header_bar_func()
{
global $woocommerce;
$cat_args = array(
'orderby' => 'name',
'order' => 'asc',
'hide_empty' => true,
'parent' => 0
);
$blog_id = get_current_blog_id();
switch_to_blog($blog_id);
$product_categories = get_terms('product_cat', $cat_args);
ob_start(); ?>
<div class='dsm-sub-header'>
<div class="container">
<div class='left'>
<p style="line-height:35px;">
<?php if (!is_front_page()) {
echo do_shortcode('[wpseo_breadcrumb]');
} ?>
</p>
</div>
<div class='right '>
<div style="line-height:35px;">
<p><?= __('Zoek uw product') ?>:</p>
<select name="dsm_merk" id="dsm_merk" class="dsm_merk hide-mobile">
<?php
$cookie_name = "intrax_category_search_parent";
if (isset($_COOKIE[$cookie_name])) {
echo $_COOKIE[$cookie_name];
} else { ?>
<option value=""><?= __('Selecteer een merk') ?></option>
<?php
}
?>
<?php if (!empty($product_categories)): ?>
<?php foreach ($product_categories as $key => $category) :
$term_link = get_term_link($category, 'product_cat');
if ($blog_id == 2) {
$term_link = str_replace("/producten/", "/products/", $term_link);
}
?>
<option value="<?= $category->slug; ?>"
data-link="<?= $term_link; ?>"><?= $category->name; ?></option>
<?php endforeach ?>
<?php endif ?>
</select>
<select name="dsm_model" id="dsm_model" class="dsm_model hide-mobile">
<?php
$cookie_name = "intrax_category_search_child";
if (isset($_COOKIE[$cookie_name]) && isset($_COOKIE['intrax_category_search_parent'])) {
echo $_COOKIE[$cookie_name];
$parent_cookie_slug = explode('"', $_COOKIE['intrax_category_search_parent'])[1];
echo get_all_child_categories_from_parent($parent_cookie_slug);
} else { ?>
<option value=""><?= __('Selecteer een model') ?></option>
<?php
}
?>
</select>
<?php
$search_button_url = "/zoek-producten/";
if ($blog_id != 1) {
$search_button_url = "/en/search-products/";
}
$cookie_name = "intrax_category_search_url";
if (isset($_COOKIE[$cookie_name]) && !empty($_COOKIE[$cookie_name])) {
$search_button_url = $_COOKIE[$cookie_name];
} ?>
<a id="dsm_product_search" class='button button-primary dsm_product_search'
href="<?= $search_button_url; ?>"><?= __('Zoek producten') ?></a>
</div>
</div>
</div>
</div>
<?php restore_current_blog();
$result_html = ob_get_contents();
ob_end_clean();
return $result_html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment