I have three separate woocommerce snippets where each one have one function. I'm trying to combine them together in one script but can't seems to return more than one value.
function display_woocommerce_order_count2( $atts, $content = null ) {
$args = shortcode_atts( array(
'status' => 'completed',
), $atts );
$statuses = array_map( 'trim', explode( ',', $args['status'] ) );
$order_count = 0;
foreach ( $statuses as $status ) {
// if we didn't get a wc- prefix, add one
if ( 0 !== strpos( $status, 'wc-' ) ) {
$status = 'wc-' . $status;
}
$order_count += wp_count_posts( 'shop_order' )->$status;
}
ob_start();
return '<span style="color:#fff;text-align:center;font-size:12px">Deals:' .
$order_count;
$user->total;
return ob_get_clean();
}
add_shortcode( 'wc_order_count3', 'display_woocommerce_order_count2' );
function get_instock_products_count(){
global $wpdb;
// The SQL query
$result = $wpdb->get_col( "
SELECT COUNT(p.ID)
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE '%product%'
AND p.post_status LIKE 'publish'
AND pm.meta_key LIKE '_stock_status'
AND pm.meta_value LIKE 'instock'
" );
return '<span style="color:#fff;text-align:center;font-size:12px">Proposals
Left: ' . reset($result);
}
add_shortcode('fp7', 'get_instock_products_count');
function new_proposals2(){
global $wpdb;
// 24 hours ago
$is_24h_ago = date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s")." -1day"));
// The SQL query
$result = $wpdb->get_col( "
SELECT COUNT(p.ID)
FROM {$wpdb->prefix}posts as p
WHERE p.post_type LIKE '%product%'
AND p.post_status LIKE 'publish'
AND p.post_date > '$is_24h_ago'
" );
return '<span style="color:#fff;text-align:center;font-size:12px">New
Proposals: ' . reset($result);
}
add_shortcode( 'new_proposals', 'new_proposals2' );
i tried combining all scripts and put function after one another and tried to return those 3 values at the end of the 3 functions. But only the first one is returned.
or return value at the end of each function, no luck.
My goal is to have something similar to:
Proposals: Taken(x) | New(x) | Left(x)
To merge that 3 shortcodes in one is very easy and can be done this way:
This should work
USAGE:
[order_multi_count]
or[order_multi_count status="processing"]
While accepted answer is perfectly valid one here is more performant and scalable code.
The issue with your code is that for each and every page load you are fetching & calculating same non-changing data.
This is an overhead which won't affect the site for few thousand visitors but when the products, orders or concurrent traffic goes up above code will start becoming a bottleneck.
In this code I have extensively used Transient API (caching) and instead of updating counts on each page load it will use cached counts. Best part is that it will automatically update the cached counts when any of the data used in each counts changes.
For example, as soon as a product is ordered (or order cancelled) the code will update Taken and Left.
This code is fully tested
Main Shortcode
Utility Functions
Usage
[wp1707_proposal_stats]
or[wp1707_proposal_stats status="processing,on-hold,completed"]
You can style the output by using
.proposal-stats
class in your CSS.