Add text before the price - woocommerce

I've been trying for days to add "price" before the product price inside woo commerce single product page. I've tried many variations of this code to no avail.
/** Add "Price" before the price */
function price_text() {
?>
<div class="price-text">
<p>Price</p>
</div>
<?php
}
add_filter('woocommerce_get_price','price_text');
This is the closest I've got, but it shows the price being $0.
I've also started to add this piece of code
'<span class="amount">' . $currency_symbol . $price . '</span>'
to no avail.but
I am really new to PHP and OPP in general. Any help would be greatly appreciated.
I am using the Genesis framework if that makes a difference.

A little improvement to be translatable.
/** Add "Price" before the price */
add_filter('woocommerce_get_price','price_text');
function price_text($price) {
if ( is_woocommerce()){ ?>
<div class="price-text">
<p><?php _e('Price','woothemes'); ?></p>
</div>
<?php
return $price;
}
else {
return $price;
}
}

Yay!
I figured it out!
add_filter('woocommerce_get_price','price_text');
function price_text($price) {
?>
<div class="price-text">
<p>Price</p>
</div>
<?php
return $price;
}
I was just missing the variable
$price
when declaring the function
price_text
and
return $price;
Hope this helps somebody:)
Cheers,
Mike

I added an if else statement so that it only shows on the product pages and not the checkout pages.
/** Add "Price" before the price */
add_filter('woocommerce_get_price','price_text');
function price_text($price) {
if ( is_woocommerce()){
?>
<div class="price-text">
<p>Price</p>
</div>
<?php
return $price;
}
else {
return $price;
}
}
Cheers,
Mike

Related

How to add text between price and short description in WordPress (WooCommerce)?

I have next problem. I need to add text after price of the product and before short description. This text can be added in short description on first line like an option. The text that I want to add show the same price without 3% of itself.
I put this code in function.php of my current theme:
add_action('woocommerce_short_description','wire_price_3',11);
function wire_price_3()
{
$product = wc_get_product();
$product_price = $product->get_regular_price();
$percent_3 = ($price_p *3)/100;
$percet_price = $product_price - $percent_3;
echo "<h1>Wire price: {$percet_price}</h1>";
echo "\n";
}
Eveythring works well but when I add this code my checkout page become a mess and show some code.
So, my question is how to find solution to this problem? BTW how can I make to show your text this text for example in green color.
Thank you.
i have changed your code, please try if it works properly
add_action( 'woocommerce_single_product_summary', 'mx_price_per_installments_w_fee', 30 );
function mx_price_per_installments_w_fee() {
global $product;
$price = (float) $product->get_price();
$feeextended = (3/100) * $price;
$price4extendedinstallments = $price - $feeextended;
echo "
<div id='installments-container'>
<div id='installments-text'>
<div class='bold'>Wire price: <span class='success'>". number_format($price4extendedinstallments, 2, ",", ".") ." €.</span></div>
</div>
</div>
";
}
try this
add_action( 'woocommerce_after_add_to_cart_form', 'mx_price_per_installments_w_fee', 30 );
function mx_price_per_installments_w_fee() {
global $product;
$price = (float) $product->get_price();
$feeextended = (3/100) * $price;
$price4extendedinstallments = $price - $feeextended;
echo "
<div id='installments-container'>
<div id='installments-text'>
<div class='bold'>Wire price: <span class='success'>". number_format($price4extendedinstallments, 2, ",", ".") ." €.</span></div>
</div>
</div>
";
}

Update a text when cart is not empty

I have a banner on my Wordpress website that I want to be updated when the cart is not empty by Ajax.
Here is the non Ajaxified version:
function dynamic_banner() {
$atts = '<div class="woofc-shipping">' . alg_wc_get_left_to_free_shipping('<span style="color:#F79917"><i class="fas fa-shipping-fast" style="margin-left: 5px;"></i>Add %amount_left_for_free_shipping% more for Free shipping</span> <div class="woofc-total-right"></div>') . '</div>';
if ( ! WC()->cart->is_empty() ) {
?>
<script>
jQuery(function($){
$(".ban-text").replaceWith('<?php echo $atts; ?>')});
</script>
<?php
}
}
add_action('wp_head', 'dynamic_banner');
This is the HTML
<div class="head_strip over--hide" style="display: block;">
<p><strong>Free fast delivery*</strong> in purchases over <span class="special-price">295 NIS</span> even during the closure period!</span></p>
</div>
Have found a solution thanks to those threads by LoicTheAztec
Functions.php Code:
// Shortcode for the banner
function display_free_shipping_progress() {
return get_free_shipping_progress();
}
add_shortcode('fs_progress', 'display_free_shipping_progress');
// Function that makes calculations and display
function get_free_shipping_progress() {
$atts = '<div class="woofc-shipping">' . alg_wc_get_left_to_free_shipping('<span style="color:#F79917"><i class="fas fa-shipping-fast" style="margin-left: 5px;"></i>הוסיפי עוד %amount_left_for_free_shipping% למשלוח חינם</span> <div class="woofc-total-right"></div>') . '</div>';
$prodgress = WC()->cart->total;
//Check if cart has something else show default
if( $prodgress > 0 ) {
return '<div class="head_strip over--hide" style="display: block;">' . $atts . '</div>';
} else {
return '<div class="head_strip over--hide" style="display: block;"><span class="ban-text"><strong>*משלוח מהיר חינם</strong > בקניה מעל<span class="special-price">295₪</span> גם בתקופת הסגר!</span></div>';
}
}
// Refreshing "free shipping" progress on cart ajax events
add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_free_shipping_progress' );
function refresh_free_shipping_progress( $fragments ){
$fragments['.head_strip'] = get_free_shipping_progress();
return $fragments;
}
Have passed all HTML needed to shortcode for simplicity like was recommended.

