$brand_tax_query = array(
		'relation' => 'OR',
	);

	foreach ( $possible_taxonomies as $taxonomy ) {

		if ( ! taxonomy_exists( $taxonomy ) ) {
			continue;
		}

		foreach ( $brand_slugs as $brand_slug ) {
			if ( ! term_exists( $brand_slug, $taxonomy ) ) {
				continue;
			}

			$brand_tax_query[] = array(
				'taxonomy' => $taxonomy,
				'field'    => 'slug',
				'terms'    => array( $brand_slug ),
			);
		}
	}

	// Ако няма намерена марка, не показваме пълния каталог по грешка.
	if ( count( $brand_tax_query ) <= 1 ) {
		$query->set( 'post__in', array( 0 ) );
		return;
	}

	$tax_query = (array) $query->get( 'tax_query' );

	$tax_query[] = $brand_tax_query;

	$query->set( 'post_type', 'product' );
	$query->set( 'tax_query', $tax_query );
}

/*--------------------------------------------------------------
# BSG - Auto Add Empty Product Attributes
# Adds predefined global attributes to each product on save.
# Values remain empty and are filled manually.
--------------------------------------------------------------*/

add_action( 'save_post_product', 'bsg_auto_add_empty_product_attributes', 20, 3 );

function bsg_auto_add_empty_product_attributes( $post_id, $post, $update ) {

	// Prevent autosave/revision issues
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}

	if ( wp_is_post_revision( $post_id ) ) {
		return;
	}

	if ( get_post_type( $post_id ) !== 'product' ) {
		return;
	}

	// Attribute slugs from Products -> Attributes
	$default_attributes = array(
		'tip',
		'kvadratura',
		'btu',
		'cooling-class',
		'heating-class',
		'cooling-power',
		'heating-power',
		'cooling-consumption',
		'heating-consumption',
		'scop',
		'refrigerant',
		'origin',
		'warranty',
	);

	$product_attributes = get_post_meta( $post_id, '_product_attributes', true );

	if ( ! is_array( $product_attributes ) ) {
		$product_attributes = array();
	}

	$position = count( $product_attributes );
	$changed  = false;

	foreach ( $default_attributes as $attribute_slug ) {

		$taxonomy = 'pa_' . $attribute_slug;

		// Add only existing global WooCommerce attributes
		if ( ! taxonomy_exists( $taxonomy ) ) {
			continue;
		}

		// Skip if already added to this product
		if ( isset( $product_attributes[ $taxonomy ] ) ) {
			continue;
		}

		$product_attributes[ $taxonomy ] = array(
			'name'         => $taxonomy,
			'value'        => '',
			'position'     => $position,
			'is_visible'   => 1,
			'is_variation' => 0,
			'is_taxonomy'  => 1,
		);

		$position++;
		$changed = true;
	}

	if ( $changed ) {
		update_post_meta( $post_id, '_product_attributes', $product_attributes );
	}
}<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="//bsg-ac.com/main-sitemap.xsl"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<sitemap>
		<loc>https://bsg-ac.com/page-sitemap.xml</loc>
		<lastmod>2026-07-13T10:08:41+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://bsg-ac.com/product_brand-sitemap.xml</loc>
		<lastmod>2026-09-15T16:16:22+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://bsg-ac.com/product_cat-sitemap.xml</loc>
		<lastmod>2026-09-15T16:16:22+00:00</lastmod>
	</sitemap>
</sitemapindex>
<!-- XML Sitemap generated by Rank Math SEO Plugin (c) Rank Math - rankmath.com -->