Add TILBEHØR (accessories) to WC Tab on single product page

/**
 * Add TILBEHØR (accessories) tab to single product page
 */
function twstudio_accessories_tab( $tabs ) {
	$accessories = get_field( 'add_accessories' );
	if ( ! empty( $accessories ) ) {
		$tabs['desc_tab'] = array(
			'title'     => __( 'Tilbehør', 'twstudio_lang' ),
			'priority'  => 12,
			'callback'  => 'twstudio_show_cross_sell_in_single_product'
		);
	}
	return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'twstudio_accessories_tab' );

function twstudio_show_cross_sell_in_single_product(){
	$crosssells = get_post_meta( get_the_ID(), '_crosssell_ids',true);
	$args = array(
		'post_type' => 'product',
		'posts_per_page' => -1,
		'post__in' => $crosssells
	);
	$products = new WP_Query( $args );
	if( $products->have_posts() ) :
		echo '<div class="cross-sells">';
		woocommerce_product_loop_start();
		while ( $products->have_posts() ) : $products->the_post();
			wc_get_template_part( 'content', 'product' );
		endwhile; // end of the loop.
		woocommerce_product_loop_end();
		echo '</div>';
	endif;
	wp_reset_postdata();
}

Leave a Reply

Your email address will not be published. Required fields are marked *