I found some custom fields turorial for WP and Woocommerce. So I played a bit with that. It all works ok, but I tried to customize part that saves values during checkbox checking, and unfortunately I'm not able to finish that. This is what I'm trying, piece of code in functions.php:
// Custom field for price labels
// 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;
// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_primer',
'wrapper_class' => '',
'label' => __('Primer', 'woocommerce' ),
'description' => __( 'Check me!', 'woocommerce' )
)
);
}
function woo_add_custom_general_fields_save( $post_id ){
// Checkbox_1
$woocommerce_checkbox_primer = isset( $_POST['_primer'] ) ? 'yes' : '';
update_post_meta( $post_id, '_primer', $woocommerce_checkbox_primer );
}
...and then this peace of code in single-product.php
<span><?php echo get_post_meta( get_the_ID(), '_primer', true ); ?></span>
So when this is implemented I have one big "yes" on single product page, but what I want to get is image instead of that "yes", so if checkbox is checked to show image on single product page, and to save that selection in database. I hope I was clear enough, and thanks in advance.
I'm not 100% sure that I understand but it sounded like you wanted to show an image conditionally if the checkbox was true in the backend
if( 'yes' == get_post_meta( get_the_ID(), '_primer', true ) ) { ?>
<img src="your-image.png"/>
<?php }
Related
I have a problem with the update_post_meta function.
I have a user submitted value, which I pass via $_POST and then saving to post meta.
All is working fine, but when the value is '0' the post meta is not updated.
This is My code:
// Add custom checkout field: woocommerce_review_order_before_submit
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field_ritiro_sede' );
function my_custom_checkout_field_ritiro_sede() {
echo '<div class="cw_custom_class"><h3>'.__('Ritiro presso sede CER S.r.l.  ').'</h3>';
echo '<div id="my_custom_checkout_field">';
woocommerce_form_field( 'ritiro_sede', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('SI'),
), WC()->checkout->get_value( 'ritiro_sede' ) );
echo '</div>';
}
// Save the custom checkout field in the order meta, when checkbox has been checked
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta_ritiro_sede', 10, 1 );
function custom_checkout_field_update_order_meta_ritiro_sede( $order_id ) {
if ( ! empty( $_POST['ritiro_sede'] ) )
update_post_meta( $order_id, 'ritiro_sede', $_POST['ritiro_sede'] );
if ( isset( $_POST['ritiro_sede'] ) )
update_post_meta( $order_id, 'ritiro_sede', $_POST['0'] );
}
Does anyone have any idea what might be wrong?
Since WooCommerce 3, here below is the best way to save your custom checkout checkbox field value as order meta data (including when the checkbox is unchecked):
// Save the custom checkout checkbox field as the order meta
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 10, 2 );
function custom_checkout_field_update_order_meta( $order, $data ) {
$value = isset($_POST['ritiro_sede']) ? '1' : '0'; // Set the correct values
$order->update_meta_data( 'ritiro_sede', $value );
}
Now as user meta data is used by WC_Checkout get_value() method in your first function on:
WC()->checkout->get_value( 'ritiro_sede' )
So if you want the submitted value to be displayed on checkout page for the next purchase, you will need to save that custom checkout field also as user meta data using instead the following:
// Save the custom checkout checkbox field as the order meta and user meta
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 10, 2 );
function custom_checkout_field_update_order_meta( $order, $data ) {
$value = isset($_POST['ritiro_sede']) ? '1' : '0'; // Set the correct values
// Save as custom order meta data
$order->update_meta_data( 'ritiro_sede', $value );
// Save as custom user meta data
if ( get_current_user_id() > 0 ) {
update_user_meta( get_current_user_id(), 'ritiro_sede', $value );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I'm helping my mother create her website for her store i Denmark.
It has gone okay, but now i'm stuck with a problem.
I need to be able to make a custom field in the order complete email, that displays a track & trace number, i've inserted on the admin order page. Everything i've done up until now, haven't worked, so therefore i seek your help.
I've added a custom field already called Track & Trace Pakkenr. (see screenshot 1)
But the problem is getting this in the order complete email, under shipping (forsendelse in danish, see screenshot 2)
I am also a complete and utter beginner in coding so if any of you can give any help or advice, please make it almost foolproof to follow.
Here are the screenshot 1 and screenshot 2.
To get this order custom field value displayed in order totals table on email notifications, use the following:
add_filter( 'woocommerce_get_order_item_totals', 'insert_custom_line_order_item_totals', 10, 3 );
function insert_custom_line_order_item_totals( $total_rows, $order, $tax_display ){
// Only on emails notifications
if( is_wc_endpoint_url() ) return $total_rows; // Exit
$tracking_label = 'Track & Trace Pakkenr.'; // The tracking label name
$tracking_value = $order->get_meta( $tracking_label ); // Get the tracking value (custom field).
if( empty($tracking_value) ) return $total_rows; // Exit
$new_total_rows = array(); // Initializing
// Loop through total rows
foreach( $total_rows as $key => $value ){
if( 'payment_method' == $key && ! empty($tracking_value) ) {
$new_total_rows['tracking_parcel'] = array(
'label' => $tracking_label,
'value' => $tracking_value,
);
}
$new_total_rows[$key] = $total_rows[$key];
}
return sizeof($new_total_rows) > 0 ? $new_total_rows : $total_rows;
}
Code goes in function.php file of your active child theme (or active theme). tested and works.
You will get something like:
From this:
You can add custom fields to the order email, you need to use the field key where the code has meta_key.
/**
* Add a custom field (in an order) to the emails
*/
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['meta_key'] = array(
'label' => __( 'Label' ),
'value' => get_post_meta( $order->id, 'meta_key', true ),
);
return $fields;
}
Ref: https://docs.woocommerce.com/document/add-a-custom-field-in-an-order-to-the-emails/
Below is a simple yet functional example of adding custom meta fields to order email
add_filter( 'woocommerce_email_order_meta_fields', 'woocommerce_email_order_meta_fields_func', 10, 3 );
function woocommerce_email_order_meta_fields_func( $fields, $sent_to_admin, $order ) {
$fields['Track_Field'] = array(
'label' => __( 'Track', 'woocommerce' ),
'value' => wptexturize( get_post_meta( $order->id, 'Track_Field', true ) )
);
//... more meta fields goes here
return $fields;
}
In email after order email table:
add_action( 'woocommerce_email_after_order_table', 'woocommerce_email_after_order_table_func' );
function woocommerce_email_after_order_table_func( $order ) {
?>
<h3>Track</h3>
<table>
<tr>
<td>Track Fields </td>
<td><?php echo wptexturize( get_post_meta( $order->id, 'Track_Field', true ) ); ?></td>
</tr>
<!--additional custom meta and HTML code goes here-->
</table>
<?php
}
For more help see this link : Click here
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';
Thanks to all developers at StackOverflow.
I want to add more fields in Linked Product Section of Woocommerce. The fields should be similar to Upsell/Crosssell.
My code so far:-
add_action( 'woocommerce_product_options_linked_product_data', 'woocom_general_product_data_custom_field' );
woocommerce_wp_text_input(
array(
'id' => '_upsizing_products',
'label' => __( 'Upsizing Products', 'woocommerce' ),
'placeholder' => 'Upsizing Products',
'desc_tip' => 'true',
'description' => __( 'Select Products Here', 'woocommerce' )
)
);
In the above code i need the combobox, For example when you type 3 characters in input box, it will show a list of matched products which can be selected. Similar to Upsell / Cross Sell.
Please anyone help me implement this custom field. Thanks in Advance.
Edit: Anyone?
There are several things missing in your code above.
The hook you have used on the first line, do not exist. The right hook is called woocommerce_product_options_related
The code where you've designed your custom field, is not inside any function.
You are creating a standard text-field. If you want a "Select Product"-dropdown, you should use another approach.
This should be inside that function that you're using in the hook.
You are missing a hook and a function the actually save the data from your custom field
1. Finding the right hook/action
To find the right hook to use, just search for "woocommerce_product_options_" inside the WoocCommerce plugin, and about 6 PHP-files should appear. One of those files is called "html-product-data-linked-products.php". This file contains all the existing options in that specific WooCommerce section. It also contains the hook used to display those options.
Open the file and check it out. The hook is at the bottom of the page
Full Path:
/wp-content/plugins/woocommerce/includes/admin/metaboxes/views/
2. Creating the dropdown
To create a select-dropdown including a product search, you need a much different code than the one above.
The spare some time, you can just copy-paste one of the existing options, in the file mentioned above, and then modify it, to your needs.
All this should be placed inside a function called: woocom_linked_products_data_custom_field().
2.1. Modify the ID/name
The first things you need to modify in the code, is of course the unique ID/name of the field.
This is placed in the label-tag (for) and the select-tag (id and name).
In your example the ID/name should be upsizing_products and the label-text Upsizing Products:
<label for="upsizing_products"><?php _e( 'Upsizing Product', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="upsizing_products" name="upsizing_products[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
Note: Don't forget to put an [] and the end of the name-tag, or your data will not be stored.
2.2. Show already chosen products
The next thing, is to show and highligt already chosen products in the WooCommerce section and the dropdown it self.
To do this replace the $product_ids-variable and the entire line with:
$product_ids = get_post_meta( $post->ID, '_upsizing_products_ids', true );
With this instead, the product id's is retrieved from your custom field in the database instead of one of the existing options (cross_sell_ids for example).
Note: The _upsizing_products_ids is the meta-key name in the database. The meta-value related to this key, contains all your field data. This is used to store and retrieve the custom field.
2.3. Display the field in the WooCommerce section
Lastly, the function should be properly hooked, so it can be displayed in the Linked Products section:
add_action( 'woocommerce_product_options_related', 'woocom_linked_products_data_custom_field' );
3. Save and store the data
Now your custom field is displayed inside the right section. The next thing is to save and store the data in the database.
Inside a new function, use $_POST to retrieve the data from the field, and update_post_meta to store the data in the database containing the post-ID, the unique field-id/name (meta-key) and the data it self (meta-value).
function woocom_linked_products_data_custom_field_save( $post_id ){
$product_field_type = $_POST['upsizing_products'];
update_post_meta( $post_id, '_upsizing_products_ids', $product_field_type );
}
add_action( 'woocommerce_process_product_meta', 'woocom_linked_products_data_custom_field_save' );
Here's the full code. Put it inside your themes functions.php or a plugin file:
// Display the custom fields in the "Linked Products" section
add_action( 'woocommerce_product_options_related', 'woocom_linked_products_data_custom_field' );
// Save to custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_linked_products_data_custom_field_save' );
// Function to generate the custom fields
function woocom_linked_products_data_custom_field() {
global $woocommerce, $post;
?>
<p class="form-field">
<label for="upsizing_products"><?php _e( 'Upsizing Product', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="upsizing_products" name="upsizing_products[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
<?php
$product_ids = get_post_meta( $post->ID, '_upsizing_products_ids', true );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select> <?php echo wc_help_tip( __( 'Select Products Here.', 'woocommerce' ) ); ?>
</p>
<?php
}
// Function the save the custom fields
function woocom_linked_products_data_custom_field_save( $post_id ){
$product_field_type = $_POST['upsizing_products'];
update_post_meta( $post_id, '_upsizing_products_ids', $product_field_type );
}
To display the stored data use _upsizing_products_ids:
echo get_post_meta( $post->ID, 'my-field-slug', true );
Check out this guide Mastering WooCommerce Products
Custom Fields for more information about Custom Fields for WooCommerce.
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