Hi i enabled the review on woocommerce and doesn't show on products page and i put this code in snippets
function bbloomer_product_reviews_shortcode( $atts ) {
if ( empty( $atts ) ) return '';
if ( ! isset( $atts['id'] ) ) return '';
$comments = get_comments( 'post_id=' . $atts['id'] );
if ( ! $comments ) return '';
$html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';
foreach ( $comments as $comment ) {
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$html .= '<li class="review">';
$html .= get_avatar( $comment, '60' );
$html .= '<div class="comment-text">';
if ( $rating ) $html .= wc_get_rating_html( $rating );
$html .= '<p class="meta"><strong class="woocommerce-review__author">';
$html .= get_comment_author( $comment );
$html .= '</strong></p>';
$html .= '<div class="description">';
$html .= $comment->comment_content;
$html .= '</div></div>';
$html .= '</li>';
}
$html .= '</ol></div></div>';
return $html;
}
add_shortcode( 'woocommerce_after_single_product', 'bbloomer_product_reviews_shortcode' );
I checked the enable review to the all products and in woocommerce to and in the discussions
You can add a new tab using the action hook woocommerce_product_tabs. check the below code. code goes in your active theme functions.php file.
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
$tabs['products_review_tab'] = array(
'title' => __( 'Reviews', 'woocommerce' ),
'priority' => 120,
'callback' => 'products_review_tab_content'
);
return $tabs;
}
Now you can echo do_shortcode[] inside products_review_tab_content callback.
function products_review_tab_content() {
global $product;
echo do_shortcode( '[product_reviews_shortcode id="'.$product->id.'"]' );
}
function product_reviews_shortcode( $atts ) {
$atts = shortcode_atts( array(
'id' => ''
), $atts, 'product_reviews_shortcode' );
$comments = get_comments( array(
'post_id' => $atts['id']
) );
if ( ! $comments ) return '';
$html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';
foreach ( $comments as $comment ) {
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$html .= '<li class="review">';
$html .= get_avatar( $comment, '60' );
$html .= '<div class="comment-text">';
if ( $rating ) $html .= wc_get_rating_html( $rating );
$html .= '<p class="meta"><strong class="woocommerce-review__author">';
$html .= get_comment_author( $comment );
$html .= '</strong></p>';
$html .= '<div class="description">';
$html .= $comment->comment_content;
$html .= '</div></div>';
$html .= '</li>';
}
$html .= '</ol></div></div>';
return $html;
}
add_shortcode( 'product_reviews_shortcode', 'product_reviews_shortcode' );
Tested and works.
Related
I've tried several things adding the code to functions.php.
I think that the time I've been closer was with this code:
printf('<a class="button" href=" ..... "?>');
echo woocommerce_get_product_thumbnail();
printf('</a>');
}
But I don't know what to href on there.
previously, I removed the other liks with:
/*remove links to loop, add link to title*/
remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10);
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5);
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_open', 5);
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_close', 20);
function woocommerce_template_loop_product_title() {
echo sprintf('<h2 class="woocommerce-loop-product__title"><a title="%2$s" href="%1$s">%2$s</a></h2>',
get_the_permalink(),
get_the_title()
);
}
Thank you very much
There may be some modifications that need to be made for it to fit your scenario.
Add the following code to your functions.php file.
This will only add the link to the thumbnail if you are on the products page.
function add_link_woocommerce_single_product_image_thumbnail_html( $html, $post_thumbnail_id ) {
/**
* This will only add the link on a single product page.
*/
if ( is_product() ) :
global $product;
$product_id = $product->get_id();
$product_url = get_permalink( $product_id );
$main_image = true;
$flexslider = (bool) apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) );
$gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
$thumbnail_size = apply_filters( 'woocommerce_gallery_thumbnail_size', array( $gallery_thumbnail['width'], $gallery_thumbnail['height'] ) );
$image_size = apply_filters( 'woocommerce_gallery_image_size', $flexslider || $main_image ? 'woocommerce_single' : $thumbnail_size );
$full_size = apply_filters( 'woocommerce_gallery_full_size', apply_filters( 'woocommerce_product_thumbnails_large_size', 'full' ) );
$thumbnail_src = wp_get_attachment_image_src( $post_thumbnail_id, $thumbnail_size );
$full_src = wp_get_attachment_image_src( $post_thumbnail_id, $full_size );
$alt_text = trim( wp_strip_all_tags( get_post_meta( $post_thumbnail_id, '_wp_attachment_image_alt', true ) ) );
$image = wp_get_attachment_image(
$post_thumbnail_id,
$image_size,
false,
apply_filters(
'woocommerce_gallery_image_html_attachment_image_params',
array(
'title' => _wp_specialchars( get_post_field( 'post_title', $post_thumbnail_id ), ENT_QUOTES, 'UTF-8', true ),
'data-caption' => _wp_specialchars( get_post_field( 'post_excerpt', $post_thumbnail_id ), ENT_QUOTES, 'UTF-8', true ),
'data-src' => esc_url( $full_src[0] ),
'data-large_image' => esc_url( $full_src[0] ),
'data-large_image_width' => esc_attr( $full_src[1] ),
'data-large_image_height' => esc_attr( $full_src[2] ),
'class' => esc_attr( $main_image ? 'wp-post-image' : '' ),
),
$post_thumbnail_id,
$image_size,
$main_image
)
);
$html = '<div data-thumb="' . esc_url( $thumbnail_src[0] ) . '" data-thumb-alt="' . esc_attr( $alt_text ) . '" class="woocommerce-product-gallery__image">' . $image . '</div>';
return $html;
endif;
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'add_link_woocommerce_single_product_image_thumbnail_html', 10, 2 );
my link is madbabe.co
inside the grid only has title and date , I want category under the date.
i have found the code for the date, don't know how to add more code to display category.
// Display date if $date is true
if ( 'true' == $date ) {
$date_output = '';
if ( $first_run ) {
$date_style = vcex_inline_style( array(
'color' => $date_color,
'font_size' => $date_font_size,
) );
}
$date_output .= '<div class="vcex-blog-entry-date entry-date"' . $date_style . '>';
$date_output .= get_the_date();
$date_output .= '</div>';
$output .= apply_filters( 'vcex_blog_grid_date', $date_output, $latts );
}
Try making that whole code this:
// Display date if $date is true
if ( 'true' == $date ) {
$date_output = '';
$id = get_the_ID();
$cats = wp_get_post_categories($id);
if ( $first_run ) {
$date_style = vcex_inline_style( array(
'color' => $date_color,
'font_size' => $date_font_size,
) );
}
$date_output .= '<div class="vcex-blog-entry-date entry-date"' . $date_style . '>';
$date_output .= get_the_date();
$date_output .= '</div>';
$date_output .= '<br><div>' . $cats[0]->name; . '</div>';
$output .= apply_filters( 'vcex_blog_grid_date', $date_output, $latts );
}
I'm setting up ACF for selecting options in single product page. I already added the custom fields in single product page. How can I save multiple fields?
This code is working, but I need to save multiple fields like on the added field option-color.
/** Output custom fields field. */
function product_options_output_field() {
$html = "";
if( have_rows('select_options') ):
$html .= "<div class='select-options'>";
$html .= "<h4>Select Options</h4>";
$html .= "<select class='option-multi-trip-one-way' id='option-multi-trip-one-way' name='option-multi-trip-one-way'>";
$html .= "<option value='N/A'>Multi-trip/One-way **</option>";
while( have_rows('select_options') ) : the_row();
$multi_trip = get_sub_field('multi-tripone-way');
$html .= "<option value=".$multi_trip.">".$multi_trip."</option>";
endwhile;
$html .= "</select>";
$html .= "<br/>";
$html .= "<select class='option-color' id='option-color' name='option-color'>";
$html .= "<option value='N/A'>Color</option>";
while( have_rows('select_options') ) : the_row();
$color = get_sub_field('color');
$html .= "<option value=".$color.">".$color."</option>";
endwhile;
$html .= "</select>";
$html .= "</div>";
endif;
echo $html;
}
add_action( 'woocommerce_before_add_to_cart_button', 'product_options_output_field', 10 );
/** Add custom fields to cart item. */
function product_options_add_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
$option_multi_trip_one_way = filter_input( INPUT_POST, 'option-multi-trip-one-way');
if ( empty( $option_multi_trip_one_way ) ) {
return $cart_item_data;
}
$cart_item_data['option-multi-trip-one-way'] = $option_multi_trip_one_way;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'product_options_add_to_cart_item', 10, 3 );
/**Display custom fields in the cart. */
function product_options_display_cart( $item_data, $cart_item ) {
if ( empty( $cart_item['option-multi-trip-one-way'] ) ) {
return $item_data;
}
$item_data[] = array(
'key' => __( 'Multi-trip/One-way **' ),
'value' => wc_clean( $cart_item['option-multi-trip-one-way'] ),
'display' => '',
);
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'product_options_display_cart', 10, 2 );
/** Add custom fields to order. */
function product_options_to_order_items( $item, $cart_item_key, $values, $order ) { if ( empty( $values['option-multi-trip-one-way'] ) ) {
return; } $item->add_meta_data( __( 'Multi-trip/One-way **' ), $values['option-multi-trip-one-way'] );}add_action( woocommerce_checkout_create_order_line_item', 'product_options_to_order_items', 10, 4 );
http://prntscr.com/n42nig http://prntscr.com/n42nz0 http://prntscr.com/n42oj5
/**Output ACF in single product. */
function product_options_output_field() {
$html = "";
if( have_rows('select_options') ):
$html .= "<div class='select-options'>";
$html .= "<h4>Select Options</h4>";
$html .= "<select class='option-multi-trip-one-way' id='option-multi-trip-one-way' name='option-multi-trip-one-way'>";
$html .= "<option value='N/A'>Multi-trip/One-way **</option>";
while( have_rows('select_options') ) : the_row();
$multi_trip = get_sub_field('multi-tripone-way');
$html .= "<option value=".$multi_trip.">".$multi_trip."</option>";
endwhile;
$html .= "</select>";
$html .= "<br/>";
$html .= "<select class='option-color' id='option-color' name='option-color'>";
$html .= "<option value='N/A'>Color</option>";
while( have_rows('select_options') ) : the_row();
$color = get_sub_field('color');
$html .= "<option value=".$color.">".$color."</option>";
endwhile;
$html .= "</select>";
$html .= "</div>";
endif;
echo $html;
}
add_action( 'woocommerce_before_add_to_cart_button', 'product_options_output_field', 10 );
/** * Add ACF to cart item. */
function product_options_add_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
$option_multi_trip_one_way = filter_input( INPUT_POST, 'option-multi-trip-one-way');
$option_color = filter_input( INPUT_POST, 'option-color');
if ( empty( $option_multi_trip_one_way) || empty( $option_color)) {
return $cart_item_data;
}
$cart_item_data['option-multi-trip-one-way'] = $option_multi_trip_one_way;
$cart_item_data['option-color'] = $option_color;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'product_options_add_to_cart_item', 10, 3 );
/** Display ACF in the cart.*/
function product_options_display_cart( $item_data, $cart_item ) {
if ( empty( $cart_item['option-multi-trip-one-way'] ) || empty( $cart_item['option-color'] )) {
return $item_data;
}
$item_data[] = array(
'key' => __( 'Multi-trip/One-way **' ),
'value' => wc_clean( $cart_item['option-multi-trip-one-way'] ),
'display' => '',
);
$item_data[] = array(
'key' => __( 'Color' ),
'value' => wc_clean( $cart_item['option-color'] ),
'display' => '',
);
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'product_options_display_cart', 10, 2 );
/** Add ACF to order.*/
function product_options_to_order_items( $item, $cart_item_key, $values, $order ) {
if ( empty( $values['option-multi-trip-one-way'] ) || empty( $values['option-color'] )) {
return;
}
$item->add_meta_data( __( 'Multi-trip/One-way **' ), $values['option-multi-trip-one-way'] );
$item->add_meta_data( __( 'Color' ), $values['option-color'] );
}
add_action( 'woocommerce_checkout_create_order_line_item', 'product_options_to_order_items', 10, 4 );
I'm trying to overylay woocommerce product title on its featured image, as i replaced featured image with some plane colored background and on it i need to call woocommerce product title. Please share how can i achieve this ?
if ( has_post_thumbnail() ) {
$html = '<div data-thumb="' . get_the_post_thumbnail_url( $post->ID, 'shop_thumbnail' ) . '" class="woocommerce-product-gallery__image"><a href="' . esc_url( $full_size_image[0] ) . '">';
$html .= get_the_post_thumbnail( $post->ID, 'shop_single', $attributes );
$html .= '</a></div>';
} else {
$html = '<div class="woocommerce-product-gallery__image--placeholder">';
$html .= sprintf( '<img src="%s" alt="%s" class="wp-post-image" />', esc_url( wc_placeholder_img_src() ), esc_html__( 'Awaiting product image', 'woocommerce' ) );
$html .= '<div class="centered">';
$html .= how to call here!!!
$html .= '</div>';
$html .= '</div>';
}
You need to get the product title from the product like this:
$product = wc_get_product( $post->ID );
$title = get_the_title($product->ID);
So your code becomes :
if ( has_post_thumbnail() ) {
$html = '<div data-thumb="' . get_the_post_thumbnail_url( $post->ID, 'shop_thumbnail' ) . '" class="woocommerce-product-gallery__image"><a href="' . esc_url( $full_size_image[0] ) . '">';
$html .= get_the_post_thumbnail( $post->ID, 'shop_single', $attributes );
$html .= '</a></div>';
} else {
$product = wc_get_product( $post->ID );
$title = get_the_title($product->ID);
$html = '<div class="woocommerce-product-gallery__image--placeholder">';
$html .= sprintf( '<img src="%s" alt="%s" class="wp-post-image" />', esc_url( wc_placeholder_img_src() ), esc_html__( 'Awaiting product image', 'woocommerce' ) );
$html .= '<div class="centered">';
$html .= $title ;
$html .= '</div>';
$html .= '</div>';
}
An edit :
Since you are within the loop, you do not need to get the product using the function I used. You can just use the global $product variable if the post is a product.
I want to remove the nofollow code from the latest posts displayed in the sidebar. I found that the code which adds rel=nofollow tag to latest post is located here
theme folder/example theme/lib/activity/plugin.php.
Here is the exact code:
private function get_latest_posts( $post_count ) {
// Get the latest posts
$latest_posts = get_posts(
array(
'numberposts' => $post_count,
'order' => 'desc',
'orderby' => 'date'
)
);
// Create the markup for the listing
$html = '<div class="tab-pane" id="recent">';
$html .= '<ul class="latest-posts">';
if( count( $latest_posts ) > 0 ) {
foreach( $latest_posts as $post ) {
$html .= '<li class="clearfix">';
// Add the small featured image
if( has_post_thumbnail( $post->ID ) ) {
$html .= '<a class="latest-post-tn fademe" href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
$html .= get_the_post_thumbnail( $post->ID, array( 50, 50 ) );
$html .= '</a>';
} // end if
$html .='<div class="latest-meta">';
// Add the title
$html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
$html .= get_the_title( $post->ID );
$html .= '</a>';
// Add date posted
// If there's no title, then we need to turn the date into the link
if( strlen( get_the_title( $post->ID ) ) == 0 ) {
$html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
} // end if
$html .= '<span class="latest-date">';
$html .= get_the_time( get_option( 'date_format' ), $post->ID );
$html .= '</span>';
// Close the anchor
if(strlen( get_the_title( $post->ID ) ) == 0 ) {
$html .= '</a>';
} // end if
$html .='</div>';
$html .= '</li>';
} // end foreach
} else {
$html .= '<li>';
$html .= '<p class="no-posts">' . __( "You have no recent posts.", 'standard' ) . '</p>';
$html .= '</li>';
} // end if/else
$html .= '</ul>';
$html .= '</div>';
return $html;
} // end get_latest_posts
Now please tell me how to remove the nofollow tag from this code using the child theme?
Since you have control of the child theme, you can wrap the call to display the widget zone for that widget with something that grabs the output, performs a regex search/replace on it, and outputs the result. I wrote a blog post about that recently:
Filtering the output of WordPress widgets
The basics are that you have a function that replaces dynamic_sidebar() with your own function, like this:
function theme_dynamic_sidebar($index = 1) {
// capture output from the widgets
ob_start();
$result = dynamic_sidebar($index);
$out = ob_get_clean();
// do regex search/replace on $out
echo $out;
return $result;
}
Seems that you are out of luck.
That's a private function and no filter hooks are offered by the theme author.
You may try to override the include('path/to/plugin.php'); and include your own modified version.