- This topic has 11 replies, 2 voices, and was last updated 2 years, 6 months ago by
Ying.
-
AuthorPosts
-
July 7, 2022 at 4:01 am #2275884
Stephan
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,
StephanJuly 7, 2022 at 10:24 am #2276394Ying
StaffCustomer SupportHi Stephan,
You just need to select
single post
as theLink source
, leave thecontent source
blank, I assume the button text will be static? eg. learn more?July 8, 2022 at 4:19 am #2277027Stephan
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?
July 8, 2022 at 9:46 am #2277352Ying
StaffCustomer SupportI’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!
July 8, 2022 at 12:38 pm #2277448Stephan
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!
July 8, 2022 at 2:51 pm #2277499Ying
StaffCustomer SupportNot 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 );
July 11, 2022 at 6:22 am #2279362Stephan
Hello Ying,
unfortunately it does not work. The button still links to the WooCommerce product page.
July 11, 2022 at 10:40 am #2279739Ying
StaffCustomer SupportAny chance you can start a staging site and provide the admin login, so we can do some testing there?
July 11, 2022 at 11:56 am #2279791Stephan
Sure thing. First two blue buttons on the left side.
Thank you!
July 11, 2022 at 12:07 pm #2279797Ying
StaffCustomer SupportI 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 ๐
July 11, 2022 at 4:26 pm #2279956Stephan
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!
July 11, 2022 at 4:43 pm #2279967Ying
StaffCustomer SupportYou are welcome!
So don’t work too late, go enjoy life more! ๐
Glad to help!
-
AuthorPosts
- You must be logged in to reply to this topic.