I'm trying to find a function that will make the decimal postions in the Woocommerce prices to have their own CSS class, so i can apply a different style than the one for the the non decimal part of the price. I have multi-lingual and multi-currency website.
I found a code that does what I'm looking for but it was coded in a way that will replace the currency symbol for the brazilian symbol, and because i don't know how to code myself, i could not amend the code to apply to my needs.
Thanks in advance.
Add a <span> to the decimals and create a class just for them :
HTML :
<div name="price">
$ 99.<span class="decimals">99<span>
</div>
CSS :
.decimals {
/* The style you want for the decimals goes here */
}
You need to use formatted_woocommerce_price filter. Check code example. You can change sub to any HTML element you want with your specific class.
add_filter( 'formatted_woocommerce_price', 'style_decimal_price', 10, 5 );
function style_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
$unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
$decimal = sprintf( '%02d', ( $price - intval( $price ) ) * 100 );
return $unit . '<sub class="custom-decimal">' . $decimal_separator. $decimal . '</sub>';
}
Related
I have hidden the label of the variation box with CSS and added a custom text to the select box containing the name of the attribute. This is the code I used for that:
function filter_woocommerce_dropdown_variation_attribute_options_args( $array ) {
$attribute_name = wc_attribute_label($array['attribute']);
$select_text = 'Choose an ' . $attribute_name;
$array['show_option_none'] = __( $select_text, 'woocommerce' );
return $array;
};
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_woocommerce_dropdown_variation_attribute_options_args', 10, 1 );
CSS for hiding the label:
.variations th.label {
display: none;
}
Now I just want to make this sentence capatilized but I can't find the right css for this. I'm almost sure I need to use text-transform: capatilize but I'm not sure where to add it and most important with which css class/id. Can I add it to the snippet maybe?
I tried some answers from similar questions here but they all didn't work.
Any help is appreciated.
function filter_woocommerce_dropdown_variation_attribute_options_args( $array ) {
$attribute_name = wc_attribute_label($array['attribute']);
$select_text = ucwords( 'Choose an ' . $attribute_name );
$array['show_option_none'] = __( $select_text, 'woocommerce' );
return $array;
};
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_woocommerce_dropdown_variation_attribute_options_args', 10, 1 );
Please check the links for changing the string case to upper, lower, capitalize...
This question already has answers here:
Adding a custom text next to a specific product attribute label in WooCommerce
(2 answers)
Closed 10 months ago.
We are building a wine-shop and have a few typical attributes like alcohol, sugar, acid that have values. These attributes are measured in units - e.g. alcohol in vol.%, sugar in g/litre, etc.
The "additional information" tab shows the attribute names and values but just the values without the unit right after.
Consequently we want to add a suffix for each attribute via a snippet/hook in functions.php and have tried with below code:
add_filter( 'woocommerce_attribute_label', 'custom_attribute_label', 10, 3 );
function custom_attribute_label( $label, $name, $product ) {
$taxonomy = 'pa_'.$name;
if( $taxonomy == 'alk' )
$label .= '<span class="custom-label"> vol.%</span>';
return $label;
}
but this does not generate any output on frontend.
Any ideas and/or feedback to solve/tweak this are appreciated!
The condition should be if ( $taxonomy === 'pa_alk' )
I modified the code, now it's working:
add_filter( 'woocommerce_attribute_label', 'custom_attribute_label', 10, 3);
function custom_attribute_label( $label, $taxonomy, $product ) {
if( $taxonomy == 'pa_alk' )
$label .= '<span class="custom-label"> vol.%</span>';
return $label;
}
i've inherited a site & someone has created a plugin to add a subscribe button to single products, which simply links to a separate page.
it's currently appearing on all products, however the client only wants this button/link to appear on products belonging to a specific category.
i know this is a simple change, for anyone but me.
can anyone point me in the right direction here?
product-category/wine or, taxonomy=product_cat&tag_ID=45
here's the code:
function add_link_to_product_page( ) {
global $product;
if( !$product->is_type( 'grouped' ) ) {
echo'<a class="button single_add_to_cart_button" href="' . get_permalink(1073) . '">Subscribe</a>';
}
}
add_action( 'woocommerce_after_add_to_cart_form', 'add_link_to_product_page', 10, 0 );
?>
many, many thanks,
Jason
You can use has_term() function. please check below code.
function add_link_to_product_page() {
global $product;
if( !$product->is_type( 'grouped' ) && has_term( 45, 'product_cat' ) ) {
echo'<a class="button single_add_to_cart_button" href="' . get_permalink(1073) . '">Subscribe</a>';
}
}
add_action( 'woocommerce_after_add_to_cart_form', 'add_link_to_product_page', 10, 0 );
I used snippet for adding prefix before price. It is working nice, but I would like to have it showed only on category product page.
I would like to hide prefix on simple product page.
Can you help me?
Thanks!
Used snippet:
add_filter( ‘woocommerce_get_price_html’, ‘njengah_text_before_price’ );
function njengah_text_before_price($price){
global $post;
$product_id = $post->ID;
$product_array = array( 25338 );//add in the product IDs to add text after price
if ( in_array( $product_id, $product_array )) {
$text_to_add_before_price = ‘ Cena od ‘; //change text in bracket to your preferred text
return $text_to_add_before_price . $price ;
}else{
return $price;
}
}
I'm designing a website in WordPress.
Can anyone help me on how to set a minimum and maximum length for a post title in WordPress?
In functions.php write this code-
function max_title_length( $title ) {
$max = 20;
if( strlen( $title ) > $max ) {
return substr( $title, 0, $max ). " …";
} else {
return $title;
}
}
And then place the function wherever you want in your theme. For example, if you want to show shortened titles on your homepage, you want to place this code into your Main Index Template (index.php)
add_filter( 'the_title', 'max_title_length');
Or you can use built-in function: wp_trim_words().
Use this where you want to display your title with limited words-
<?php echo wp_trim_words( get_the_title(), 5 ); ?>
replace number 5 with whatever number of words you need to display.