[Resolved] WooCommerce: Product link dynamic in Query Loop block button

Home Forums Support [Resolved] WooCommerce: Product link dynamic in Query Loop block button

Home Forums Support WooCommerce: Product link dynamic in Query Loop block button

  • This topic has 11 replies, 2 voices, and was last updated 1 month ago by Ying.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #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,
    Stephan

    #2276394
    Ying
    Staff
    Customer Support

    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?

    #2277027
    Stephan

    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?

    #2277352
    Ying
    Staff
    Customer Support

    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!

    #2277448
    Stephan

    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!

    #2277499
    Ying
    Staff
    Customer Support

    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 );
    #2279362
    Stephan

    Hello Ying,

    unfortunately it does not work. The button still links to the WooCommerce product page.

    #2279739
    Ying
    Staff
    Customer Support

    Any chance you can start a staging site and provide the admin login, so we can do some testing there?

    #2279791
    Stephan

    Sure thing. First two blue buttons on the left side.

    Thank you!

    #2279797
    Ying
    Staff
    Customer Support

    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 ๐Ÿ™‚

    #2279956
    Stephan

    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!

    #2279967
    Ying
    Staff
    Customer Support

    You are welcome!

    So don’t work too late, go enjoy life more! ๐Ÿ˜›

    Glad to help!

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.