Skip to content
Advertisement

Creating a List of Available WooCommerce Coupon Codes and Display anywhere Using Shortcode

I am trying to generate a list of available coupons and to display them using a shortcode. I was hoping to generate the list using SQL and not “-1” since that’s heavier on the db from what I understand.

The error I get is this: Notice: Array to string conversion

JavaScript

Advertisement

Answer

There are 2 mistakes in your code: You are trying to display an array with echo and when using a shortcode function the data to display requires to be returned (not echoed).

The following function (shortcode), will display a coma separated string of all available coupon codes using a light SQL Query:

JavaScript

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


USAGE:

1) In the WordPress text editor of a post, a custom post or a page:

JavaScript

2) On a php file or template:

JavaScript

or

JavaScript

With a WP_Query (like in your code):

JavaScript

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Same usage than the first function

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement