Woocommerce - Hide price when empty - wordpress

I use woocommerce.
In my product page, I have changed the code in price.php to show both price inclusive and exclusive taxes. But for some products, there is no price and they have to contact us to get the price. So when the price is empty, I don't want to show the text with the prices.
I have combined some code that I found, with the code I already had, but it doesn't really work, I keep getting the "excl. BTW €0,00 incl. BTW" text, even when the price of the product is empty.
<p class="price"><?php if( !empty(woocommerce_price ($product-> get_price_excluding_tax ()))) { echo $product->get_price_html() . ' excl. BTW<br />' . wc_price( wc_get_price_including_tax( $product ) ) . ' incl. BTW'; } ?></p>
Thanks in advance!

Related

Show prices including tax on product page

I am using woocoommerce V3.0.9, and have tax settings enabled. I set Prices entered with tax as Yes, I will enter prices inclusive of tax and Display prices in the shop as Including Tax and Additional tax classes as Reduced Rate Zero Rate.
Also, while adding product i added product prices including tax. But on product detail page the prices are showing without tax. For example i added product price 135.90 while adding product including Tax and on product detail page its showing me price 123.55 excluding Tax but it should show 135.90 as i have set the setting to show prices including tax.
On checkout page, i am getting product price 123.55 + 12.35 Tax = 135.90 as product total price which is working fine.
But i want to show the actual price including tax on product detail page so that customer knows the original price before adding product to cart.
Can anyone help me how i can get this working.
Thanks in advance.
Before check that your WooCommerce Tax general settings match with your needs.
As suggested, you need to copy from woocommerce the templates folder inside your active child theme or theme. Then rename it woocommerce. In this woocommerce templates folder you will find inside single-product subfolder the price.php template to edit related to pricing display in single product pages.
In price.php just after:
global $product;
Replace the code with:
?>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<?php
$simple_product_price = $product->get_price_html(); // price without VAT
$simple_product_total = $product->get_price_including_tax(); // price with included VAT
$simple_product_vat = $simple_product_total - $simple_product_price; // VAT price amount
?>
<p class="price"><?php echo $simple_product_price; /* without VAT */ ?></p> (formatted)
<p class="price-vat"><?php echo $simple_product_vat; /* VAT */ ?></p>
<p class="price-and-vat"><?php echo $simple_product_total; /* With VAT */ ?></p>
<meta itemprop="price" content="<?php echo esc_attr( $product->get_price() ); ?>" />
<meta itemprop="priceCurrency" content="<?php echo esc_attr( get_woocommerce_currency() ); ?>" />
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
</div>
Because the additional prices are unformatted, you may need to mix some other elements with this additionals prices using some woocommerce php functions like:
get_price_suffix( ) // Get the suffix to display after prices > 0.
$currency = esc_attr( get_woocommerce_currency( ) ) // Get the currency code.
get_woocommerce_currency_symbol( $currency ) // Get the currency symbol.
get_tax_class( ) // Returns the tax class.
get_tax_status( ) // Returns the tax status.
Woocommerce Reference Link: https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html

Display always stock status without quantity in WooCommerce

I would like to display the product stock status, without showing the remaining quantity.
Right now, the standard WooCommerce stock display, as well as the plugins I found so far (WooCommerce Booster, etc), display either the quantity, or a "package deal" showing the quantity AND the phrase "in stock".
In other words,
I have: "5 in stock"
I want to show: "In Stock"
Is this possible? If so, how?
Resolved.
Woocommerce -> Settings -> Products -> Inventory -> Stock Display Format.
...can't believe I didn't see it before.
If You have problem with 'In Stock' custom message try this code below:
add_filter( 'woocommerce_get_availability', 'custom_override_get_availability', 10, 2);
// The hook in function $availability is passed via the filter!
function custom_override_get_availability( $availability, $_product ) {
if ( $_product->is_in_stock() ) $availability['availability'] = __('In Stock', 'woocommerce');
return $availability;
}
or this
//* Add stock status to archive pages
function envy_stock_catalog() {
global $product;
if ( $product->is_in_stock() ) {
echo '<div class="stock" >' . $product->get_stock_quantity() . __( ' in stock', 'envy' ) . '</div>';
} else {
echo '<div class="out-of-stock" >' . __( 'out of stock', 'envy' ) . '</div>';
}
}
add_action( 'woocommerce_after_shop_loop_item_title', 'envy_stock_catalog' );

How to display a low stock message for a WooCommerce variable product if backorders are allowed?

I've created a custom message to display if one of my variable products stock is low. The problem i'm running into is if I enable backorders it causes the message to be displayed regardless of the quantity left. My $max_qty variable also does not appear in my custom message.
Any help would be greatly appreciated!
foreach( $available_variations as $i => $variation ) {
// Get max qty that user can purchase
$max_qty = $variation['max_qty'];
//check if variation has stock or not
if ( $variation['is_in_stock'] && $max_qty < 5) {
// Prepare availability html for stock available instance
$availability_html = '<p class="stock in-stock" style="font-size: 1em">Hurry only ' . $max_qty . ' spots left on this day and time!' . '</p>';
}
$available_variations[$i]['availability_html'] = $availability_html;
}

WooCommerce - Getting total products count in the cart - not their quantity

I have total cart quantity but I need count of products available in the cart. I donot want to show total quantity but want to show total products/orders in the cart.
Please help!
i had same issue in client project # jivith.com
But i resolved ...
Use in minicart / cart function replace total products count in the cart - not their quantity items
$_cartQty = count( WC()->cart->get_cart() );
**or** use sizeof (WC()->cart->get_cart());
i getting the total unique total products count in the cart instead of item of their quantity...
My demo code:
<span class="cart-items"><?php echo ($minicart_type == 'minicart-inline')
? '<span class="mobile-hide">' . sprintf( _n( '%d item', '%d items', $_cartQty, 'porto' ), $_cartQty ) . '</span><span class="mobile-show">' . $_cartQty . '</span>'
: (($_cartQty > 0) ? $_cartQty : '0'); ?></span>
You can get the total number of unique product is using WC()->cart->cart_contents. This contains an array of cart items. You can use array_unique() function to avoid repetition of the ids. So finally you can use array_count to get the count of unique products.
I things Following code will help you. I had a issue i want to show total product counts not their quantity in the cart option.
If you change on class-wc-cart.php file just change php function array_sum() to count() it will work.
public function get_cart_contents_count() {
return apply_filters( 'woocommerce_cart_contents_count', count( wp_list_pluck( $this->get_cart(), 'quantity' )) );
}

Include the current product category name at the top of the single product page in woocommerce?

Just wanted to ask if anyone here can tell me how to include the current product category name at the top of the single product page in Woocommerce.
I'm going to want to place it above the breadcrumb trail at the top of my product page. I've included a link to an example of what I'm looking for here: http://www.espguitars.com/guitars/signature/kirk.html -
Notice at the top right-hand corner of the page you'll see the current category name, in this case, the category name is Guitars. I realize that the site example I've given is not a wordpress site, but I feel certain there's a way to do this with woocommerce.
If you check the meta.php file in the "templates/single-product" folder of the WooCommerce plugin, you'll see how the WooCommerce plugin displays the product categories of the current product as product meta information.
<?php
...
global $post, $product;
?>
<div class="product_meta">
...
<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
...
</div>
Please copy the code to your theme and modify it accordingly.

Resources