Adding custom checkbox to WooCommerce product variation options - wordpress

I've added a custom field to variable products in the WC admin. Using the action:
action_woocommerce_variation_options_pricing
But it's displaying in the wrong location. I'd ideally like the checkbox to display in the options section above. But i can't find the action to move it there.
I've spent ages looking through the WC docs, but can't find the correct action. Does anyone know the correct way for me to move the checkbox to this location?

You can use the woocommerce_variation_options action hook, to add a custom checkbox to the WooCommerce product variation options.
(Copied from: views/html-variation-admin.php, line 188)
<?php do_action( 'woocommerce_variation_options', $loop, $variation_data, $variation ); ?>
So you get:
// Add checkbox
function action_woocommerce_variation_options( $loop, $variation_data, $variation ) {
$is_checked = get_post_meta( $variation->ID, '_mycheckbox', true );
if ( $is_checked == 'yes' ) {
$is_checked = 'checked';
} else {
$is_checked = '';
}
?>
<label class="tips" data-tip="<?php esc_attr_e( 'This is my data tip', 'woocommerce' ); ?>">
<?php esc_html_e( 'Checkbox:', 'woocommerce' ); ?>
<input type="checkbox" class="checkbox variable_checkbox" name="_mycheckbox[<?php echo esc_attr( $loop ); ?>]"<?php echo $is_checked; ?>/>
</label>
<?php
}
add_action( 'woocommerce_variation_options', 'action_woocommerce_variation_options', 10, 3);
// Save checkbox
function action_woocommerce_save_product_variation( $variation_id, $i ) {
if ( ! empty( $_POST['_mycheckbox'] ) && ! empty( $_POST['_mycheckbox'][$i] ) ) {
update_post_meta( $variation_id, '_mycheckbox', 'yes' );
} else {
update_post_meta( $variation_id, '_mycheckbox', 'no' );
}
}
add_action( 'woocommerce_save_product_variation', 'action_woocommerce_save_product_variation', 10, 2 );

Related

woocommerce how to get the value of custom upsell

I am trying to create a custom upsell n woocommerce field. This code allowed me to create the custom field in the backend. I can't get the products to show via the echo
It returns me the word Array.
What am I doing wrong?
The value should be displayed in a custom jquery sidebar
// 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="total_look"><?php _e( 'Total Look EleNelen', 'woocommerce' ); ?>.
</label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;"
id="total_look_ids" name="_total_look_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( '_total_look_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['_total_look_ids'] ) ? array_map( 'intval', (array)
$_POST['_total_look_ids'] ) : array();
$product->update_meta_data( '_total_look_ids', $data );
}

Display billing_company when performing customer search in “Add New Sales Order” page

