How to get a term custom field value in WooCommerce? - php

I've created a 'color' custom field for pa_color taxonomy terms, by native ways as follows:
This is my code:
add_action( 'pa_color_add_form_fields', 'themename_pa_color_add_term_fields' );
function themename_pa_color_add_term_fields( $taxonomy ) {
?>
<div class="form-field">
<label for="themename_pa_color">PA Color</label>
<input type="color" name="themename_pa_color" id="themename_pa_color" class="wpColorChoose" />
<p>Field description may go here.</p>
</div>
<?php
}
add_action( 'pa_color_edit_form_fields', 'themename_pa_color_edit_term_fields', 10, 2 );
function themename_pa_color_edit_term_fields( $term, $taxonomy ) {
$value = get_term_meta( $term->term_id, 'themename_pa_color', true );
echo '<tr class="form-field">
<th>
<label for="themename_pa_color">PA Color</label>
</th>
<td>
<input name="themename_pa_color" id="themename_pa_color" type="color" class="wpColorChoose" value="' . esc_attr( $value ) .'" />
<p class="description">Field description may go here.</p>
</td>
</tr>';
}
add_action( 'created_pa_color', 'themename_pa_color_save_term_fields' );
add_action( 'edited_pa_color', 'themename_pa_color_save_term_fields' );
function themename_pa_color_save_term_fields( $term_id ) {
update_term_meta(
$term_id,
'themename_pa_color',
sanitize_text_field( $_POST[ 'themename_pa_color' ] )
);
}
How can I get their values to the place I need?
When trying to use $term->themename_pa_color, it doesn't work.
It works with name and description default fields, like $term->name or $term->description, but not with my field.
That's how I created it, it correctly saves values.

Your code is adding a custom field to the terms from "pa_color" taxonomy… So this is about term meta data which is never included in the WP_Term Object, and so not accessible as a property from that WP_Term Object.
So the answer is in your code. You need to use the WordPress function get_term_meta() like in your own code (in your 2nd function):
$value = get_term_meta( $term->term_id, 'themename_pa_color', true );
echo $value; // Display value
(where $term->term_id is the term Id and themename_pa_color the meta key)…

Related

WordPress/WooCommerce - What is the hook for custom category bulk edit?

I have added bulk action in the WooCommerce product category edit. I just want to know what the hook is when I click on Apply to do some actions on selected categories. (Specifically, I want to update all selected categories by updating the wp_termmeta table and set the example value to 1 or 0) The only thing that I want is the name of the hook when clicking the Apply button in /wp-admin/edit-tags.php?taxonomy=product_cat&post_type=product.
Just copy & paste into your functions.php. Enjoy
// Note: change all occurrences of "custom_field" with the key of your custom field add_action( 'woocommerce_product_bulk_edit_start', 'bbloomer_custom_field_bulk_edit_input' ); function bbloomer_custom_field_bulk_edit_input() { ?> <div class="inline-edit-group"> <label class="alignleft"> <span class="title"><?php _e( 'Custom Field', 'woocommerce' ); ?></span> <span class="input-text-wrap"> <input type="text" name="custom_field" class="text" value=""> </span> </label> </div> <?php } add_action( 'woocommerce_product_bulk_edit_save', 'bbloomer_custom_field_bulk_edit_save' ); function bbloomer_custom_field_bulk_edit_save( $product ) { $post_id = $product->get_id(); if ( isset( $_REQUEST['custom_field'] ) ) { $custom_field = $_REQUEST['custom_field']; update_post_meta( $post_id, 'custom_field', wc_clean( $custom_field ) ); } }

Additional image field in WooCommerce category

With help i've made custom field on WooCommerce's category, but i want to make it media field (to pick cover image and upload directly from form).
I'm using "How do I add custom fields to the categories in Woocommerce?"
// Add term page
function custom_product_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[category_cover]"><?php _e( 'Category Cover', 'category-cover' ); ?></label>
<input type="text" name="term_meta[category_cover]" id="term_meta[category_cover]" value="">
<p class="description"><?php _e( 'Enter a value for this field','category-cover' ); ?></p>
</div>
<?php
}
add_action( 'product_cat_add_form_fields', 'custom_product_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function custom_product_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[category_cover]"><?php _e( 'Category Cover', 'category-cover' ); ?></label></th>
<td>
<input type="text" name="term_meta[category_cover]" id="term_meta[category_cover]" value="<?php echo esc_attr( $term_meta['category_cover'] ) ? esc_attr( $term_meta['category_cover'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter an URL for category cover','category-cover' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'custom_product_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
I dont know what should I tell more, but i will be really thankful for help me! :)

How to get all WooCommerce product attributes taxonomies slugs and names

