Skip to content

Instantly share code, notes, and snippets.

@pelmered
Last active September 6, 2015 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pelmered/2e6041e2dffa7af496de to your computer and use it in GitHub Desktop.
Save pelmered/2e6041e2dffa7af496de to your computer and use it in GitHub Desktop.
Add info link based on product category [WooCommerce]
<?php
add_action( 'woocommerce_single_product_summary', 'my_add_read_more_about_category', 25);
function my_add_read_more_about_category()
{
global $product;
$cat_post_map = array(
// category id => array(<page id to link to>, <category plural label (optional)>)
173 => array( 449, 'cyklar' ),
222 => array( 449, 'bilar' )
);
$category_ids = array_keys(get_the_terms( $product->id, 'product_cat' ));
foreach( $cat_post_map AS $cat_id => $data )
{
if( in_array( $cat_id, $category_ids ) )
{
$post_id = $data[0];
// Get category label
if( isset( $data[1] ) )
{
$cat_name = $data[1];
}
else
{
$cat = get_term( $cat_id, 'product_cat' );
$cat_name = $cat->name;
}
echo '<a class="button" href="'.get_permalink( $post_id ).'">'.sprintf( __('Läs mer om %s'), $cat_name ).'</a>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment