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 );
Related
I want to show a condictional unit price on cart line ..
if the payment method is X(bacs) (auto have added discount coupom) I have to take all %porcent discount from this coupons (multiple, sometime) and display price with and without discount and if no have coupom display normal price, I am working on this code, but I am so far to get this working ...
add_filter( 'woocommerce_cart_item_price', 'kd_custom_price_message' );
function kd_custom_price_message( $price) {
global $woocommerce, $coupon_html, $coupon, $discount_amount_html;
$applied_coupons = WC()->cart->get_applied_coupons();
$percent_coupons_only = $applied_coupons->discount_type('percent');
if ( in_array( $percent_coupons_only,$applied_coupons, true ) ) {
$total_percent_discount = $applied_coupons->get_amount();
}
}
// Get the chosen payment gateway (dynamically)
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if( $chosen_payment_method == 'bacs'){
$price_to_be_disconted = (($price / 100) * $total_percent_discount)
$discounted_price = ($price - $price_to_be_disconted);
// price with discount (I have to add CSS)
}
$card_label = '<div style="font-size:11px; color:#ada9a9; text-align:center;">3x s/ juros</div>';
// split payment 3x label
$discounted_price_label = '<div style="font-size:11px; color:green; text-align:center;">ou a vista por:</div>';
// custom sale price label for $discounted_price
return $card_label.'<div style="text-align:center;">'.$price.'</div>'.$discounted_price_label. $discounted_price ;
}
How do I add the price of a subscription to the Subscribe button?
Instead of only show "Subscribe" I would like to state "Subscribe - $140" or "Subscribe every 2nd month for $140"
I have this code that works perfect with all simple products, but not subscriptions...
/**
* #snippet Add Price to "Add to Cart" Button
* #author LoicTheAztec
* #compatible WooCommerce 5.8
* #source https://stackoverflow.com/a/51584094/4275509
*/
add_filter('woocommerce_product_add_to_cart_text', 'custom_add_to_cart_price', 20, 2); // Shop and other archives pages
add_filter('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 20, 2); // Single product pages
function custom_add_to_cart_price($button_text, $product)
{
// Variable products
if ($product->is_type('variable')) {
// shop and archives
if (!is_product()) {
$product_price = wc_price(wc_get_price_to_display($product, array('price' => $product->get_variation_price())));
return $button_text . ' - From ' . strip_tags($product_price);
}
// Single product pages
else {
return $button_text;
}
}
// All other product types
else {
$product_price = wc_price(wc_get_price_to_display($product));
return $button_text . ' – ' . strip_tags($product_price);
}
}
```
You can get product type using get_the_terms() function. try below code. code will go in your active theme functions.php file.
add_filter('woocommerce_product_add_to_cart_text', 'custom_add_to_cart_price', 20, 2); // Shop and other archives pages
add_filter('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 20, 2); // Single product pages
function custom_add_to_cart_price($button_text, $product){
$product_type = get_the_terms( $product->get_id(),'product_type' )[0]->slug;
// Variable products
if ( $product_type == 'subscription' ) {
$product_price = wc_price(wc_get_price_to_display($product));
return $button_text . ' – ' . strip_tags($product_price);
} else {
return $button_text;
}
}
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?
I need to create a coupon in WooCommerce that will only work if the user has an email which ends in .ac.uk.
For example student.name#uwl.ac.uk or teacher#kings.ac.uk.
The discount would be 10% off the total shopping basket.
I can't find any solutions online or any plugins which could help.
Any ideas?
Thanks.
You can use this Auto apply Discount Coupon to Customer’s Purchase function with the right conditionals (user email finishing by ac.uk):
add_action('woocommerce_before_cart_table', 'coupon_auto_apply_user_email_tld', 10, 0);
function coupon_auto_apply_user_email_tld() {
$current_user = wp_get_current_user();
$user_email = $current_user->user_email;
$mail_part = explode('#', $user_email);
$domain_part = explode('.', $mailParts[1]);
$mail_tld = $domain_part[1] . '.' . $domain_part[2];
if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;
if ( $mail_tld == 'ac.uk' ) {
$coupon_code = 'UNIQUECODE'; // <= set the name of your coupon code here
if ( ! WC()->cart->add_discount( sanitize_text_field( $coupon_code ) ) ) {
WC()->show_messages();
}
echo '<div class="woocommerce_message"><strong>The total price in your cart will be discounted 10% off…</strong></div>';
}
else
{
return;
}
}
You will have to set in WooCommerce coupon settings a coupon with the desire behavior (10% off) and you will report this coupon slug in this function.
Copy code in the functions.php file located in your active theme or better your active child theme.
I have a WordPress site that uses WooCommerce plug-in. When a discount is applied to a product, the cart displays the price as crossed out with the discounted price at the bottom.
I would like to have a message with the last date of the discount, something like "$100-early bird rate applies up to 2015-09-30". How would I tweak the discount display on the cart or is there a plug-in for this?
Thanks in advance
Sale end date is stored as product meta, you can pull it using get_post_meta , the filter to use is get_price_html
add_filter( 'woocommerce_get_price_html', 'so32504214_price_html', 20, 2 );
function so32504214_price_html( $price, $product ){
$date = get_post_meta( $product->id, '_sale_price_dates_to', true );
if( strstr( $price, "<ins>" ) && "" != $date ) {
$date = date("Y-m-d", $date);
return str_replace( '</ins>', ' </ins> - early bird rate applies up to '. $date , $price );
}
return $price;
}