Display sum of variation in stocks of variable product - php

In woocommerce, is it possible to display the sum of total stock of variations in variable product? I've the code from here Get the total stock of all variations from a variable product In Woocommerce
and its working but it displays two stocks infos. for example, I leave the stock in variable product as blank (because I wanted it to be auto generated as the sum of variation.) The default stock display say its out of stock, while the other one which is in the code displays the accurate one(one that highlighted in the picture). please help
I've used this code in the red text stocks.
function show_stock() {global $product;if ( $product->stock ) { // if manage stock is enabled if ( ! $product->managing_stock() && ! $product->is_in_stock() ) echo '';}if ( number_format($product->stock,0,'','') > 0 ) { // if stock is lowecho '<div class="remainingpc" style="text-align:center;"><font color="red"> ' . number_format($product->stock,0,'','') . ' Pcs Left</font></div>';} else {echo '<div class="remaining" style="text-align:center;"><font color="red">Out of Stock</font></div>'; }} add_action('woocommerce_after_shop_loop_item','show_stock', 10);

Try replacing the provided code with:
add_action( 'woocommerce_after_shop_loop_item', 'display_variable_product_stock_quantity', 10 );
function display_variable_product_stock_quantity(){
wc_get_variable_product_stock_quantity( 'echo_html' );
}

Related

WooCommerce Offset Stock levels Based On Custom Field Value Threshold

I need to set a threshold on a per product basis for stock inventory.
Example : If the product has a threshold of 10 and there's 10 or less in stock, the product shows the out of stock text. If the threshold is set at 10 but there's 15 in stock, 5 is shown in stock.
So what i need to do is offset the real inventory levels on a per product basis using a value.
This is the code i'm trying to get working
add_filter( 'woocommerce_get_availability', 'override_get_availability', 10, 2);
function override_get_availability( $availability, $product ) {
$stock_threshold = get_post_meta( $product->get_id(), '_stock_threshold', true );
if ( $product->managing_stock() && ! empty( $stock_threshold ) ) {
$availability['availability'] = $product->get_stock_quantity() - $stock_threshold;
return $availability;
}
}
This code doesn't show the Out of Stock text on the single product page if the inventory is calculated at 0 or less than zero.
Example : When the stock level is set at 2 and the offset is set at 2, it = 0 so should show Out of Stock but doesn't.
I think this gets you some of the way there;
add_filter( 'woocommerce_get_availability', 'override_get_availability', 10, 2);
function override_get_availability( $availability, $product ) {
$stock_threshold = $product->get_low_stock_amount();
$stock_quantity = $product->get_stock_quantity();
if ( $product->managing_stock() && !empty($stock_threshold) ) {
$avail_stock = ($stock_quantity - $stock_threshold);
if ($avail_stock <= 0){
$availability['availability'] = __('Out of Stock', 'woocommerce');
}
return $availability;
}
}
Basically, we're getting the low stock amount (threshold) and subtracting the stock quantity. If it's 0 or less, then set the availability to Out of Stock.
This works without a problem.
However if the product has 15 in stock and the threshold at 10, it still shows 15 as the out of stock is NOT 0.
I think the best approach is a combination of the above, and then shortcode to display the quantity minus the threshold rather than using the code inside WooCommerce.
Maybe someone else can build off what I have posted above as I couldn't get the quantity number to reduce on products NOT out of stock.

Woocommerce hide out of stock variation price from variable price range

I want to hide out of stock variation price from woocommerce variable products price-range
for example i have product that has 2 variation, one of them sold out but in price range it's still showing old price for sold out variation
i want to show price range only for available (in stock) variations
I searched a lot and did't find any solution
Found this code
add_filter( 'woocommerce_get_price_html', 'remove_price_ofs', 10, 2 );
function remove_price_ofs( $price, $product ) {
if ( ! $product->is_in_stock() && (is_product() || is_product_category() || is_shop() )) {$price = '';}
return $price;
}
but it's only hide price from product itself, but in price range it's still showing out of stock variation price
Thanks all in advance for help!

Change the Label on the shop page to display stock quantity

Right now, my shop only displays only 2 badges on my shop page. Special Offer and out of stock. I would also want to include stock quantity below 10 and Allow for back order. I have looked at multiple plugins as well as php code, but nothing seems to work. What I want to do is display "Only {QTY} Is Available" is the stock drops below 10 and if it is 0 then "available on back order". Website is built in WooCommerce and WordPress
I would like these messages to replace the special offer badge if these conditions are met. I have attached a screenshot below:-
Any assistance is greatly appriciated
There are in fact two parts to your question: how to change the stock availability text and how to remove the sale badge, when some criteria on the product stock level is met.
Changing the stock availability text should be easily achieved by hooking in the woocommerce_get_availability filter. Get the product stock quantity and amend the text accordingly. E.g.:
add_filter( 'woocommerce_get_availability', 'custom_override_get_availability', 10, 2 );
function custom_override_get_availability( $availability, $_product ) {
$stock_quantity = $_product->get_stock_quantity();
if ( 0 === $stock_quantity ) {
$availability['availability'] = 'Available on backorder';
} elseif ( $stock_quantity < 10 ) {
$availability['availability'] = sprintf( 'Only %s available', $stock_quantity );
} else {
$availability['availability'] = sprintf( '%s left in stock', $stock_quantity );
}
return $availability;
}
As for removing the sale badge, I think the plugin you're using provides templates that can be overridden. Have a look in the Views folder, you should find the sale-flash.php templates. You can then override those templates in your theme and apply a similar logic, as you have access to the product. E.g. this shows the sale badge only when the product is on sale and the stock quantity is greater than or equal to 10:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $post, $product;
?>
<?php if ( $product->is_on_sale() && $product->get_stock_quantity() >= 10 ) : ?>
<?php echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale wdr-onsale">' . esc_html__( 'Sale!', 'woocommerce' ) . '</span>', $post, $product ); ?>
<?php endif;
Although I'm not really sure if you want to remove the sale badge when the product stock quantity is less than 10.

Show message based on cart total before order review table in WooCommerce checkout

On my WooCommerce checkout page, I am trying to show a notification before the order review section when the total of the cart equals €0.01
So far, I have placed the following in the functions.php file of my child theme:
add_action('woocommerce_checkout_before_order_review', 'test_funtion');
function test_funtion(){
?>
<p>Notice goes here</p>
<?php
}
How can I modify these lines such that this notice is only shown if the order total equals € 0.01?
You can use WC_Cart::get_cart_contents_total(); - Gets cart total. This is the total of items in the cart, but after discounts. Subtotal is before discounts.
So you get:
function action_woocommerce_checkout_before_order_review () {
// Get cart total
$cart_total = WC()->cart->get_cart_contents_total();
// Compare
if ( $cart_total == 0.01 ) {
echo '<p>' . __( 'My message', 'woocommerce' ) . '</p>';
}
}
add_action( 'woocommerce_checkout_before_order_review', 'action_woocommerce_checkout_before_order_review', 10, 0 );

Woocommerce - Display Lowest Price on Category (Archive) Page Only

I'm trying to display the lowest price instead of the price range on the category pages while continuing to display the price range on the individual product pages.
I found this answer from someone else's question:
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
$price = '';
if ( !$product->min_variation_price || $product->min_variation_price !== $product->max_variation_price ) {
$price .= '<span class="from">' . _x('From', 'min_price', 'woocommerce') . ' </span>';
$price .= woocommerce_price($product->get_price());
}
return $price;
}
And it works, but when I put the code in functions.php, it applies to the whole site. I only want it to apply to the category pages.
Also, when I use this code, items with only one price end up with no price being displayed.
What can I do?
Here's a screenshot of what I mean:
Screenshot of current category page
If I got you right then you just need to display the minimal price whenever there's a range of prices. So basically you need to change the $price's HTML only if product's minimal price is set (if it's not set then the product has a single price and no change is needed). I also included the check for whether it's a category page. Here's the code:
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
if ( ! empty($product->min_variation_price) && is_product_category() ) {
$price = '<span class="from">' . _x('From', 'min_price', 'woocommerce') . ' </span>';
$price .= woocommerce_price($product->get_price());
}
return $price;
}

Categories