Wordpress / WooCommerce - dynamic hyperlink based on current product - wordpress

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}>Contact us...</a>
Any ideas folks of how I might approach this or has anyone tried something similar?
Thanks

So you want the title of product name ?
$product = wc_get_product( id );
$product_name = $product->get_title();
<a href='...co.uk/contact-form/?subject=<?php echo $product_name; ?>'>Contact us...</a>
If you want the title of the page you can give this parameter to your href
$pageTitle = get_the_title();

Related

ACF field in function.php

Following the request of an SEO agency, I'm trying to recover the content of a field (that I created via ACF in the product categories of WooCommerce) in a function of the functions.php file. I've tried a few tricks found on the net, but nothing works, I can't recover the content.
It's to replace the product category title by another one (the one of the new ACF field) and that this title is only displayed on the category archives of the site (not in the menus and other widgets where the default title is displayed.
Here is the code I did. I get the ID back fine if I display it, I make progress but nothing for $value
<?php
$query_id = get_queried_object_id();
function titre_seo_cat_prod() {
// recupere la valuur d'un post specifique
$value = get_field( 'titre_seo_categorie_de_produit', $query_id );
return $value;
}
?>

How to get a custom value using Contact Form 7 - Dynamic Text Extension

I have a $_SESSION array variable with post ids. Inside foreach loop, I would like to get the posts titles of these ids. Thus so far I have something like this:
sport_title = '';
foreach($_SESSION['sports_post_id'] as $sports_id {
$sport_title = get_the_title($sport_id);
$sports_titles .= $sport_title . "<br />";
}
Now, my problem is that I do not know how to pass it in a custom variable in Contact Form 7 - Dynamic Text Extension plugin.
I have inside my form this field (inside CF7):
[dynamichidden dynamic_sports readonly default:shortcode_attr]
and inside my custom page template php file:
echo do_shortcode('[contact-form-7 id="3561" "CF7_get_custom_field dynamic_sports=\'$sports_titles\'" title="Availability Form EN"]');
Thus, I would like to send these post titles in email.. How can I make it work? thanks in advance
ok I figure it out how to do it! If anyone wants more explanation:
Inside Contact Form 7 - Form tab, I have insert this code:
[dynamichidden dynamic_sports "CF7_GET key='sports_post_id'"]
where key is a standard word (could not change it).
Inside Email tab, you should have this code:
Sports: [dynamic_sports]
Now, inside my Custom template PHP file, I have this shortcode:
echo do_shortcode('[contact-form-7 id="3561" title="Availability Form EN"]');
I also have a form with a hidden input type, with a name sports_post_id and value the id of the current post:
<input type="hidden" value="<?php echo get_the_title( get_the_ID() ); ?>" name="sports_post_id" id="sports_post_id" />
EDITED
Another solution via plugin that extends the CF7, would be the following:
Install Contact Form 7 - Dynamic Text Extension
Copy and paste the form-tag code below and then add it inside the form code block
[dynamichidden page-title "CF7_get_post_var key='title'"]
The above code will add a hidden text input to the form which will pre-populate the page title. This is good to use when you are using the same contact form on multiple pages so you know where the user has submitted the form from. Alternatively, you can display the page URL or slug using one of the below shortcodes instead:
[dynamichidden page-url "CF7_bloginfo show='url'"]
[dynamichidden page-slug "CF7_bloginfo show='url'"]
Displaying the Hidden Dynamic Content Tag Variable in Contact Form 7
Finally, display the hidden dynamic content tag variable in Contact Form 7 form. While you are on the CF7 settings page, click on the "Email" tab and insert this:
[page-title]
If you are using the URL or Slug fields, you these instead:
[page-url]
[page-slug]
In your CF7 Form configuration > Email Tab, you only have to add the desired field between hooks [...]
[dynamic_sports]
This will print the dynamic field value in your email.

Add Page Title to source key in url for use in a form - Wordpress Website

I am trying to add a waitlist button functionality to a page on my wordpress website. Basically I need to autopopulate the source key in the query string in a url with the page title.
More Details:
I have the button linking to a general waitlist form but I want the product information from the page title go into a text field. I can do this by using a source key in the url.
We have our product listed on a portfolio post here with a waitlist button: http://www.inventivewebdesign.com/renohifi/listings_portfolio/pass-labs-xa-160-8-monoblock-power-amps/
The button goes to a form here with a text field that auto populates with the source key in the url.
So if I use the link: http://www.inventivewebdesign.com/renohifi/waitlist-request/?source=Pass%20Labs%20XA-160.8%20Monoblock%20Power%20Amps, The source key will populate the the product text field with "Pass Labs XA-160.8 Monoblock Power Amps". This all works great!
Now, I want to automate the code for the button so it always pulls the Page title as the source key so we don't have to manually enter in code for each button.
How can I get the page title to auto-populate the query string in the link url so that the link will be http://www.inventivewebdesign.com/renohifi/waitlist-request/?source={PAGE_TITLE}?
FYI - I am using the Visual Composer plugin for page layout and button creation.
UPDATE:
I am trying to use code like this:
<div class="vc_btn3-container vc_btn3-center">
<a title="Waitlist - <?php the_title_attribute(); ?>" href="http://www.inventivewebdesign.com/renohifi/waitlist-request/?source=<?php echo get_the_title(); ?>">
Add Me to the Waitlist
</a>
</div>
It is in the Wordpress editor so the code is not showing up as anything other than code. I want the title to show (I am trying two different wordpress calls for the page title). You can see this in the 2nd "Add Me to the Wishlist" Button on the first link above.
In JavaScript you can get the page title from the DOM using document.title, the value is encoded using the encodeURIComponent() function.
Your URL as a JavaScript string:
var url = "http://www.inventivewebdesign.com/renohifi/waitlist-request/?source=" + encodeURIComponent(document.title);
I couldn't find any reference to the URL on the page to which you linked.
EDIT: To encode the title string using PHP you need to use the urlencode() function.
Template code:
<div class="vc_btn3-container vc_btn3-center">
<a title="Waitlist - <?php the_title_attribute(); ?>" href="http://www.inventivewebdesign.com/renohifi/waitlist-request/?source=<?php echo urlencode(get_the_title()); ?>">
Add Me to the Waitlist
</a>
</div>
I did it by adding a shortcode instead:
function shortcode_waitlist( $atts ){
$pagetitle = get_the_title();
$link = '<div class="vc_btn3-container vc_btn3-center"><a class="vc_general vc_btn3 vc_btn3-size-lg vc_btn3-shape-rounded vc_btn3-style-modern vc_btn3-block vc_btn3-color-primary" href="http://www.inventivewebdesign.com/renohifi/waitlist-request/?source='.$pagetitle.'" title="Waitlist - '.$pagetitle.'">Add Me to the Waitlist</a></div>';
return $link;
}
add_shortcode( 'waitlist_button', 'shortcode_waitlist' );
Then just used [waitlist_button] in the page.
Thanks for your suggestions, they helped me get to where I needed to go.

Display additional product information (e.g. image caption) when hovering product image

I'm using using the WooCommerce plugin for WordPress to display my products. The thing is, when you are viewing the product category (archive), you can see the product name, image and price, but that doesn't really say all that much about exactly what the product is.
What I would like is for some more information to become available when you hover the product images. Something a bit like this.
Would it be possible to retrieve some of the information about the image, that I can enter in the WordPress media libray: title, caption alt text or description?
You can check the webshop here.
EDIT:
I found that editing the content-product.php file in the WooCommerce plugin folder, if I put this:
?>
random text
<?php
somewhere inside the php tags in the <li> section of that file, I could get 'random text' to show either above or below the product image on the product archive page. So, if I could replace that with a function that would retreive for instance the product image caption or some custom field that I can fill out for each product, that would go a long way towards solving the issue.
So, if anyone knows of a function that does this, please share it here.
hello sir just use this plugin
https://wordpress.org/plugins/woocommerce-custom-product-data-fields/
for retrieving any costume fields do like this
Retrieving multiselect value
global $wc_cpdf;
$multiselect = $wc_cpdf->get_value($post->ID, '_mymultiselect');
foreach ($multiselect as $value) {
echo $value;
}
Retrieving image value
global $wc_cpdf;
$image_id = $wc_cpdf->get_value($post->ID, '_myimage');
$size = 'thumbnail';
$image_attachment = wp_get_attachment_image($image_id, $size);
echo $image_attachment;
}
I recently did this with a website for a client. I changed the hover text for each product from "Add to Cart" to the product short description. Here is my code:
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // 2.1 +
function woo_archive_custom_cart_button_text() {
//get the product object
global $product;
$id = $product->get_id();
$descript = $product->get_short_description();
//take out html from description
$descript1 = strip_tags($descript);
//shorten description length to fit the product image box
if (strlen($descript1) > 100){
$descript1 = substr($descript1, 0, 99) . '...';
}
return __( $descript1, 'woocommerce' );
}

Wordpress: add custom page attribute

I have the NextGEN Gallery plugin on my wordpress site. Normally I would add this short code in my page content to display gallery items:
[ nggallery id=5 template=custom ]
Now I'd like to replace this by adding custom fields in the Page Attributes setting when you are adding/editing a page. The custom fields would be "Gallery ID" and "Template name".
I'm of course using a custom page template. How can I retrieve the page attributes into this page template?
Thanks in advance!
Yes, what Stratboy said. Documentation here. This is the setup that should work for you:
<?php
$gallery_ID = get_post_meta($post->ID, 'Gallery ID', true);
$template_name = get_post_meta($post->ID, 'Template Name', true);
if ($gallery_ID && $template_name){
//echo '$gallery_ID: '.$gallery_ID.'; $template_name: '.$template_name.';';
echo do_shortcode('[nggallery id="'.$gallery_ID.'" template="'.$template_name.'"]');
}
?>
So, first:
have you already implemented the custom page template?
have you already implemented the custom fields?
Anyway, generally, in a template you get your custom fields values using the get_post_meta function in the Loop, like this:
//the last param tell if you want the value returned as a string (true) or an array (false)
get_post_meta($post->ID,'field name',true);
You can echo the returned value or use it for other tasks.
Let me know.

Resources