Search code examples
wordpresswoocommercecode-snippetscoupon

shortcode to display number of products sold by coupon code


I have coupon "coupon-test"

I have this snippets to show the number of usage for the coupons

function simple_function_1() {
    
    $coupon_code = 'coupon-test';
    global $woocommerce;
    $coupon_data = new WC_Coupon($coupon_code);
    echo ($coupon_data->usage_count);// return number of remaining coupons
}
add_shortcode( 'own_shortcode1', 'simple_function_1' );

I want to make shortcode to display number of usage for coupon and number of products sold by coupon


Solution

  • Output from shortcode functions is presented to WordPress's page construction code by doing return $theResult;, not via echo $theResult;. Your code uses echo, though.