Reputation: 87
I would like to limit the display of stocks on my product pages (only on the frontend) in order to limit the stocks that are too high.
My question is similar to this article: Set a max displayed available stock quantity on Woocommerce archives for variable products
In it, the code below displays something similar but has several drawbacks:
add_action( 'woocommerce_after_shop_loop_item', 'wc_loop_get_product_stock_availability_text', 10 );
function wc_loop_get_product_stock_availability_text() {
global $wpdb, $product;
$max_stock_qty = 50; // Maximum Number of Available Stock qty
// For variable products
if( $product->is_type('variable') ) {
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish' AND p.post_parent = '".get_the_id()."'
AND pm.meta_key = '_stock' AND pm.meta_value IS NOT NULL
");
if ( $stock_quantity > 0 ) {
// Here we limit the sock quantity display to 50 when it's up to 50
$stock_quantity = $stock_quantity >= $max_stock_qty ? $max_stock_qty : $stock_quantity;
echo '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
echo '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
return;
}
}
// Other products types
else {
echo wc_get_stock_html( $product );
}
}
The returned value should replace the stock displayed on the product pages taking into account these conditions:
I am stuck and I don't know if I am going in the right direction, Thanks a lot in advance!
Upvotes: 1
Views: 1422
Reputation: 5639
You could filter the woocommerce_get_availability_text
and check the stock quantity.
add_filter( 'woocommerce_get_availability_text', 'custom_availability_text', 10, 2 );
function custom_availability_text( $availability, $product ) {
if ( ! $product->is_in_stock() ) {
$availability = __( 'Out of stock', 'woocommerce' );
} elseif ( $product->managing_stock() && $product->is_on_backorder( 1 ) ) {
$availability = $product->backorders_require_notification() ? __( 'Available on backorder', 'woocommerce' ) : '';
} elseif ( ! $product->managing_stock() && $product->is_on_backorder( 1 ) ) {
$availability = __( 'Available on backorder', 'woocommerce' );
} elseif ( $product->managing_stock() ) {
$stock_amount = $product->get_stock_quantity();
// Check if Stock is less than 50.
if ( 50 > $stock_amount ) {
$availability = wc_format_stock_for_display( $product );
} else {
if ( false === ( $custom_availability = get_transient( 'custom_availability_text_' . $product->get_id() ) ) ) {
$random = wp_rand( 50, 100 );
$availability = sprintf( __( '%s in stock', 'woocommerce' ), wc_format_stock_quantity_for_display( $random, $product ) );
// Store for 6 hours. YMMV.
set_transient( 'custom_availability_text_' . $product->get_id(), $availability, 6 * HOUR_IN_SECONDS );
} else {
return $custom_availability;
}
}
} else {
$availability = '';
}
return $availability;
}
I've updated the answer to include a 6 hour cache.
Upvotes: 1