In WooCommerce I am trying to add a custom field to all of product attributes as a plugin…
For the moment in the code below, I am able to add a custom field to one taxonomy:
function pippin_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'pippin' ); ?></label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
<p class="description"><?php _e( 'Enter a value for this field','pippin' ); ?></p>
</div>
<?php
}
add_action( 'pa_flavor_add_form_fields', 'pippin_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function pippin_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'pippin' ); ?></label></th>
<td>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter a value for this field','pippin' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'pa_flavor_edit_form_fields', 'pippin_taxonomy_edit_meta_field', 10, 2 );
How to get all WooCommerce product attributes taxonomies slugs (and names)?
This way I could make those custom fields dynamically for all existing product attributes.
You can get product attribute taxonomy slugs (and taxonomy names) by using:
Before WooCommerce version 3.6 with wc_get_attribute_taxonomies() dedicated function:
// Get an array of product attribute taxonomies slugs
$attributes_tax_slugs = array_keys( wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' ) );
// Get an array of product attribute taxonomies names (starting with "pa_")
$attributes_tax_names = array_filter( array_map( 'wc_attribute_taxonomy_name', $attribute_taxonomies ));
Since WooCommerce version 3.6+ with wc_get_attribute_taxonomy_labels() dedicated function:
// Get an array of product attribute taxonomies slugs
$attributes_tax_slugs = array_keys( wc_get_attribute_taxonomy_labels() );
// Get an array of product attribute taxonomies names (starting with "pa_")
$attributes_tax_names = array_filter( array_map( 'wc_attribute_taxonomy_name', $attribute_taxonomies ));
Official documentation:
All WooCommerce related product attribute functions: wc-attribute-functions.php core file
WordPress: wp_list_pluck() function
PHP functions: array_keys(), array_filter() and array_map()

Add a custom multi-select field to admin product options settings in Woocommerce

I have followed this answer How to add more custom field in Linked Product of Woocommerce to add a custom select field to my Linked Product screen in Woocommerce. This new field is meta key is subscription_toggle_product.
It's working fine, but once you have selected a product, you can't delete it (there is no "Empty" option or cross symbol). I haven't got a clue how I can allow the selection to be deselected. I've tried adding an <option> with an empty value, but it hasn't worked.
Here is my code:
// 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;
$product = wc_get_product( $post->ID );
?>
<p class="form-field">
<label for="subscription_toggle_product"><?php _e( 'Subscription Toggle Product', 'woocommerce' ); ?></label>
<select class="wc-product-search" style="width: 50%;" id="subscription_toggle_product" name="subscription_toggle_product" 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_id = get_post_meta( $post->ID, '_subscription_toggle_product_id', true );
if ( $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>
</p>
<?php
}
// Function the save the custom fields
function woocom_linked_products_data_custom_field_save( $post_id ){
if (isset($_POST['subscription_toggle_product'])) {
$product_field_type = $_POST['subscription_toggle_product'];
update_post_meta( $post_id, '_subscription_toggle_product_id', $product_field_type );
}
}
This kind of select field only works with a defined multiple attribute and work with an array of values. so you can't use it for a simple ID. If you add to your select field multiple="multiple" attribute it will work.
Also since Woocommerce 3 things have changed:
- There are better hooks to save the data.
- You can now use CRUD Objects and related methods.
The following code will work for multiple product IDs (an array of products IDs):
// Display a custom select field in "Linked Products" section
add_action( 'woocommerce_product_options_related', 'display_linked_products_data_custom_field' );
function display_linked_products_data_custom_field() {
global $product_object, $post;
?>
<p class="form-field">
<label for="subscription_toggle_products"><?php _e( 'Subscription Toggle Products', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="subscription_toggle_ids" name="_subscription_toggle_ids[]" 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 = $product_object->get_meta( '_subscription_toggle_ids' );
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>
</p>
<?php
}
// Save the values to the product
add_action( 'woocommerce_admin_process_product_object', 'save_linked_products_data_custom_field_value', 10, 1 );
function save_linked_products_data_custom_field_value( $product ){
$data = isset( $_POST['_subscription_toggle_ids'] ) ? array_map( 'intval', (array) $_POST['_subscription_toggle_ids'] ) : array();
$product->update_meta_data( '_subscription_toggle_ids', $data );
}
Code goes in function.php file of your active child theme (active theme). Tested and works.

Show Custom Meta Field Linked to Custom Taxonomy on Front End

Hi I have a site with a Custom Post Type of 'Researcher', within that I have a taxonomy called 'Supervisors', and within that a meta field named 'Job Title'. I am trying to figure out how to display this 'Job Title' in the front end.
The code for the meta field (from functions.php) is as follows:
// Edit Supervisor Taxonomy
// Add term page
function jobtitle_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[jobtitle]"><?php _e( 'Job Title', 'jobtitle' ); ?></label>
<input type="text" name="term_meta[jobtitle]" id="term_meta[jobtitle]" value="">
<p class="description"><?php _e( 'Enter a value for this field','jobtitle' ); ?></p>
</div>
<?php
}
add_action( 'supervisor_add_form_fields', 'jobtitle_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function jobtitle_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[jobtitle]"><?php _e( 'Job Title', 'jobtitle' ); ?></label></th>
<td>
<input type="text" name="term_meta[jobtitle]" id="term_meta[jobtitle]" value="<?php echo esc_attr( $term_meta['jobtitle'] ) ? esc_attr( $term_meta['jobtitle'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter a value for this field','jobtitle' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'supervisor_edit_form_fields', 'jobtitle_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_supervisor', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_supervisor', 'save_taxonomy_custom_meta', 10, 2 );
// End Edit Supervisor Taxonomy
Image showing the backend — http://d.pr/i/TS13
You will need:
The post ID of the researcher. On the single-researcher.php page (if your post type is named researcher) you should be able to use $post->ID to obtain the post ID of the researcher.
The name of the taxonomy. This can be obtained by reading the URL of the screenshot you posted previously. It should say: ...edit-tags.php?taxonomy=supervisors&.... I'll assume the taxonomy is named supervisors.
If you're on the single-researcher.php page, this might work... Note: $supervisor_tags is an array of tags for that researcher. Each researcher could have 0, 1, or more supervisors, so you might want to loop through them:
global $post;
$supervisor_tags = get_the_terms($post->ID, 'supervisors');
echo "<h2>Supervisors:</h2>";
foreach ($supervisor_tags as $tag){
$term_meta = get_option('taxonomy_' . $tag->term_id);
echo "<p>Name: " . $tag->name . " - Job Title: " . $term_meta['jobtitle'] . "</p>";
}
You can echo "<pre>"; print_r($variable); echo "</pre>"; to see what the $variable array looks like, if you're having trouble.

Categories