WC_Template_Loader::unsupported_theme_tax_archive_init()private staticWC 3.3.0

Enhance the unsupported theme experience on Product Category and Attribute pages by rendering those pages using the single template and shortcode-based content. To do this we make a dummy post and set a shortcode as the post content. This approach is adapted from bbPress.

Method of the class: WC_Template_Loader{}

No Hooks.

Return

null. Nothing (null).

Usage

$result = WC_Template_Loader::unsupported_theme_tax_archive_init();

Changelog

Since 3.3.0 Introduced.

WC_Template_Loader::unsupported_theme_tax_archive_init() code WC 8.7.0

private static function unsupported_theme_tax_archive_init() {
	global $wp_query, $post;

	$queried_object = get_queried_object();
	$args           = self::get_current_shop_view_args();
	$shortcode_args = array(
		'page'     => $args->page,
		'columns'  => $args->columns,
		'rows'     => $args->rows,
		'orderby'  => '',
		'order'    => '',
		'paginate' => true,
		'cache'    => false,
	);

	if ( is_product_category() ) {
		$shortcode_args['category'] = sanitize_title( $queried_object->slug );
	} elseif ( taxonomy_is_product_attribute( $queried_object->taxonomy ) ) {
		$shortcode_args['attribute'] = sanitize_title( $queried_object->taxonomy );
		$shortcode_args['terms']     = sanitize_title( $queried_object->slug );
	} elseif ( is_product_tag() ) {
		$shortcode_args['tag'] = sanitize_title( $queried_object->slug );
	} else {
		// Default theme archive for all other taxonomies.
		return;
	}

	// Description handling.
	if ( ! empty( $queried_object->description ) && ( empty( $_GET['product-page'] ) || 1 === absint( $_GET['product-page'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$prefix = '<div class="term-description">' . wc_format_content( wp_kses_post( $queried_object->description ) ) . '</div>';
	} else {
		$prefix = '';
	}

	add_filter( 'woocommerce_shortcode_products_query', array( __CLASS__, 'unsupported_archive_layered_nav_compatibility' ) );
	$shortcode = new WC_Shortcode_Products( $shortcode_args );
	remove_filter( 'woocommerce_shortcode_products_query', array( __CLASS__, 'unsupported_archive_layered_nav_compatibility' ) );
	$shop_page = get_post( self::$shop_page_id );

	$dummy_post_properties = array(
		'ID'                    => 0,
		'post_status'           => 'publish',
		'post_author'           => $shop_page->post_author,
		'post_parent'           => 0,
		'post_type'             => 'page',
		'post_date'             => $shop_page->post_date,
		'post_date_gmt'         => $shop_page->post_date_gmt,
		'post_modified'         => $shop_page->post_modified,
		'post_modified_gmt'     => $shop_page->post_modified_gmt,
		'post_content'          => $prefix . $shortcode->get_content(),
		'post_title'            => wc_clean( $queried_object->name ),
		'post_excerpt'          => '',
		'post_content_filtered' => '',
		'post_mime_type'        => '',
		'post_password'         => '',
		'post_name'             => $queried_object->slug,
		'guid'                  => '',
		'menu_order'            => 0,
		'pinged'                => '',
		'to_ping'               => '',
		'ping_status'           => '',
		'comment_status'        => 'closed',
		'comment_count'         => 0,
		'filter'                => 'raw',
	);

	// Set the $post global.
	$post = new WP_Post( (object) $dummy_post_properties ); // @codingStandardsIgnoreLine.

	// Copy the new post global into the main $wp_query.
	$wp_query->post  = $post;
	$wp_query->posts = array( $post );

	// Prevent comments form from appearing.
	$wp_query->post_count    = 1;
	$wp_query->is_404        = false;
	$wp_query->is_page       = true;
	$wp_query->is_single     = true;
	$wp_query->is_archive    = false;
	$wp_query->is_tax        = true;
	$wp_query->max_num_pages = 0;

	// Prepare everything for rendering.
	setup_postdata( $post );
	remove_all_filters( 'the_content' );
	remove_all_filters( 'the_excerpt' );
	add_filter( 'template_include', array( __CLASS__, 'force_single_template_filter' ) );
}