Archived topic
WooCommerce: Product link dynamic in Query Loop block button
11 replies · Started by Stephan on July 7, 2022
Hello guys,
I am moving from WP Show Posts to the new Generate Blocks and am amazed by your work!
I ran into one issue though: When creating a custom block with a button I cannot make the link of the button dynamically change. It should match the link of the WooCommerce product.
What I did so far was activating "Dynamic Data > Current post > Post meta" and in Post meta field I put "_product_url". That gives the correct link for a specific WooCommerce product, but it is not clickable, the whole link is shown and it is therefore not a button anymore.
With WP Show posts you have helped me with this snippet:
add_filter( 'wpsp_read_more_output', function( $output, $settings ) {
$productURL = get_post_meta( get_the_ID(),'_product_url', false );
if( 1987 === (int) $settings["list_id"]){
return sprintf(
'<div class="wpsp-read-more"><a title="%1$s" class="%4$s" href="%2$s">%3$s</a></div>',
the_title_attribute( 'echo=0' ),
$productURL[0],
__( 'Read2 more', 'wp-show-posts' ),
esc_attr( $settings['read_more_class'] )
);
}
return $output;
}, 10, 2 );
Is this also possible in combination with the Query Loop block?
Thanks a lot,
Stephan
Hi Stephan,
You just need to select single post as the Link source, leave the content source blank, I assume the button text will be static? eg. learn more?
Hello Ying,
thanks for your support! I realize that I described the situation wrong, my bad. I do not want to have a link to the WooCommerce product itself (that works with your suggestion) but to the affiliate link that the WooCommerce external product has. So instead of Button > WooCommerce product > External link I want the button directly carry the external link, without going to the WooCommerce product first.
Is that possible?
I'm not sure how you call the external link, do you know where the link is stored? Is it stored as post meta?
I'll need more info, can you provide it?
Let me know!
Yes it is stored in post meta. I call "_product_url" that is the official WooCommerce name for that field. When I make a WooCommerce product that is the link for external affiliate products.
As I said that works for calling the link from the WooCommerce product, but it just gets copied as complete URL into the button, and is not clickable.
Thanks for any suggestions!
Not sure if this will work, give it a try and let me know:
add_filter( 'generateblocks_dynamic_url_output', function( $url, $attributes ) {
if ( 'post-meta' === $attributes['dynamicLinkType'] && isset( $attributes['linkMetaFieldName'] ) ) {
if ( '_product_url' === $attributes['linkMetaFieldName'] ) {
$productURL = get_post_meta( get_the_ID(),'_product_url', true );
if( $productURL ):
$url = get_permalink($productURL->ID);
return $url;
endif;
}
}
return $url;
}, 10, 2 );
Hello Ying,
unfortunately it does not work. The button still links to the WooCommerce product page.
Any chance you can start a staging site and provide the admin login, so we can do some testing there?
Sure thing. First two blue buttons on the left side.
Thank you!
I tested with the correct setting for the button:
1. Choose Post meta as link source.
2. Enter _product_url as the meta field name.
It worked right away without activating the PHP snippet.
https://www.screencast.com/t/6qOvMGjwWW
Do the same for the other button, you can remove the PHP snippet :)
Hello Ying,
I could have sworn that is how I started. Blame it on my stupidity - maybe I am working too late.
Thank you so much for your friendly support!
You are welcome!
So don't work too late, go enjoy life more! :P
Glad to help!