Issue
I'm trying to add a "View more" button to external products on wordpress to link to the product page instead of to the external link. This way users can get more details before doing the external click (in this case it is a link to a booking system, not an affiliate).
I was able to add some code and the view more button shows now but only on the category pages example: https://faceup-romanorte.com/tratamiento-categoria/valoraciones/
It doesn't show tho on my other pages where I add a block to show wordpress products using Elementor
e.g. see my main page www.faceup-romanorte.com
Products are visible and if you click on them you get to the details page, but it is a bit confusing for users (we can see this through hotjar)
This is the code I added
//Modify
add_filter( 'woocommerce_loop_add_to_cart_link', 'ts_replace_add_to_cart_button', 10, 2 );
function ts_replace_add_to_cart_button( $button, $product ) {
if (is_product_category() || is_shop()) {
$button_text = __("Ver más", "woocommerce");
$button_link = $product->get_permalink();
$button = '<a class="button" href="' . $button_link . '">' . $button_text . '</a>';
return $button;
}
}
But it seems to be somehow only activating it for category pages and not on all other pages. Can someone help with this?
Category page before adding the code
When I add the code, the button does change on the category page
Category page with modified code ("Ver más" means "View Details")
But it hides the button from the main page which is also not ideal
Main page when adding code - buttons disappear
Could anyone help with what I am missing here?
Solution
For external products, to change the loop add to cart button on archive pages with a "View more button linked to the single product, use the following:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_external_product_loop_add_to_cart_link', 100, 3 );
function replace_external_product_loop_add_to_cart_link( $button, $product, $args ) {
// Only for external products
if ( $product->is_type('external') ) {
$button = sprintf( '<a href="%s" class="%s" %s>%s</a>',
esc_url( $product->get_permalink() ),
esc_attr( 'button' ),
isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
esc_html__( 'View more' )
);
}
return $button;
}
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
Answered By - LoicTheAztec
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.