Woocommerce product rating only with stars - woocommerce

I am trying to make the woocommerce product rating only with stars. Therefore I need to remove the currently required fields name, email and comment.
I have accomplished to remove comment with this code
<?php
function rei_preprocess_comment($comment_data) {
if ($comment_data['comment_content'] == '%dummy-text%') {
$comment_data['comment_content'] = ''; // replace dummy text.
}
return $comment_data;
}
add_filter('preprocess_comment', 'rei_preprocess_comment');
function rei_wp_footer() {
?>
<script>
jQuery(function($){
var comment = $('textarea#comment');
comment.removeAttr('required'); // remove required attribute of textarea.
$('#commentform').on('submit',function(){
if (comment.val() == '') {
comment.css('text-indent','-999px').val('%dummy-text%'); // change to dummy text.
}
});
});
</script>
<?php
}
add_action( 'wp_footer', 'rei_wp_footer' );
?>
However I am not being able to remove the required name and email field so unregistered user can enter the star product rating.
Please help

This is an option in Wordpress go to Settings > Discussion and look for the required email and name setting. Remove the checkmark and save the settings page.

Related

How can I display text message if woocommerce quantity input is greater than stock quantity?

I would like to display a custom text message to notify customers that their selected input quantity (prior to clicking the "Add to Cart" button). This message will appear if the selected quantity is greater than the existing available stock quantity, right above the quantity selection within the individual product page. For example:
Existing Stock Quantity: 2
User Selects: >2
In such a scenario, I would like to tell the customer something like: "Your selected order quantity is greater than our existing stock. Please expect a delay of up to 2 weeks for our stock to be replenished."
I've tried to add the custom code into Code Snippets which looks something like that:
function display_order_quantity_exceeds_stock_quantity_text( $message, $product ) {
if( $product->woocommerce_quantity_input() > $product->get_stock_quantity()) {
$message = "Your selected order quantity is greater than our existing stock. Please expect a delay of up to 2 weeks for our stock to be replenished.";
}
return $message;
}
Does anyone know how I can obtain the woocommerce_quantity_input and get this to work?
Would prefer to have the solution just by adding a function into Code Snippets, rather than using Javascript (if possible).
Adding this jQuery code in your theme's footer.php will trigger an alert when user enters a value more than stock
<script type="text/javascript">
function show_error($field, $mesg) {
if ($field.prev('.error_msg').length) {
$field.prev('.error_msg').html('<p>' + $mesg + '</p>');
} else {
jQuery('<div class="error_msg" style="color:#f00"><p>' + $mesg + '</p></div>').insertBefore($field);
}
}
function remove_error($field) {
if ($field.prev('.error_msg').length) {
$field.prev('.error_msg').remove();
}
}
jQuery(".quantity input[name=quantity]").on('change', function(e) {
if (jQuery(this).val() > jQuery(this).attr("max")) {
show_error(jQuery(this).parent(".quantity"), "Your selected order quantity is greater than our existing stock. Please expect a delay of up to 2 weeks for our stock to be replenished")
} else {
remove_error(jQuery(this).parent(".quantity"));
}
})
</script>
You must have to enable Manage stock, and have set a stock number for this product to get it working.
I would do this through jQuery, so add this to your functions.php (the $src variable points to your JS file location in your theme folder):
function a3_enqueue_scripts() {
if(is_singular( 'product' )){
$handle = 'a3_wooc_js';
//path to your Javascript file
$src = get_theme_file_uri( '/js/a3_wooc.js' );
wp_enqueue_script( $handle, $src, array( 'jquery' ), false, false);
}
}
add_action( 'wp_enqueue_scripts', 'a3_enqueue_scripts' );
And something like this in the included JS file:
(function($){
$(document).ready(function(){
//the jQuery selector depends on your theme output for the quantity text box identifiers
$('[name="quantity"]').on('change', function(e){
var qty_box = $(this);
var error_message = $('<div class="error_msg">Your selected order quantity is greater than our existing stock. Please expect a delay of up to 2 weeks for our stock to be replenished</div>');
console.log(parseInt(qty_box.val()), parseInt(qty_box.attr('max')), qty_box.val() > parseInt(qty_box.attr('max')));
if(parseInt(qty_box.val()) > parseInt(qty_box.attr('max'))) {
// the action to take if the quantity exceeds max stock
if($('.quantity .error_msg').length < 1){
$('.quantity').prepend(error_message);
}
}
else {
$('.quantity .error_msg').remove();
}
});
});
})(jQuery);

Sold Individually field missing in WooCommerce Custom Product type Inventory tab