How to hide Woocommerce "out of stock" items price

I'd like to display my out of stock items, but without price in WooCommerce. Is there any easy way to hide price of "out of stock" items?
Thanks!
Adding these to the CSS worked for me. The first one removes the price from the out of stock item's page and the second removes the price from the out of stock item in search results.
.outofstock .price{display:none}
.outofstock .amount{display:none}
Create a file price.php in /your-theme/woocommerce/single-product/ folder. Paste the following code.
$pricelabel = ""; - will be the variable that will display instead of the price if the stock quantity is 0.
If you use $pricelabel = ""; - this will remove the price. You can try $pricelabel = "SOLD OUT!"; or whatever message you want to display if you want.
I actually wrote this code to display a text message instead of a particular price. I just changed it to check stock quantity instead of price.
<?php
/**
* Single Product Price, including microdata for SEO
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $product;
?>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<p class="price"> <?php
$stockamount = $product->get_stock_quantity();
$price = $product->get_price_html();
$pricelabel = "";
if($stockamount == 0)
{
echo $pricelabel;
}
else
{
echo $price;
};
?>
</p>
<meta itemprop="price" content="<?php echo $product->get_price(); ?>" />
<meta itemprop="priceCurrency" content="<?php echo get_woocommerce_currency(); ?>" />
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
</div>
stick this in functions
add_filter( 'woocommerce_get_price_html', 'remove_price_ofs', 10, 2 );
function remove_price_ofs( $price, $product ) {
if ( ! $product->is_in_stock()) {$price = '';}
return $price;
}
In cases like this I use CSS.
I would typically go use Chrome's inspect element (or any code browser), find the class of the Out of Stock price (could be something like .outOfStockPrice).
Then I would use something like Simple Custom CSS to apply my custom CSS (so I do not have to look for a specific file: http://wordpress.org/plugins/simple-custom-css/)
And add this to your custom css:
.outOfStockPrice {
visibility: hidden;
}
For more info on hiding elements using CSS: http://www.kirupa.com/html5/hiding_things_using_css.htm
very simple, woocommerce still add outofstock class to the whole page so use that and than price
this will work
.outofstock .price{display:none}

Browsing custom taxonomy terms on WordPress

I ran into a little problem and I might need your help to sort it out.
I an building a website that uses custom taxonomies and tags posts accordingly. So, there is a page like http://example.com/custom-taxonomy/term that displays all the posts tagged with "term".
The visitor should be able to go to http://example.com/custom-taxonomy/ and see a lists of all the terms that are used inside that custom taxonomy in order to browse them. This list should also be "paginated" since some taxonomies could have quite a lot of terms in them.
Any ideas on how I should handle this?
Thanks a bunch!
I'll answer my own question here, maybe it will help others.
I have created a custom page template that uses get_terms to get the terms for a specific taxonomy and iterates through them displaying them in the desired manner.
I then created a page with the exact same slug as the main taxonomy slug (http://example.com/actors in this case) and thus when going to /actors/ you actually see the page created that acts as an index page for the taxonomy. You can see it in effect at http://couch.ro/actori/
In the actual code I am also using the Taxonomy Images plugin for having images on the actual tags so the get_terms is executed through the apply_filter() function. You have the full code for the template below. Any feedback is highly appreciated!
<?php
/*
Template Name: Taxonomy Index
*/
$slug_to_taxonomy=array('actori'=>'actor','regizori'=>'director');
$terms_per_page=get_option( 'c2c_custom_post_limits' );
if ($terms_per_page['archives_limit']==0)
{
$terms_per_page=get_options('posts_per_page');
}
else
{
$terms_per_page=$terms_per_page['archives_limit'];
}
$slug=$post->post_name;
if (!isset($slug_to_taxonomy[$slug]))
{
header("Location: /");exit;
}
else
{
$taxonomy=$slug_to_taxonomy[$slug];
}
$terms_page=get_query_var('paged');
if (empty($terms_page))
{
$terms_page=1;
}
$terms=apply_filters( 'taxonomy-images-get-terms', '',array('having_images'=>false,'taxonomy'=>$taxonomy, 'term_args'=>array('offset'=>($terms_page-1)*$terms_per_page,'number'=>$terms_per_page)) );
if (empty($terms))
{
header("Location: /");exit;
}
$processed_terms=array();
foreach ($terms as $term)
{
if (!empty($term->image_id))
{
$image_src=wp_get_attachment_image_src($term->image_id,'archive-thumbnail');
$image_src=$image_src[0];
}
else
{
$image_src='http://couch.ro/wp-content/uploads/couchie_75.png';
}
$term_posts=get_posts(array('posts_per_page'=>3,'tax_query'=>array(array('taxonomy'=>$taxonomy,'field'=>'slug','terms'=>$term->slug))));
$actual_term_posts=array();
foreach ($term_posts as $post)
{
$actual_term_posts[$post->post_title]=get_permalink($post->id);
}
$processed_terms[]=array(
'name'=>$term->name,
'description'=>$term->description,
'url'=>get_term_link($term),
'image'=>$image_src,
'posts'=>$actual_term_posts,
'count'=>$term->count
);
}
$has_next_page=(isset($processed_terms[$terms_page]));
get_header();
?>
<div class="posts-wrap">
<div class="archive-title_wrap"><h1 class="archive-title"><?php the_title(); ?></h1></div>
<div id="post_list_wrap">
<?php
foreach ($processed_terms as $term)
{
echo "<div class='post post-archive'>
<a href='{$term['url']}' title='{$term['name']}'><img src='{$term['image']}' alt='{$term['name']}'></a>
<div class='rating' style='text-align:right;'>{$term['count']} ".($term['count']==1?'review':'reviewuri')."</div>
<h2 class='index-entry-title'>
<a href='{$term['url']}' title='{$term['name']}'>{$term['name']}</a>
</h2>
<div class='archive-meta entry-meta-index'>
<span>";
$first_term_post=true;
foreach ($term['posts'] as $title=>$link)
{
echo ($first_term_post?'':', ')."<a href='{$link}' title='{$title}'>{$title}</a>";
$first_term_post=false;
}
echo "</span>
</div>
</div>";
}
?>
</div>
<?php if ($terms_page>1 OR $has_next_page) { ?>
<div class="navigation">
<div class="nav-prev left"><?php if ($terms_page>1) echo "<a href='/{$slug}/".($terms_page>2?"page/".($terms_page-1)."/":'')."'>".__('« Previous Page', 'designcrumbs')."</a>"; ?></div>
<div class="nav-next right"><?php if ($has_next_page) echo "<a href='/{$slug}/page/".($terms_page+1)."/'>".__('Next Page »', 'designcrumbs')."</a>" ?></div>
<div class="clear"></div>
</div>
<?php } ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
You can use query_posts function like this
$termname = get_query_var('pagename'); //custom-taxonomy term
query_posts(array(
'post_type' => POST_TYPE,
'showposts' => $limit,
'custom-taxonomy' => $termname // use $term1.','.$term2.','.$term3 to get multiple terms posts
));
see Documentation at Wordpress
To get all Terms under Custom Taxonomy try this you will have full control.
global $wpdb;
$taxonomy = CUSTOM_CAT_TYPE;
$table_prefix = $wpdb->prefix;
$wpcat_id = NULL;
//Fetch category or Term as said
$wpcategories = (array) $wpdb->get_results("
SELECT * FROM {$table_prefix}terms, {$table_prefix}term_taxonomy
WHERE {$table_prefix}terms.term_id = {$table_prefix}term_taxonomy.term_id
AND {$table_prefix}term_taxonomy.taxonomy ='" . $taxonomy . "' and {$table_prefix}term_taxonomy.parent=0 ORDER BY {$table_prefix}terms.name ASC");
$wpcategories = array_values($wpcategories);
foreach ($wpcategories as $wpcat) {
$termid = $wpcat->term_id;
$name = $wpcat->name;
$termprice = $wpcat->term_price;
$tparent = $wpcat->parent;
}

wordpress shortcode problem

Apparently the following simple code breaks the shortcode API of wordpress. When I add this code in the function.php the shortcode API won't work.
This code is normally to add a text at the bottom of each page, any idea why?
function cmstut_basic_promote($content)
{
echo $content;
if(is_single())
{
?>
<div class="promote">
<h2>Enjoy this article?</h2>
<p>If you have enjoyed this article please subscribe to our RSS Feed</p>
</div>
<?php
}
}
add_filter('the_content', 'cmstut_basic_promote');
you must return the content from your filter not echo it - so something like
function cmstut_basic_promote($content) {
if(is_single()) {
return $content . '<div class="promote"><h2>Enjoy this article?</h2> ...';
} else {
return $content;
}
}
would be the way to go

Resources