Display always stock status without quantity in WooCommerce - wordpress

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' );

Related

WooCommerce - Show regular price striked before the sale price in the add to cart button?

I would like to show the regular price before the discounted price in the add to cart button on the single product pages only.
Here's the code snippet I added to the functions.php file of my child theme:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' );
function woocommerce_custom_single_add_to_cart_text() {
global $product;
$regular_price = woocommerce_price( $_product->get_regular_price() );
$sale_price = woocommerce_price( $_product->get_sale_price() );
return __( 'Add to cart' . $regular_price . $sale_price, 'woocommerce' );
}
This code is incorrect and crashes the website but I don't understand why. Can anyone help me fix it?
The code crashes because you use a variable $_product which is not defined inside your hook. Please try this code instead:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'filter_woocommerce_product_single_add_to_cart_text' );
function filter_woocommerce_product_single_add_to_cart_text(): string {
global $product;
$regular_price = wc_price( $product->get_regular_price() );
$sale_price = wc_price( $product->get_sale_price() );
return __( 'Add to cart', 'woocommerce' ) . $regular_price . $sale_price;
}
I've fixed your variable name $_product to $product (as defined inside a global) and moved your price to text assignment outside the translation function. Also, I've preplaced the deprecated function woocommerce_price with wc_price.
This way, your code works but don't display the price correctly since this filter filters only the text as a string.
I think you need to check for a different hook to achieve your goal.

Make brand name linkable under product title in Woocommerce

I am trying to place a clickable link for brands under the product title on Woocommerce single product pages.
I have successfully been able to put the brand under the title, but can't make it a link to the brand category page.
Any help?
I'm using the WooCommerce Brands plugin.
And here is the code I'm using:
add_action( 'woocommerce_single_product_summary', 'wc_brands_add_brand_name', 8 );
function wc_brands_add_brand_name() {
global $product;
$brands = implode(', ', wp_get_post_terms($product->get_id(), 'product_brand', ['fields' => 'names']));
echo "<p>Brand: " . '' . $brands . '' . "</p>";
}
get_the_term_list is the right function to use in a situation where you want to show links of terms of a post.
In get_the_term_list you can set before, after & separator, so you don't even have to make an if check for show/hide the string.
Here is the code:
/**
* Display product brands
*/
function vh_wc_display_brand_links() {
global $product;
echo get_the_term_list( $product->get_id(), 'product_brand', '<p class="product-brands">' . __( 'Brands:', 'text-domain' ) . ' ', ', ', '</p>' );
}
add_action( 'woocommerce_single_product_summary', 'vh_wc_display_brand_links', 8 );

Change Add to Cart Button When ACF Has Value on WooCommerce Product Archive

I'm trying to change the Add to Cart button when a product has an ACF (Advanced Custom Fields) value on WooCommerce product archive. For instance if my field named mix_and_match_enabled is switched on, I need the button to read "View options" and link to the product page instead of the default "Add to cart" button.
Here's some sample code of how I believe this could be done but it's likely wrong.
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){
$mix_and_match = get_post_meta($post_id, 'mix_and_match_enabled', true);
if ( $mix_and_match ){
$button = '' . __( "View options", "woocommerce" ) . '';
}
return $button;
}
Any help would be appreciated. Thanks!
I believe I've solved my issue. I've pasted the solution below. Please feel free to improve on this code.
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){
$mix_and_match = get_post_meta(get_the_ID(), 'mix_and_match_enabled', true);
if ( $mix_and_match == 1 ){
$button = '' . __( "View options", "woocommerce" ) . '';
}
return $button;
}

Woocommerce show price incl and excl tax in product catalog

