How to show 0 star rating in woocommerce? - woocommerce

Woocommerce won't display any star rating if there are no ratings for a product. Is there a hook that I can use which will allow the star rating to be shown at all times even if it is 0?

By Default woocommerce show the rating greater than 0
To show 0 rating too we need to remove the action that shows rating like
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
Then add custom function to show the rating
add_action('woocommerce_after_shop_loop_item', 'get_star_rating' );
function get_star_rating()
{
global $woocommerce, $product;
$average = $product->get_average_rating();
echo '<div class="star-rating"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>';
}
Solution found in the https://stackoverflow.com/a/20794406/846060

Related

How to display variation weights and prices of a product on shop page?

Example image
I want the weights and prices of a product to appear on my shop page like in the example image above (weights and prices are to the right of the product title in the example). How do I do this?
I have tried this code, but it doesn't show my variation weights and prices on the shop page.
/**
* Show product weight on archive pages
*/
add_action( 'woocommerce_after_shop_loop_item', 'rs_show_weights', 9 );
function rs_show_weights() {
global $product;
$weight = $product->get_weight();
if ( $product->has_weight() ) {
echo '<div class="product-meta"><span class="product-meta-label">Weight: </span>' . $weight . get_option('woocommerce_weight_unit') . '</div></br>';
}
}

How to hide add to cart button on single product page - woocommerce

I'm new to wordpress and woocommerce. I've to hide add to cart button if the product weight is greater than 8 grams. I've used this code to do so.
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){
$weight = $product->get_weight();
preg_replace('/\D/', '', $weight);
if ( $weight > 8 ){
$button = '';
}
return $button;
}
Its working great on shop page. But its not working on the single product page. Kindly help me to hide the add to cart button in single product page only if the product's weight is greater than 8 grams.
Your code is good for the shop page, for the single page you need to use a different hook:
// Removing the single product button add to cart
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
global $product;
$weight = $product->get_weight();
preg_replace('/\D/', '', $weight);
if ( $weight > 8 ){
// For variable product types
remove_action( 'woocommerce_single_variation','woocommerce_single_variation_add_to_cart_button', 20 );
// For all other product types
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}

WooCommerce Price Variable Display on Single Product Page

I'm actually working on a WooCommerce and I want to display a custom label at single product page that contain the product price divided by 12.
At the moment i'm doing it with a custom field manually input the price but I want to do it automatically
How can i get the product price variable and show it divided by 12
Something like
If the product has regular price divided by 12 if not get sale price and do it
Thanks for helping me.
Edit:
I can show the price divided by 12 with the following code on single product pages and products loop:
add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
// Only on sale products on frontend and excluding min/max price on variable products
if( $product->is_type('simple') ){
// Get product prices
$regular_price = (float) $product->get_regular_price(); // Regular price
$sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)
$precision = 2; // Max number of decimals
if (empty($sale_price)) {
$cuotaprecioregular = round ($regular_price / 12, $precision);
$price .= sprintf( __('<p class="saved-sale">12 cuotas de: %s</p>', 'woocommerce' ), $cuotaprecioregular );
}
else {
$cuotapreciosale = round ($sale_price / 12, $precision);
$price .= sprintf( __('<p class="saved-sale">12 cuotas de: %s</p>', 'woocommerce' ), $cuotapreciosale );
}
}
return $price;
}
But also I'm trying to show the product price with a 20% increment on a popup page thats opens on my product page. With this function the price is showing it below the original price
How can I do the same but only show it in the desired popup?

Set Discounted Price for Related Product on Product Page

I am trying to set the price of all related products on the products page to 10% less = 0.9
The goal is to provide a discount of all related products on the product page but when viewed as a product, give the normal price.
Overall, the idea is to provide incentive that generates up-selling of related products.
I am asking for two things here. ONE: change the product price for related products on the product page (10% off) and TWO: carry that discounted price into the cart and checkout when the related discounted product is added to cart from the product page.
I almost got the first part down, but the code I'm trying to get working is giving me an error saying:
Warning: A non-numeric value encountered
My code so far:
add_filter( 'woocommerce_get_price_html', 'related_product_price_discount', 100, 2 );
function related_product_price_discount( $price, $product ) {
global $woocommerce_loop;
// make sure this is a related product on product page
if( is_product() && $woocommerce_loop['name'] == 'related' ){
$price = $price * 0.9;
}
// return related product price with discount
return apply_filters( 'woocommerce_get_price', $price );
}
The rule in StackOverFlow is one question at the time, so I will answer only your first question, which is related to your code issue...
Note that the hook woocommerce_get_price_html is related to the displayed formatted price.
To avoid the error "Warning: A non-numeric value encountered", you will use the following:
add_filter( 'woocommerce_get_price_html', 'related_product_price_discount', 100, 2 );
function related_product_price_discount( $price_html, $product ) {
global $woocommerce_loop;
// make sure this is a related product on product page
if( is_product() && $woocommerce_loop['name'] == 'related' ){
$price_html = wc_price( wc_get_price_to_display( $product ) * 0.9 );
}
// return related product displayed discounted formatted price
return $price_html;
}
Code goes in functions.php file of your active child theme (active theme). Tested and works.

Woocommerce Applying Coupon on Specific product in Cart

I want to apply the discount coupon to the specific product in cart from checkout page.
Inside the cart:
product1 w/ upgrades
product2 w/o upgrades
when I apply the discount coupon I want that the discount will take effect only in product2's price. I tried to search in some forums but the coupon will take effect in the whole cart items.
You can try this piece of code:
function calculate_variation_addon_price( $cart_object ) {
global $isProcessed;
// Loop for all products in cart
foreach ( $cart_object->cart_contents as $key => $value ) {
// Your condition here for product specific i.e. id == 25
if((!isset($isProcessed) || empty($isProcessed))) {
$orgPrice = floatval( $value['data']->get_price() );
$cart_object->cart_contents[$key]['data']->set_price( $orgPrice * 0.5 );
}
}
$isProcessed = 1;
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_variation_addon_price', 2, 1 );
While adding coupon from admin panel, find a tab there, called 'Usage Restriction'.
There you can add products or product categories or even exclude some for the coupon code to be applied.
You can so that using product id, sku or name
function filter_woocommerce_coupon_get_discount_amount( $discounting_amount, $price_to_discount , $cart_item, $single, $coupon ) {
// On backorder
if ( $cart_item['data']->get_id() !== 'product2_id' ) {
$discounting_amount = 0;
}
return $discounting_amount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
You can use in_array instead of !== for multiple products
You also replace get_id() by other properties or use custom attributs...

Resources