WooCommerce Price Variable Display on Single Product Page - wordpress

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?

Related

Woocommerce custom price after actual price

I'm in search for a function that could help me to add a custom price for my woocommerce (7.2.2 version) shop products. (Only as text in frontend). I'm using the yith membership to give the discount to my customers already, but I have no option to display discounted prices before the users can buy the membership.
Example:
My price is 2$ and I want to add the following text after price: 1$ with subscription x.
demo:
Product title
2$
1$ with subscription x
The function need to calculate the price with subscription x using the following fixed discount
original price - 50% = subscription price
Also this need to have some condition if the product already have a discount.
Product title
<del>2$</del> 1.8$
0,9$ with subscription x
I tried some modules which allowed me to add some text as current price suffix, but there is no way to do a calculation of the custom price using those modules.
Final solution : i added a 25% discount + custom text
function wptips_custom_html_addon_to_price( $price, $product ) {
$pretinitial = $price;
$sale_price = $product->sale_price;
if (!empty($sale_price)) {
$pretfinalscos = $sale_price;
}
else
{
$pretfinalscos = $pretinitial;
}
$pretfinal = preg_replace("/[^0-9,.]/", "", "$pretfinalscos");
$pretfinal = str_replace(',', '.', $pretfinal);
$pricegold = $pretfinal - ($pretfinal * (25 / 100));
$html_price_suffix = '<span style="color:black;" class="price-suffix"> <br>Cu abonamentul Gold '.number_format((float)$pricegold, 2, '.', '').' lei</span>'; // custom suffix html that you want to add after price
if ( !empty( $price ) ) {
$price = $html_price_prefix . ' ' . $price . ' ' . $html_price_suffix;
return $price;
} else {
return $price;
}
}
add_filter( 'woocommerce_get_price_html', 'wptips_custom_html_addon_to_price', 999, 2 );
add_filter( 'woocommerce_cart_item_price', 'wptips_custom_html_addon_to_price', 999, 2 );

How to add /kg or /each after price in WooCommerce when selling based on weight and unit

I don't want to do complicated price calculations, I just want to display /kg when I'm selling the product by weight or /each when I'm selling the product by the unit to be shown after product price in the same shop.
for example:
if the product is a shirt, I would like the price to show like $9.99/each.
if the product is Apples, I would like the price to show like $2.99/kg.
Is there a simple way to achieve this?
You can use the following code to add a suffix after the price
It is checked by product category, because in a certain way you have to determine what the product belongs to
https://developer.wordpress.org/reference/functions/has_term/
Unless you want to determine in another way how to check where the product belongs to?
function add_price_suffix( $html, $product, $price, $qty ) {
// Category - shirt
if ( has_term( 'shirt', 'product_cat' ) ) {
$html .= '/each.';
} elseif ( has_term( 'apples', 'product_cat' ) ) {
$html .= '/kg.';
} else {
$html .= '/.';
}
return $html;
}
add_filter( 'woocommerce_get_price_suffix', 'add_price_suffix', 10, 4 );

woocommerce calculate line total and cart totals

As you can see in the image, I have a cart that has two products.
Each of the products has some addons.
The "addon#1" costs 20chf.
If a customer chooses 3 "test product 1" products and adds 2 "addon#1" addons,
I want to calculate first the quantity and then to add the cost of the addons.
For example, in the image, for the first item in the cart, 120 * 3 = 360 and then 2 addons * 20 = 40,
I want in Total to have 400.
I tried some actions like woocommerce_after_calculate_totals,
woocommerce_cart_item_subtotal,
woocommerce_cart_product_subtotal but even if I got the results I want, the total cost of the cart is not the right, it doesn't include the addons.
function add_addons_to_total_price( $product_subtotal, $product, $quantity, $cart ) {
$subtotal_product_price = 0;
$product_price = $product->get_price();
$subtotal_product_price = $product_price * $quantity;
foreach( $cart->get_cart() as $key=>$value ) {
if( isset( $value['total_addons_price'] ) ) {
$subtotal_product_price += $value['total_addons_price'];
}
}
return ($subtotal_product_price) ? wc_price($subtotal_product_price) : $product_subtotal;
}
add_filter( 'woocommerce_cart_product_subtotal', 'add_addons_to_total_price', 10, 4 );
Do you have any ideas on how to change the total cost of each item in cart and update the total cost of the cart?

Setting up an error message for minimum quantity in WooCommerce

I set up a minimum quantity for a product category in WooCommerce. In my case I set the minimum quantity to 5 items. The code I use works fine but I would like to add two error messages for the customer:
1) If the customer tries to change the the quantity to less than the minimum by clicking the "-" symbol I would like to have something like: "The minimum quantity of this product is 5, please add at least 5 items to the basket"
2) If the customer clicks the "add to the basket" button I would like to have something like this: "There is a minimum quantity of 5 items for this product. Please check your basket"
Is there some code I can add to my actual code?
add_filter( 'woocommerce_quantity_input_args', 'min_qty_filter_callback', 20, 2 );
function min_qty_filter_callback( $args, $product ) {
$category = 'Noten'; // The targeted product category
$min_qty = 5; // The minimum product quantity
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
if( has_term( $category, 'product_cat', $product_id ) ){
$args['min_value'] = $min_qty;
}
return $args;
}
For add to cart validation add the follows code snippet to validate your item quantity -
function add_to_cart_validation( $flag, $product_id, $qunatity ) {
if( $qunatity < 5 ) { // if quantity less than 5
wc_add_notice( __( 'There is a minimum quantity of 5 items for this product. Please check your basket', 'text-domain' ), 'error' );
$flag = false;
}
return $flag;
}
add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation', 10, 3 );

How to show 0 star rating in 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

Resources