Site logo

Archived topic

Add shortcode on the button.

4 replies · Started by Jusung on March 10, 2023

Viewing posts 1–5 of 5

Hello. I am trying to add a shortcode on the button.
I first get the count for processing in woocommerce order.
and I made a shortcode for it to add on the button.

However, when I add it on the button, it appears on the top of the page.

add_shortcode('get_count_processing', 'count_for_processing');
function count_for_processing() {
    $args = array(
        'status' => array('wc-processing'),
        'return' => 'ids',
        'limit' => -1
    );
    $processing_order = wc_get_orders($args);
    echo "Number of processing order = " . count($processing_order);
}

These are the code I added to get the count.
I want to add this inside button, but whereever I put this code, it appears on the top of the page.

Hi there,

a shortcode should return its value and not echo it.
However if you use object buffering then you can overcome that like so:

add_shortcode('get_count_processing', 'count_for_processing');
function count_for_processing() {
    ob_start();
    $args = array(
        'status' => array('wc-processing'),
        'return' => 'ids',
        'limit' => -1
    );
    $processing_order = wc_get_orders($args);
    echo "Number of processing order = " . count($processing_order);
    return ob_get_clean();
}

Thank you!
It works well!

This has been resolved!

This is resolved!

I just added this
'customer_id' => get_current_user_id(),

Glad to hear that

This archived topic is closed to new replies.