How can I display "Sold Individually" field in my custom product type "Test Product"?
I tried with the following hook function
static function addField() {
echo "<script>jQuery('.show_if_simple').addClass('show_if_test');
jQuery('._sold_individually_field.show_if_simple').addClass('show_if_test');
</script>";
}
add_action('woocommerce_product_options_general_product_data','addField'));
Please add below code in your function.php file. you need to add class show_if_{your_custom_product_type} in your case it will be show_if_test. replace show_if_simple_rental with show_if_{your_custom_product_type}. for more information click here
function wh_simple_rental_admin_custom_js() {
if ('product' != get_post_type()) :
return;
endif;
?>
<script type='text/javascript'>
jQuery(document).ready(function () {
//for Inventory tab
jQuery('.inventory_options').addClass('show_if_simple_rental').show();
jQuery('#inventory_product_data ._sold_individually_field').parent().addClass('show_if_simple_rental').show();
jQuery('#inventory_product_data ._sold_individually_field').addClass('show_if_simple_rental').show();
});
</script>
<?php
}
add_action('admin_footer', 'wh_simple_rental_admin_custom_js');

Change Review Display Name on Woo-commerce Store

I want to be able to change the name that shows up on the reviews on a woocommerce platform. I have found some information but I am new to WordPress and no one seems to have the answer. Here is a link that I was able to find something but no installation specifics.
I need to know where and how to change this please. Thanks.
Here is the link that I found.
https://silicondales.com/tutorials/woocommerce-tutorials/woocommerce-change-review-author-display-name-username/
I have added a plugin called Username Changer and it let me change the username of the account but it won't update the review username.
Here are some images as of now (3/6/2017)
Snapshot of how user is configured with edited username:
Ok I have figured it out. You will need to post the code into the functions.php file if you do not have a child theme. It is recommended that you set one up but I haven't yet and I just wanted to get this fixed first. Here is a snapshot of what you need to too.
Add the below block of text to the bottom or your custom-functions.php or functions.php page within Appearance > Editor
add_filter('get_comment_author', 'my_comment_author', 10, 1);
function my_comment_author( $author = '' ) {
// Get the comment ID from WP_Query
$comment = get_comment( $comment_ID );
if (!empty($comment->comment_author) ) {
if($comment->user_id > 0){
$user=get_userdata($comment->user_id);
$author=$user->first_name.' '.substr($user->last_name,0,1).'.'; // this is the actual line you want to change
} else {
$author = __('Anonymous');
}
} else {
$author = $comment->comment_author;
}
return $author;
}
Make sure to update the user info.
Missing Sections from the Original Question

Woocommerce I am looking for billing address and order-received email hooks

I have to make two customization on my woocommerce site.
I need to know two main hooks. Someone out there please help me out!
Show custom field values to billing address on order-received page.(I added custom fields on checkout page.)
Need to include these values to customers' order-received email, too.
Thanks for stopping by.
To show custom field in order-received page you have to use
woocommerce_thankyou hook.
Here is the code:
// define the woocommerce_thankyou callback
function action_woocommerce_thankyou($order_id)
{
$my_custom_field = get_post_meta($order_id, '_billing_my_field', TRUE);
}
// add the action
add_action('woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1);
UPDARED This section after post author comment
To add custom billing fields in WooCommerce email you have to use
woocommerce_email_customer_details hook; this will be displayed just
before the customer details.
Here is the code:
add_filter('woocommerce_email_customer_details', 'custom_woocommerce_email_order_meta_fields', 10, 3);
function custom_woocommerce_email_order_meta_fields($order, $sent_to_admin, $plain_text)
{
$_billing_my_field = get_post_meta($order->id, '_billing_my_field', true);
if ($plain_text)
{
echo 'My field is ' . $_billing_my_field;
}
else
{
echo '<p>My field is ' . $_billing_my_field . '</p>';
}
}
All code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Please Note:
I have assuming that you have added a custom billing fields as $fields['billing']['billing_my_field'] using woocommerce_checkout_fields hook.
All the codes are tested and fully functional.

How to add a button to a custom post type in Wordpress?

I have a "Products" custom post type. Normally, this custom post type have an "Add New" button. I want to add another button call "Update from Provider".
Currently, I have modify the Wordpress code (in "wordpress\wp-admin\includes\class-wp-list-table.php") to add that button. In this case, when I update Wordpress, my modified code will be deleted. Therefore, I need to move that button to my plug-in code.
In this case, please help me how to move that button to my plug-in code.
Well, if you opened the core file you saw that there's no action in it where we can hook.
Only a couple of filters. We can use the following:
add_filter( 'views_edit-movies', 'so_13813805_add_button_to_views' );
function so_13813805_add_button_to_views( $views )
{
$views['my-button'] = '<button id="update-from-provider" type="button" title="Update from Provider" style="margin:5px">Update from Provider</button>';
return $views;
}
It produces this:
To put it in an approximate position from where you'd like, use the following:
add_action( 'admin_head-edit.php', 'so_13813805_move_custom_button' );
function so_13813805_move_custom_button( )
{
global $current_screen;
// Not our post type, exit earlier
if( 'movies' != $current_screen->post_type )
return;
?>
<script type="text/javascript">
jQuery(document).ready( function($)
{
$('#update-from-provider').prependTo('span.displaying-num');
});
</script>
<?php
}
Which results in this:

Resources