Reputation: 67
How to add a custom button with link to main category inside "added to cart" message on single product page of the currect product.
For clarity of the position of the custom button, see image:
I know how to add button with link to main shop page.
add_filter( 'wc_add_to_cart_message_html', 'my_custom_add_to_cart_message' );
function my_custom_add_to_cart_message( $message ) {
$message .= sprintf( '<a href="%s" class="button wc-forward back-to-shop" style="margin-right: 15px;">%s</a>', get_permalink( woocommerce_get_page_id( 'shop' ) ), 'Continue');
return $message;
}
But i don't know how to make a link to last or main product category.
Using Woocommerce Continue Shopping Button Link to Last Product Category Page answer code does not work for me as it is not quite what I am looking for.
Upvotes: 0
Views: 350
Reputation: 29640
To add a button with a link to the main category, inside the "Product added to cart message" on the single product page you can use:
function filter_wc_add_to_cart_message ( $message, $product_id ) {
// Retrieves the terms for a post
$terms = wp_get_post_terms( $product_id, 'product_cat', array( 'include_children' => false ) );
// First main product category
$term = reset( $terms );
// Link
$term_link = get_term_link( $term->term_id, 'product_cat' );
// Button
$term_button = '<a href="' . $term_link . '" class="button">My button</a>';
return $message . $term_button;
}
add_filter( 'wc_add_to_cart_message', 'filter_wc_add_to_cart_message', 10, 2 );
Note: Custom CSS and minor html changes may be required, theme dependent
Upvotes: 1