Home Forums WoodMart support forum Theme Presets Not Working For Terms Attributes eg: Brand Reply To: Theme Presets Not Working For Terms Attributes eg: Brand

#583483

Luke Nielsen
Keymaster

Hello,

Here is the updated code, and it works well – https://prnt.sc/1lIN2edF6xZK

add_filter(
	'xts_active_options_presets',
	function ( $active_presets, $all ) {
		foreach ( $all as $preset ) {
			if ( empty( $preset['condition'] ) || empty( $preset['condition']['rules'] ) ) {
				continue;
			}

			$rules = $preset['condition']['rules'];
			foreach ( $rules as $rule ) {
				$is_active = false;

				switch ( $rule['type'] ) {
					case 'term_id':
						$object  = get_queried_object();
						$term_id = false;

						if ( is_object( $object ) && property_exists( $object, 'term_id' ) ) {
							$term_id = get_queried_object()->term_id;
							$type    = get_queried_object()->taxonomy;

							$ancestors = get_ancestors( $term_id, $type );

							if ( in_array( $rule['value_id'], $ancestors, false ) ) { // phpcs:ignore
								$term_id = $rule['value_id'];
							}
						} else if ( isset( $_GET['filter_brand'] ) ) {
							$taxonomy = 'pa_brand';
							$term     = get_term_by( 'slug', $_GET['filter_brand'], $taxonomy );
							$term_id  = $term->term_id;

							$ancestors = get_ancestors( $term_id, $taxonomy );

							if ( in_array( $rule['value_id'], $ancestors, false ) ) { // phpcs:ignore
								$term_id = $rule['value_id'];
							}
						}

						if ( $term_id ) {
							$condition = (int) $term_id === (int) $rule['value_id'];
							$is_active = 'equals' === $rule['comparison'] ? $condition : ! $condition;
						}
						break;
				}

				if ( isset( $_GET['page'] ) && isset( $_GET['preset'] ) && 'xts_theme_settings' === $_GET['page'] ) { // phpcs:ignore
					$is_active    = true;
					$preset['id'] = $_GET['preset']; // phpcs:ignore
				}

				if ( $is_active ) {
					$priority                    = isset( $preset['priority'] ) ? $preset['priority'] : '';
					$active_presets[ $priority ] = $preset['id'];
				}
			}
		}

		foreach ( $all as $preset ) {
			if ( isset( $_GET['opts'] ) && $preset['name'] === $_GET['opts'] ) { // phpcs:ignore
				$active_presets[] = $preset['id'];
			}
		}

		ksort( $active_presets );

		return array_unique( $active_presets );
	},
	10,
	2
);

Kind Regards