I am working on a WordPress eCommerce website, with WooCommerce being the chosen Shopping Platform.
I have created a Custom Field, within the WooCommerce Product Dashboard by inserting the following code into the functions.php file:
function product_custom_fields_add(){
echo '<div class="product_custom_field">';
// Minimum Required Custom Letters
woocommerce_wp_text_input(
array(
'id' => '_minimum_engrave_text_option',
'name' => '_minimum_engrave_text_option',
'desc' => __('set custom minimum Lettering text field', 'woocommerce'),
'label' => __('Minimum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
add_action('woocommerce_product_options_advanced', 'product_custom_fields_add');
In order to save the values, I have entered the following code into the functions.php file:
// Save Minimum Required Custom Letters
function woocommerce_product_custom_fields_save1($post_id){
if ( ! empty( $_POST['_minimum_engrave_text_option'] ) )
update_post_meta($post_id, '_minimum_engrave_text_option', esc_attr( $_POST['_minimum_engrave_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save1' );
The above code works when a value is entered into the Custom Field. My problem is that I am unable to successfully save a Blank Value. Whenever I save a Product, the Custom Field either auto populates the Field with '1' or saves a previously entered number.
I tried applying the answer, from a similar question, but could not quote get it to work.
Does anyone know where I am going wrong here?
Try to replace empty() by isset() as a condition in the if statement of your hooked function:
// Save Minimum Required Custom Letters
function woocommerce_product_custom_fields_save1($post_id){
if ( isset( $_POST['_minimum_engrave_text_option'] ) )
update_post_meta($post_id, '_minimum_engrave_text_option', esc_attr( $_POST['_minimum_engrave_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save1' );
Now this will works
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
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.
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'm creating a plugin for wordpress and woocommerce. In my plugin, I've inserted an input box above the checkout form using code using 'woocommerce_before_checkout_form'. On completion of the order, I'd like to be able to add the value from that input to the order's meta data. to that end, I created this code in the functions.php file of my plugin:
add_action( 'woocommerce_checkout_update_order_meta', 'add_input_meta', 1, 2 );
function add_input_meta( $order_id, $posted ) {
$inputsData = $_POST['InputBox'];
update_post_meta( $order_id, 'my_key', $inputsData);
}
The problem is, it returns NULL every time. I created the code below to see what the value of $_POST and it came up with 'array(0) { }'
function debugthing( $content ) {
$content .=var_dump($_POST);
return $content;
die();
}
add_filter( 'the_content', 'debugthing' );
I've exhausted every idea I could ahve about what is causing this. can anyone help?
$_POST, even php://input return an empty array or absolutely nothing.
woocommerce_before_checkout_form is not the right hook to add input fields. For this hook is outside the form. This explains why you are getting null on $_POST
use any of the hook inside the <form in form-checkout.php#L35
call woocommerce_form_field to add fields...
next is you need to hook inside process_checkout() function.
a. woocommerce_after_checkout_validation - for input validations...
b. woocommerce_checkout_order_processed - order created, do your meta data addition...
// add form fields
add_action( 'woocommerce_checkout_before_customer_details', 'woocommerce_checkout_before_customer_details' );
function woocommerce_checkout_before_customer_details() {
$args = array(
'type' => 'text',
'label' => 'My Custom field',
'description' => 'This is custom field',
'placeholder' => '',
'required' => true,
);
woocommerce_form_field( 'InputBox' , $args ); // you can call woocommerce_form_field as many as you want...
}
// validate your form field(s)
add_action( 'woocommerce_after_checkout_validation', 'woocommerce_after_checkout_validation' );
function woocommerce_after_checkout_validation() {
// $_POST['InputBox'] will be visible here...
// do your validations here... forget this hook if you don't need to validate...
// wc_add_notice( __( 'Invalid message!', 'woocommerce' ), 'error' );
// call wc_add_notice if you want to invalidate the form.
}
add_action( 'woocommerce_checkout_order_processed', 'woocommerce_checkout_order_processed' );
function woocommerce_checkout_order_processed( $order_id ) {
// we now have $order_id, you can now add your meta data....
}
change your hook priority like this.
add_action( 'woocommerce_checkout_update_order_meta', 'add_input_meta', 99, 2 );
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