Skip to content

Instantly share code, notes, and snippets.

@eto4detak
Last active May 26, 2019 12:16
Show Gist options
  • Save eto4detak/968079ee5fea5e4b2743154eb9920456 to your computer and use it in GitHub Desktop.
Save eto4detak/968079ee5fea5e4b2743154eb9920456 to your computer and use it in GitHub Desktop.
woo php rating
<?php
// show rating plagin
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_custom_action', 15 );
function bbloomer_custom_action() {
if(function_exists('the_ratings')) { the_ratings(); }
}
//
add_action( 'comment_form_logged_in_after', 'extend_comment_custom_fields' );
add_action( 'comment_form_after_fields', 'extend_comment_custom_fields' );
function extend_comment_custom_fields() {
global $post;
if($post->post_type == 'pixad-autos'){
echo '<div class="comment-form-rating">
<label for="rating">Your rating</label>
<select name="rating" id="rating-autos" aria-required="true" required="" style="display: none;">
<option value="">Rate…</option>
<option value="5">Perfect</option>
<option value="4">Good</option>
<option value="3">Average</option>
<option value="2">Not that bad</option>
<option value="1">Very poor</option>
</select></div>';
}
}
add_action( 'comment_post', 'save_extend_comment_meta_data' );
function save_extend_comment_meta_data( $comment_id ){
if( !empty( $_POST['rating'] ) ){
$rating = intval($_POST['rating']);
add_comment_meta( $comment_id, 'rating', $rating );
}
}
add_filter( 'comment_text', 'modify_extend_comment');
function modify_extend_comment( $text ){
global $post;
if($post->post_type == 'pixad-autos'){
if( $rating = intval( get_comment_meta( get_comment_ID(), 'rating', true ) ) ) {
$commentrating = '<div class="star-rating"><span style="width:' . ( $rating * 20 ) . '%">' . sprintf( __( '%s out of 5', 'woocommerce' ), $rating ) . '</span></div>';
$text = $text . $commentrating;
return $text;
} else {
return $text;
}
}
}
//?>
/* COMMENTS */
// COMMENT RAITING
$("body").on("init", "#rating-autos", function() {
$("#rating-autos").hide().before('<p class="stars"><span><a class="star-1" href="#">1</a><a class="star-2" href="#">2</a><a class="star-3" href="#">3</a><a class="star-4" href="#">4</a><a class="star-5" href="#">5</a></span></p>')
}).on("click", "#respond p.stars a", function() {
var e = $(this)
, t = $(this).closest("#respond").find("#rating-autos")
, i = $(this).closest(".stars");
return t.val(e.text()),
e.siblings("a").removeClass("active"),
e.addClass("active"),
i.addClass("selected"),
!1
}),
$(".wc-tabs-wrapper, .woocommerce-tabs, #rating-autos").trigger("init");
.tab-content
#reviews #comments ol.commentlist ul.children .star-rating {
display: none
}
.tab-content
.star-rating {
float: right;
overflow: hidden;
position: relative;
height: 1em;
line-height: 1;
font-size: 1em;
width: 5.4em;
font-family: star
}
.tab-content
.star-rating:before {
content: "\73\73\73\73\73";
color: #d3ced2;
float: left;
top: 0;
left: 0;
position: absolute
}
.tab-content
.star-rating span {
overflow: hidden;
float: left;
top: 0;
left: 0;
position: absolute;
padding-top: 1.5em
}
.tab-content
.star-rating span:before {
content: "\53\53\53\53\53";
top: 0;
position: absolute;
left: 0;
color: orange;
}
.tab-content
.tab-content-product-rating .star-rating {
margin: .5em 4px 0 0;
float: left
}
.tab-content
.products .star-rating {
display: none;
margin: 0 0 .5em;
float: none
}
.tab-content
.hreview-aggregate .star-rating {
margin: 10px 0 0
}
.tab-content
p.stars a:before,
.tab-content
p.stars a:hover~a:before {
content: "\e021"
}
.tab-content
p.stars a {
position: relative;
height: 1em;
width: 1em;
text-indent: -999em;
display: inline-block;
text-decoration: none
}
.tab-content
p.stars a:before {
display: block;
position: absolute;
top: 0;
left: 0;
width: 1em;
height: 1em;
line-height: 1;
font-family: WooCommerce
;
text-indent: 0
}
.tab-content
p.stars.selected a.active:before,
.tab-content
p.stars:hover a:before {
content: "\e020"
}
.tab-content
p.stars.selected a.active~a:before {
content: "\e021"
}
.tab-content
p.stars.selected a:not(.active):before {
content: "\e020"
}
.tab-content
.star-rating {
float: left;
overflow: hidden;
position: relative;
height: 1em;
line-height: 1;
font-size: 1em;
width: 5.4em;
font-family: star
}
.tab-content
.star-rating:before {
content: "\73\73\73\73\73";
color: #d3ced2;
float: left;
top: 0;
left: 0;
position: absolute
}
.tab-content
.star-rating span {
overflow: hidden;
float: left;
top: 0;
left: 0;
position: absolute;
padding-top: 1.5em
}
.tab-content
.star-rating span:before {
content: "\53\53\53\53\53";
top: 0;
position: absolute;
left: 0;
color: orange;
}
.tab-content
.tab-content-product-rating .star-rating {
margin: .5em 4px 0 0;
float: left
}
.tab-content
.products .star-rating {
display: none;
margin: 0 0 .5em;
float: none
}
.tab-content
.hreview-aggregate .star-rating {
margin: 10px 0 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment