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:
function order_multi_count( $atts, $content = null ) {
global $wpdb;
$args = shortcode_atts( array(
'status' => 'completed',
), $atts );
## ---- ---- ---- ---- ---- ---- TAKEN ---- ---- ---- ---- ---- ---- ---- ##
$statuses = array_map( 'trim', explode( ',', $args['status'] ) );
$taken = 0;
foreach ( $statuses as $status ) {
// if we didn't get a wc- prefix, add one
if ( 0 !== strpos( $status, 'wc-' ) ) {
$status = 'wc-' . $status;
}
$taken += wp_count_posts( 'shop_order' )->$status;
}
## ---- ---- ---- ---- ---- ---- LEFT ---- ---- ---- ---- ---- ---- ---- ##
// 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'
" );
$left = reset($result);
## ---- ---- ---- ---- ---- ---- NEW ---- ---- ---- ---- ---- ---- ---- ##
// 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
$result2 = $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'
" );
$new = reset($result2);
## ---- ---- ---- ---- ---- RETURNING VALUE ---- ---- ---- ---- ---- ---- ##
$style = 'style="color:#fff;text-align:center;font-size:12px"';
return "<span $style><strong>Proposals:</strong> Taken ($taken) | New ($new) | Left ($left)</span>";
}
add_shortcode( 'order_multi_count', 'order_multi_count' );
This should work
USAGE: [order_multi_count]
or [order_multi_count status="processing"]