I have added custom fields to my woocommerce products and have been able to enter and fill the custom field data on products. I am trying now to use the data from that custom field in a button I am adding on the woocommerce single product page.
The custom field is a URL for the product sample. I am then trying to add a "View Product Sample" button on the woocommerce single product page that navigates to the url entered int he custom field. Here is the code I have:
// Display Fields
add_action( ‘woocommerce_product_options_general_product_data’, ‘woo_add_custom_general_fields’ );
// Save Fields
add_action( ‘woocommerce_process_product_meta’, ‘woo_add_custom_general_fields_save’ );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
// Text Field
woocommerce_wp_text_input(
array(
‘id’ => ‘product_sample’,
‘label’ => __( ‘Sample Product Link’, ‘woocommerce’ ),
‘placeholder’ => ‘http://’,
‘desc_tip’ => ‘true’,
‘description’ => __( ‘Enter the sample product link here.’, ‘woocommerce’ )
)
);
}
function woo_add_custom_general_fields_save( $post_id ){
// Textfield
$woocommerce_text_field = $_POST[‘product_sample’];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, ‘product_sample’, esc_html( $woocommerce_text_field ) );
}
add_action(‘woocommerce_after_add_to_cart_button’,’cmk_additional_button’);
function cmk_additional_button() {
echo '<a href="CONTENT OF CUSTOM FIELD - URL" target="_blank" button
type="submit" class="button sample">View Product Sample</a>';
}
I need help determining how to get my custom field meta data into the link for the button that is echoed at the end of the code.
Thanks for any help anyone can give.
You Can use ACF plugin for it and call the shortcode on single product page
Reference Url:http://blog.adlivetech.com/get-custom-fields-woocomerce/
You need to use get_post_meta() function this way:
add_action( 'woocommerce_after_add_to_cart_button', 'cmk_additional_button' );
function cmk_additional_button() {
global $post;
$url = get_post_meta( $post->ID, 'product_sample', true );
echo 'View Product Sample';
}
Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.
This should work.
In the code of your question you are using ’ and you should replace them by ' instead, to avoid php errors.
Related
I would like to add a text field in the back-end Woocommerce product page and displaying/echo the text on the front-end below the Product title.
Now I have the 'custom field box' to write a text in the back-end (see screenshot), but I don't know how I can showing the text on the front-end. Can someone help me with this code?
I followed this page, but it is only for archive pages...
Add a custom field value below product title in WooCommerce archives pages
Thank you in advance!
jerry
Functions.php
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
echo '<div class="product_custom_field">';
// Custom Product Text Field
woocommerce_wp_text_input(
array(
'id' => '_custom_product_text_field',
'placeholder' => 'Custom Product Text Field',
'label' => __('Custom Product Text Field', 'woocommerce'),
'desc_tip' => 'true'
)
);
}
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
if (!empty($woocommerce_custom_product_text_field))
update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));
}
Your solution is the right hook, when you use add_action() you need to choose the right hook to insert your code in the right location.
Probebly the location you want is "woocommerce_before_add_to_cart_form";
add_action('woocommerce_before_add_to_cart_form', 'woocommerce_product_custom_fields');
I'm not sure about the exact location but you can just change "woocommerce_before_add_to_cart_form" and place the right hook you want.
This is a great article that shows the location and the required hook for every location. Let me know what you get!
Add the following to display that product custom field in single product pages below product title:
add_action( 'woocommerce_single_product_summary', 'custom_field_display_below_title', 7 );
Code goes in functions.php file of your active child theme (or active theme). It should work.
Then it will call the function, that you are already using, to display the custom field on product archive pages:
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );
function custom_field_display_below_title(){
global $product;
// Get the custom field value
$custom_field = get_post_meta( $product->get_id(), '_custom_product_text_field', true );
// Display
if( ! empty($custom_field) ){
echo '<p class="my-custom-field">'.$custom_field.'</p>';
}
}
Related: WooCommerce action hooks and overriding templates
Trying to add text (best inside a div) next to the title/label for a variation (i.e. SIZE) without editing the woocommerce page template.
Pseudocode:
function my_text_after_variation (){
if attribute pa_size exists {
echo label for pa_size;
echo <div> MY TEXT </div>;
}else{
return;
}};
Every help is highly welcome.
To display a custom text just after a specific attribute label name, you will use this a custom function hooked in woocommerce_attribute_label dedicated filter hook, this way:
add_filter( 'woocommerce_attribute_label', 'custom_attribute_label', 10, 3 );
function custom_attribute_label( $label, $name, $product ) {
$taxonomy = 'pa_'.$name;
if( $taxonomy == 'pa_size' )
$label .= '<div class="custom-label">' . __('MY TEXT', 'woocommerce') . '</div>';
return $label;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.
If you can not edit the woocommerce page template, try to use jQuery.
For example:
$('.pa_size label').after('<span class="some-text">some text</text>');
If you need to use variable text, you can create object with this variable using 'wp_localize_script' and get it from this object:
function.php
// Localize the script with your data
$data_array = array(
'some_text' => __( 'Some text to insert', 'my-domain' ),
);
wp_localize_script( 'some_handle', 'object_name', $data_array );
jQuery:
$('.pa_size label').after(object_name.some_text);
In WooCommerce I would like to add custom text to my products display, that will be grabbed from a custom field in product's edit page.
This is how it looks now:
You can see the products with their title below:
I would like to add a text below every product, marked with blue pen:
I have managed to find some custom php code to add custom field in product's page like this:
.
This is my code:
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
echo '<div class="product_custom_field">';
// Custom Product Text Field
woocommerce_wp_text_input(
array(
'id' => '_custom_product_text_field',
'placeholder' => 'Custom Product Text Field',
'label' => __('Custom Product Text Field', 'woocommerce'),
'desc_tip' => 'true'
)
);
}
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
if (!empty($woocommerce_custom_product_text_field))
update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));
}
But I don't know how to connect the custom field to the product's display,
so the custom field's text will be shown below the product's title.
How to display the custom field below product title
Updated:
Try this custom hooked function, that will display your custom field value below product title:
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );
function custom_field_display_below_title(){
global $product;
// Get the custom field value
$custom_field = get_post_meta( $product->get_id(), '_custom_product_text_field', true );
// Display
if( ! empty($custom_field) ){
echo '<p class="my-custom-field">'.$custom_field.'</p>';
}
}
Code goes in function.php file of your active child theme (or active theme).
It should work.
For anyone trying to implement this, the closing div is missing from function woocommerce_product_custom_fields(). Add echo ''; before the closing curly bracket otherwise all of the other product data tabs in the product settings will be empty.
If you are trying to use this code the closing div needs to be:
echo '</div>';
otherwise echo '<div class="product_custom_field">'; remains open.
I have an attribute in WooCommerce: demo_url. Every product has it's own URL inserted there.
I have also added a button near ADD TO CART so the user can easily view the product on his head (glasses shop). Clicking this button will redirect the user to outside application. Every glasses will have a different link for clicking.
I have tried something like that:
echo 'Text';
But it is not working at all. I've tried it with different global variables (like $currentday) but to no available.
I desperately need this to work. Could You please help?
Thanks
Instead an using a product attribute you should use a custom field (a custom field in the product pages general settings tabs).
Here is that code:
// Create and display the custom field in product general setting tab
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_field_general_product_fields' );
function add_custom_field_general_product_fields(){
global $post;
$value = get_post_meta( $post->ID, '_demo_url', true );
if(empty($value))
$value = '';
echo '<div class="options_group">';
woocommerce_wp_text_input( array(
'id' => 'demo_url',
'label' => __( 'Demo Url', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the Demo Url.', 'woocommerce' ),
'value' => $value
) );
echo '</div>';
}
// Save the custom field
add_action( 'woocommerce_process_product_meta', 'save_custom_field_general_product_fields' );
function save_custom_field_general_product_fields( $post_id){
// Text Field
$demo_url = $_POST['demo_url'];
if( !empty( $demo_url ) )
update_post_meta( $post_id, '_demo_url', esc_attr( $demo_url ) );
}
Code goes in any php file of your active child theme (or theme) or also in any plugin php file.
This code is tested and works... Then you will get this:
Now you can add in your code:
// If you dont have the product id use this:
$global $product;
$product_id = $product->get_id();
// Getting your custom product demo URL
$demo_url = get_post_meta( $product_id, '_demo_url', true );
// Add it to your button:
echo 'Text';
I've been reading WP forums and trying different plugins for over a week now with no luck, so I decided to give it a try here.
I'm creating a WP website with a premium theme, that supports a woocommerce. What I need to do is the following:
Create a subtitle area (which would be named REG. NO:), so I could not only write Title of the product, but subtitle also. So when you would open a single product page, it would be like:
This_is_my_product_title
REG.NO: this_is_my_reg_no
another issue is, I would need a REG.NO: (subtitle) to be an external hyperlink to a different website.
Greatest thanks to anyone who could help me out.
If you want to go pure WooCommerce way, here's the gist.
1 - Add custom field ( this code goes in functions.php )
add_action( 'woocommerce_product_options_general_product_data', 'my_custom_field' );
function my_custom_field() {
woocommerce_wp_text_input(
array(
'id' => '_subtitle',
'label' => __( 'Subtitle', 'woocommerce' ),
'placeholder' => 'Subtitle....',
'description' => __( 'Enter the subtitle.', 'woocommerce' )
)
);
}
The field will appear as shown in this screen grab : http://i.imgur.com/fGC86DA.jpg
2 - Save the field's data when product is saved. ( this code goes in functions.php )
add_action( 'woocommerce_process_product_meta', 'my_custom_field_save' );
function my_custom_field_save( $post_id ){
$subtitle = $_POST['_subtitle'];
if( !empty( $subtitle ) )
update_post_meta( $post_id, '_subtitle', esc_attr( $subtitle ) );
}
3 - Edit single product template and display the field's value
<?php
global $post;
echo get_post_meta( $post->ID, '_subtitle', true );
?>
Ok so for everyone else who might have the same problem. Although both of options posted already are worth considering and will definitely save them as favorites because I'm sure I will need this in the future, this is the solution that worked best for me.
Although I'm trying to use as few plugins as possible, I ultimately decided to go with KIA SUBTITLE plugin. Then, you have to write this code in your functions.php:
function kia_add_subtitle_link_to_woocommerce(){
if( function_exists( 'the_subtitle' ) ){
$link = the_subtitle( '<h2 class="subtitle">', '</h2>', false );
printf( $link, get_permalink(), sprintf( __( 'Permalink to %s', 'your-text-domain' ), get_the_title() ) );
}
}
add_action( 'some_custom_hook', 'kia_add_subtitle_link_to_woocommerce' );
I used the following hook:
add_action( 'woocommerce_single_product_summary', 'kia_add_subtitle_link_to_woocommerce' );
You can use Advanced custom field plugin to create extra field in add product for REG NO
and you simple get field value on single page using the_field('name_u_give')
or you can also add post meta for post type product