Last active
September 6, 2015 12:23
-
-
Save pelmered/2e6041e2dffa7af496de to your computer and use it in GitHub Desktop.
Add info link based on product category [WooCommerce]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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