Title: Wordpress / WooCommerce - dynamic hyperlink based on current product
Tags: wordpress;hyperlink;woocommerce;contact-form-7
Question: I'm running WooCommerce in Wordpress here and on some products, I will be embedding a block of text which includes a hyperlink to a contact form. I would like the Subject on the contact form to populate with the name of the product where the hyperlink was clicked
I have established that I can populate a CF7 field by appending a query string to the URL (e.g. www...co.uk/contact-form/?subject=blah,blah), so if there was some method of dynamically altering the hyperlink in the aforementioned block of text when the product page is loaded so as to append the product name after the query string
Maybe to describe this a little clearer...
The block of text on the product page will look like this:
<a href='...co.uk/contact-form/?subject={NEED-TO-DYNAMICALLY-APPEND-PRODUCT-NAME-HERE}&gt;Contact us...</a&gt;
Any ideas folks of how I might approach this or has anyone tried something similar?
Thanks


Here is another answer: So you want the title  of product name ?
$product = wc_get_product( id );
$product_name = $product-&gt;get_title();
<a href='...co.uk/contact-form/?subject=<?php echo $product_name; ?&gt;'&gt;Contact us...</a&gt;
If you want the title of the page you can give this parameter to your href
$pageTitle = get_the_title();
Comment for this answer: Thanks tasos... this looks promising. Not sure where to place the above, I assume I need to wrap all up in PHP... the hyperlink, styling, etc and this becomes my block of text to show on the product page? The block is as follows:
Comment for this answer: Require Assistance?Click here to get in touch
Comment for this answer: Hmm... I can't seem to get the query strings to append, I see the "?php" text as above instead. Do I place "$product = wc_get_product( id );" and "$product_name = $product->get_title();" in functions.php or in the single-product page or where should I place it? I've tried functions but my pages don't load then. Sorry for being a pest
Comment for this answer: You can write it like this: " target="_blank" rel="noopener">Require Assistance?Click here to get in touch

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:03/07/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<wordpress><hyperlink><woocommerce><contact-form-7>', 'Title': 'Wordpress / WooCommerce - dynamic hyperlink based on current product', 'ViewCount': 425, 'id': 49157713, 'user_AboutMe': None, 'user_AccountId': 8898312.0, 'user_DisplayName': 'ConM', 'user_Reputation': 21.0, 'user_Views': 96.0, 'user_id': 6642520.0}}