May I know how to display $user->billing_company upon results return when performing customer search in Sales Order form? Ideally am thinking of using filter/action hooks instead of modifying the core file in class-wc-meta-box-order-data.php
So far am able to search for customer based on the $user->billing_company, the results will show up on the editing order page, but not on the adding new order page ($user->billing_company is not displayed in real time upon typing and clicking on the searched result)
Images:
https://i.stack.imgur.com/o4yfW.png
https://i.stack.imgur.com/pjfEM.png
Modified code:
esc_html__( '[%4$s] %1$s (#%2$s – %3$s)', 'woocommerce' )
$user->display_name,
absint( $user->ID ),
$user->user_email, $user->billing_company
Original code from class-wc-meta-box-order-data.php:
<?php
$user_string = '';
$user_id = '';
if ( $order->get_user_id() ) {
$user_id = absint( $order->get_user_id() );
$user = get_user_by( 'id', $user_id );
/* translators: 1: user display name 2: user ID 3: user email */
$user_string = sprintf(
esc_html__( '%1$s (#%2$s – %3$s)', 'woocommerce' ),
$user->display_name,
absint( $user->ID ),
$user->user_email
);
}
?>
<select class="wc-customer-search" id="customer_user" name="customer_user" data-placeholder="<?php esc_attr_e( 'Guest', 'woocommerce' ); ?>" data-allow_clear="true">
<option value="<?php echo esc_attr( $user_id ); ?>" selected="selected"><?php echo htmlspecialchars( $user_string ); ?></option>
</select>
<!--/email_off-->
</p>
<?php do_action( 'woocommerce_admin_order_data_after_order_details', $order ); ?>
Thank you in advance!
The following hooked function will allow you to display the billing company without modifying core files:
add_filter( 'gettext', 'change_admin_single_order_heading3', 10, 3 );
add_filter( 'ngettext', 'change_admin_single_order_heading3', 10, 3 );
function change_admin_single_order_heading3( $translated, $text, $domain ) {
global $pagenow, $theorder;
if ( is_admin() && $pagenow === 'post.php' && isset($_GET['post']) && get_post_type($_GET['post']) === 'shop_order' )
{
if( $text === '%1$s (#%2$s – %3$s)' && $domain === 'woocommerce' && $theorder->get_user_id() > 0 ){
// Get user meta billing company
if( $billing_company = get_user_meta( $theorder->get_user_id(), 'billing_company', true ) ) {
$translated = esc_html__( '['.$billing_company.'] %1$s (#%2$s – %3$s)', $domain );
}
}
}
return $translated;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
But you will not be able to display it when performing a search as the search choices are pulled with ajax…

Custom Meta Boxes in Wordpress are not updating - need help identifying the errors in this function

I created a small plugin to add a Color meta box to a custom post type. The box displays fine but I can't seem to get it to save the data. When I type something in the field and click update, the field returns blank. I researched this and followed many tutorials, and each offered a slightly different approach. The code below was the easiest for me to follow, so I would really appreciate any help with identifying the error(s) in it. This is my first question so sorry if I omitted anything relevant.
Here is how I added the box:
add_action( 'add_meta_boxes', 'addmeta' );
function addmeta() {
$post_types = array ('post', 'ev');
foreach ( $post_types as $post_type ) {
add_meta_box (
'color_box',
'Color',
'display_meta_box',
$post_type,
'side'
);
}
}
add_action ( 'add_meta_boxes', 'addmeta');
Function to display the meta box:
function display_meta_box() {
$value = get_post_meta( $post->ID, '_mykey', true);
wp_nonce_field( basename( __FILE__ ), 'my_nonce' );
?>
<label for="color_box"><strong>Color:</strong> </label>
<input type="text" name="my_text" id="my_text" />
<?php
}
And this is the save function:
function save_meta_box ( $post_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$wp_valid_nonce = false;
if( isset( $_POST[ 'my_nonce' ] ) ) {
if ( wp_verify_nonce( $_POST['my_nonce'], basename( __FILE__ ) ) ) {
$is_valid_nonce = true;
}
}
if ( $is_autosave || $is_revision || !$is_valid_nonce ) return;
if( array_key_exists( 'color_box', $_POST ) ) {
update_post_meta(
$post_id,
'_mykey',
sanitize_text_field( $_POST[ 'color_box' ] )
);
}
}
add_action( 'save_post', 'save_meta_box' );
Thank you!
you need to define ID. Check this code it will work definitively.
function display_meta_box( ) {
$value = get_post_meta( get_the_ID(), '_mykey', true);
wp_nonce_field( basename( __FILE__ ), 'my_nonce' );
?>
<label for="color_box"><strong>Color:</strong> </label>
<input type="text" name="color_box" value="<?php echo $value;?>" id="my_text" />
<?php
}
You are putting wrong name in input box in your function display_meta_box. Replace your function with below function.
function display_meta_box() {
global $post;
$value = get_post_meta( $post->ID, '_mykey', true);
wp_nonce_field( basename( __FILE__ ), 'my_nonce' );
?>
<label for="color_box"><strong>Color:</strong> </label>
<input type="text" name="color_box" value="<?php echo $value;?>" id="my_text" />
<?php
}
Hope this will help. Please let me know if any issue.

WPUF user profile select terms from taxonomy on wordpress using action hook

I have created a registration form on WPUF so that users can select their region from a taxonomy. The code is loaded on the form using an action hook.
The code loads the terms from the taxonomy in a dropdown and users can select the region.
But I am having an issue saving the selected terms into the custom field on the user profile. Find the code below:
function user_region_taxonomy( $form_id, $post_id, $form_settings, $tax, $user_id ) {
$taxterms = get_terms( 'user_region', array("hide_empty" => "0"));
if ( $post_id ) {
get_post_meta( $post_id, 'user_region_location', true );
}
?>
<div class="wpuf-label">
<label>User Region</label>
</div>
<div class="wpuf-fields">
<select name='user_region_location' id='user_region_location'>
<option value='' <?php if (!count( $names )) echo "selected";?>>Select Term</option>
<?php foreach ( $taxterms as $term ) {
echo '<option name="user_region_location" value="' . $term->slug . '" selected>' . $term->name . '</option>',"\n";
} ?>
</select>
</div>
<?php
}
add_action( 'user_region_hook', 'user_region_taxonomy', 10, 3 );
function update_user_region_hook( $post_id ) {
if ( isset( $_POST['user_region_location'] ) ) {
update_post_meta( $post_id, 'user_region', $_POST['user_region_location'] );
}
}
add_action( 'wpuf_after_register', 'update_user_region_hook' );
add_action( 'wpuf_update_profile', 'update_user_region_hook' );

Adding metabox to custom post type category

I can add extra form elements to category editor using the following hook:
add_action ( 'edit_category_form_fields', 'my_custom_function');
What I wonder is, is there a way to add extra form elements to a "custom post type category"
I checked the hook reference, I found hooks for terms (e.g. edit_term_taxonomy) but not categories.
Thanks
This will add a field called 'TERM META TEXT' to your categories. I did take out the nonce but I really think it should go back in. Also, it's just better to have some sanitization vs. none. This example includes javascript and CSS hooks which you may or may not need but you can quickly see how all the parts go together.
// REGISTER TERM META
add_action( 'init', '___register_term_meta_text' );
function ___register_term_meta_text() {
register_meta( 'term', '__term_meta_text', '___sanitize_term_meta_text' );
}
// SANITIZE DATA
function ___sanitize_term_meta_text ( $value ) {
return sanitize_text_field ($value);
}
// GETTER (will be sanitized)
function ___get_term_meta_text( $term_id ) {
$value = get_term_meta( $term_id, '__term_meta_text', true );
$value = ___sanitize_term_meta_text( $value );
return $value;
}
// ADD FIELD TO CATEGORY TERM PAGE
add_action( 'category_add_form_fields', '___add_form_field_term_meta_text' );
function ___add_form_field_term_meta_text() { ?>
<?php wp_nonce_field( basename( __FILE__ ), 'term_meta_text_nonce' ); ?>
<div class="form-field term-meta-text-wrap">
<label for="term-meta-text"><?php _e( 'TERM META TEXT', 'text_domain' ); ?></label>
<input type="text" name="term_meta_text" id="term-meta-text" value="" class="term-meta-text-field" />
</div>
<?php }
// ADD FIELD TO CATEGORY EDIT PAGE
add_action( 'category_edit_form_fields', '___edit_form_field_term_meta_text' );
function ___edit_form_field_term_meta_text( $term ) {
$value = ___get_term_meta_text( $term->term_id );
if ( ! $value )
$value = ""; ?>
<tr class="form-field term-meta-text-wrap">
<th scope="row"><label for="term-meta-text"><?php _e( 'TERM META TEXT', 'text_domain' ); ?></label></th>
<td>
<?php wp_nonce_field( basename( __FILE__ ), 'term_meta_text_nonce' ); ?>
<input type="text" name="term_meta_text" id="term-meta-text" value="<?php echo esc_attr( $value ); ?>" class="term-meta-text-field" />
</td>
</tr>
<?php }
// SAVE TERM META (on term edit & create)
add_action( 'edit_category', '___save_term_meta_text' );
add_action( 'create_category', '___save_term_meta_text' );
function ___save_term_meta_text( $term_id ) {
// verify the nonce --- remove if you don't care
if ( ! isset( $_POST['term_meta_text_nonce'] ) || ! wp_verify_nonce( $_POST['term_meta_text_nonce'], basename( __FILE__ ) ) )
return;
$old_value = ___get_term_meta_text( $term_id );
$new_value = isset( $_POST['term_meta_text'] ) ? ___sanitize_term_meta_text ( $_POST['term_meta_text'] ) : '';
if ( $old_value && '' === $new_value )
delete_term_meta( $term_id, '__term_meta_text' );
else if ( $old_value !== $new_value )
update_term_meta( $term_id, '__term_meta_text', $new_value );
}
// MODIFY COLUMNS (add our meta to the list)
add_filter( 'manage_edit-category_columns', '___edit_term_columns' );
function ___edit_term_columns( $columns ) {
$columns['__term_meta_text'] = __( 'TERM META TEXT', 'text_domain' );
return $columns;
}
// RENDER COLUMNS (render the meta data on a column)
add_filter( 'manage_category_custom_column', '___manage_term_custom_column', 10, 3 );
function ___manage_term_custom_column( $out, $column, $term_id ) {
if ( '__term_meta_text' === $column ) {
$value = ___get_term_meta_text( $term_id );
if ( ! $value )
$value = '';
$out = sprintf( '<span class="term-meta-text-block" style="" >%s</div>', esc_attr( $value ) );
}
return $out;
}
// ADD JAVASCRIPT & STYLES TO COLUMNS
add_action( 'admin_enqueue_scripts', '___admin_enqueue_scripts' );
function ___admin_enqueue_scripts( $hook_suffix ) {
if ( 'edit-tags.php' !== $hook_suffix || 'category' !== get_current_screen()->taxonomy )
return;
// ADD YOUR SUPPORTING CSS / JS FILES HERE
// wp_enqueue_style( 'wp-color-picker' );
// wp_enqueue_script( 'wp-color-picker' );
add_action( 'admin_head', '___meta_term_text_print_styles' );
add_action( 'admin_footer', '___meta_term_text_print_scripts' );
}
// PRINT OUR CUSTOM STYLES
function ___meta_term_text_print_styles() { ?>
<style type="text/css">
.column-__term_meta_text { background-color:rgb(249, 249, 249); border: 1px solid lightgray;}
.column-__term_meta_text .term-meta-text-block { display: inline-block; color:darkturquoise; }
</style>
<?php }
// PRINT OUR CUSTOM SCRIPTS
function ___meta_term_text_print_scripts() { ?>
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
$input_field = $( '.term-meta-text-field' );
// console.log($input_field); // your input field
} );
</script>
<?php }
Yes, you can, it is called taxonomy (the custom category). The hook you are looking for is:
$taxonomy_edit_form_fields
The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug (custom taxonomy you have registered. Like e.g. for care_types it will be car_types_edit_form_fields).
Source:
https://developer.wordpress.org/reference/hooks/taxonomy_edit_form_fields/
Note the hook you referred above is deprecated see this page:
This hook has been deprecated. Use {$taxonomy}_edit_form_fields
instead.
Source:
https://developer.wordpress.org/reference/hooks/edit_category_form_fields/

Resources