I want to show the product price including and exclunding tax under each product in the catalog page of my Woocommerce shop.
Its already working, but it is not showing anything for variable products where I have only one variation. On single products it is also working.
Also I do get the notification:
Notice: WC_Product::get_price_including_tax ist seit Version 3.0 veraltet! Benutze stattdessen wc_get_price_including_tax.
Notice: WC_Product::get_price_excluding_tax ist seit Version 3.0 veraltet! Benutze stattdessen wc_get_price_excluding_tax.
But if I do so, it is not working anymore at all.
add_action( 'woocommerce_after_shop_loop_item_title', 'preise_notice', 10 );
function preise_notice() {
global $product;
if ( $price_html_incl_tax = $product->get_price_including_tax() )
if ( $price_html_excl_tax = $product->get_price_excluding_tax() ) {
echo '<div class="product-prices-excl-vat"><a>ab ' . wc_price($price_html_excl_tax) . ' netto</a></div>';
echo '<div class="product-prices-incl-vat"><a>(' . wc_price($price_html_incl_tax) . ' inkl. 19% MwSt.)</a></div>';
}
}
The wc_get_price_including_tax and wc_get_price_excluding_tax functions expect $product as an argument. So you will have to pass it like this:
wc_get_price_including_tax( $product )
Also it seems like a good idea to get the product's tax rate instead of hard coding it in. Maybe in the future you will have products that do not have a 19% tax rate. I also included the currency argument to the wc_price function so the price will be shown in the shop's currency.
You can use the following snippet that will get the product's tax rate and prints the prices including and excluding tax:
add_action( 'woocommerce_after_shop_loop_item_title', 'add_product_price_incl_and_excl_tax', 10 );
function add_product_price_incl_and_excl_tax() {
global $product;
$tax_rate = '';
$tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
//Check the product tax rate
if ( !empty( $tax_rates ) ) {
$tax_rate = reset($tax_rates);
$tax_rate = sprintf( ' inkl. %.0f%% MwSt.', $tax_rate['rate'] );
}
//Print product prices including tax and tax percentage, and excluding tax
printf( '<div class="product-prices-excl-vat">ab %s netto</div>', wc_price( wc_get_price_excluding_tax( $product ), array( 'currency' => get_woocommerce_currency() ) ) );
printf( '<div class="product-prices-incl-vat">%s%s</div>', wc_price( wc_get_price_including_tax( $product ), array( 'currency' => get_woocommerce_currency() ) ), $tax_rate );
}

Automatically add woocommerce product short description on creation

I want to add a product short description by default whenever is the new product is being created. All the products will have the same short description, so there is no point keep copying and pasting it. So it should just be there when I click on the add a new product.
I would appreciate any help.
add_filter( 'woocommerce_short_description',
'single_product_short_description', 10, 1 );
function single_product_short_description( $post_excerpt )
{
global $product;
if ( is_single( $product->id ) )
$post_excerpt = '<div class="product-message"><p>' . __( "Article only
available in the store.", "woocommerce" ) . '</p></div>' . $post_excerpt;
return $post_excerpt;
}
I found the above code but couldn't get it to work :(
Thank you.
Regards,
Emre.
Add this code inside your themes function.php
Change the short description content as per your need - "Here goes your short desc."
add_filter( 'wp_insert_post_data' , 'cdx_add_product_short_desc' , '99', 1 );
function cdx_add_product_short_desc( $data )
{
//only for product post type
if($data['post_type'] == 'product' ) {
//only if short description is not present
if( '' == trim($data['post_excerpt']) ):
$short_desc = 'Here goes your short desc.';
$data['post_excerpt'] = $short_desc ;
endif;
}
// Returns the modified data.
return $data;
}
This will work to automatically override anything put into a product's short description field on the front-end only. It will not add the text to the backend field itself, which is good because it keeps it globalized if you need to change it later.
add_filter( 'woocommerce_short_description', 'filter_woocommerce_short_description', 10, 1 );
function filter_woocommerce_short_description( $post_excerpt ) {
$post_excerpt = '<div class="product-message"><p>' . __( "Article only available in the store.", "woocommerce" ) . '</p></div>';
return $post_excerpt;
}

Resources