Woocommerce hide out of stock variation price from variable price range - php

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!

Related

Change number of decimals in Woocommerce checkout and payment page

My Woocommerce website doesn't display decimals as I use a currency converter plugin and don't want cents to show.
I wanted to have the possibility to create custom orders (in order to send payment links to customers) with cents. I tried to create an exception for my custom product category.
Here is what I tried:
add_filter( 'wc_get_price_decimals', 'custom_price_decimals', 10, 1 );
function custom_price_decimals($decimals) {
global $product;
if (is_a($product, 'WC_Product') && has_term('voyage-sur-mesure', 'product_cat', $product->get_id()) && is_cart() || is_checkout() || is_account_page()) {
$decimals = 2;
}
return $decimals;
}
The result is that my cents are showing on the payment page but it is rounded on the total line. Yet, I need the cents to be paid.
Product × 1 50,23€
Subtotal : 50,23€
Total : 50,00€
Could someone please help?

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.

Show regular price along with discounted price in WooCommerce when assigning price

I am using a function to put my entire store on sale for 15% off. It works great but it changes all the pricing the the loops and the product page just to the new 15% off price. How can I set the sale price but also show the regular price too so I can then cross out the regular price with CSS.
add_filter('woocommerce_product_get_price', 'assign_tier_pricing', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'assign_tier_pricing', 90, 2 );
function assign_tier_pricing( $price, $product ) {
if ( $product->is_type('simple') ) {
$price *= 0.85; // Set all prices for simple products to 50% off.
}
return $price;
}
I would like to also return the regular price in a span so I can style it and cross it out so customers can see the normal price along with the sale price
So also return <span class="regular-price">Display regular price</span> then the new sale price right next to it.
Use is_on_sale() to check https://woocommerce.github.io/code-reference/classes/WC-Product.html#method_is_on_sale
if ( $product->is_type('simple') && is_on_sale () )
or
if ( $product->is_type('simple') && ! is_on_sale () )
For ! Not on sale

Display "excl. VAT" in Woocommerce product-subtotal

in Woocommerce when you choose to display prices during checkout inclusive VAT, Woocommerce then adds a .tax_label after the td.product-subtotal, and a .includes_tax after the total. For customers from countries with 0% VAT, nothing is displayed, I need the tax_label and includes_tax to be displayed though as “excl. VAT”. So in the td.product-subtotal cell there should be the subtotal price with the added "excl. VAT" and in the order-total.td there should be the total price also with the added "excl.VAT".
add_filter( 'woocommerce_cart_hide_zero_taxes', '__return_false' );
Above filter does not do what I want because it does not add "excl.VAT" to the td.product-subtotal.
If you're talking about the cart subtotal there is a filter for that woocommerce_cart_subtotal.
<?php
add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );
function filter_woocommerce_cart_subtotal( $cart_subtotal, $compound, $instance ) {
if( ! empty( $cart_subtotal ) ) {
return $cart_subtotal . ' - excl. VAT';
};
}; ?>
Untested but should be working.

Force specific woocommerce variation price instead of from

I have a woocommerce subscription product with two variations (Pay once and Pay over 12 months) in my shop/product pages the displayed price is the one of the variation (Pay over 12 months which is for ex. "From $19.9/month"). I want to show the price of the other variation (Pay once) which is the full price of the product. How can i do that?
I've tried this but it's not working
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
$price = '';
$price .= woocommerce_price($product->max_variation_price);
return $price;
}
FYI : i have other variable products without subscription, i just want to do that only for the subscription product.

Categories