Reputation: 363
I have this specific requirement regarding woocommerce orders. So what i am trying to achieve is, As soon as the count of total number of orders in the woocommerce hits a specific number (let's say 200) then i don't want to accept anymore order and i want to display "out of stock" message or any other custom message (basically i don't want to let the customer buy anything when that condition becomes true) on every product that i have in my inventory. I want this logic to work with variable products as well.
I was able to get the count of orders but i'm stuck now. This is what i have tried so far.
function display_woocommerce_order_count( $atts, $content = null ) {
$args = shortcode_atts( array(
'status' => 'processing',
), $atts );
$statuses = array_map( 'trim', explode( ',', $args['status'] ) );
$order_count = 0;
foreach ( $statuses as $status ) {
if ( 0 !== strpos( $status, 'wc-' ) ) {
$status = 'wc-' . $status;
}
$order_count += wp_count_posts( 'shop_order' )->$status;
}
echo number_format( $order_count );
if($order_count == 200){
//Then make all products go out of stock automatically
//Not able to figure out this part
}
}
please guide me how to go forward from here.
Upvotes: 1
Views: 733
Reputation: 254483
You can't make that in your shortcode directly… Also your function to get orders status count is a bit heavy. Instead you can use the following custom function that uses a very light SQL query to get the orders count.
Another set of hooked functions will set all products (even the product variations of the variable products) to "Out of stock" status, when the order count will reach 200.
Finally I have changed your shortcode function that will display the order count.
You can set the order statuses as you want, in the function that counts orders, in your shortcode too.
Instead use the following:
// Utility function to get orders count based on orders statuses
// Default statuses are processing + completed (and you can change them as you like)
function get_orders_count_from_statuses( $statuses = 'processing, completed' ){
global $wpdb;
// Filtering order statuses
$statuses = "wc-" . str_replace(array("wc-",", ",","), array("",",","','wc-"), $statuses );
// return the query
return (int) $wpdb->get_var("SELECT count(ID) FROM {$wpdb->prefix}posts
WHERE post_status IN ('$statuses') AND `post_type` LIKE 'shop_order'");
}
// Set conditionally product status based on order count limit
add_filter( 'woocommerce_product_get_stock_status', 'conditional_product_status', 10, 2 );
add_filter( 'woocommerce_product_variation_get_stock_status', 'conditional_product_status', 10, 2 );
function conditional_product_status( $stock_status, $product ){
if( get_orders_count_from_statuses() >= 200 ){
$stock_status = 'outofstock';
}
return $stock_status;
}
// Your shortcode function that will display the count (if needed)
add_shortcode( 'order_count', 'display_woocommerce_order_count' );
function display_woocommerce_order_count( $atts ) {
$atts = shortcode_atts( array(
'statuses' => 'processing,completed',
), $atts, 'order_count' );
$order_count = get_orders_count_from_statuses( $atts['statuses'] );
return number_format( $order_count ); // Always use return (not echo for a shortcode)
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
Upvotes: 2