As part of a plugin I am creating with the WooCommerce API, I need to get the coupons to show in the select field. Here is the relevant piece of code:
// Get coupons
function coupon_list() {
$coupon_posts = get_posts( array(
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'asc',
'post_type' => 'shop_coupon',
'post_status' => 'publish',
) );
$coupon_codes = [];
foreach( $coupon_posts as $coupon_post) {
$coupon_codes[] = $coupon_post->post_name;
}
return implode($coupon_codes) ;
}
$settings = array(
'coupon_to_use' => array(
'name' => __( 'Coupon to use'),
'type' => 'select',
'default' => '',
'desc' => __( 'Use this.'),
'desc_tip' => true,
'id' => 'the_coupon_type',
'options' => array(
coupon_list(), // This is where I am stuck
)
)
);
return apply_filters( 'the_coupon_settings', $settings );
The options array should have something like this...
'options' => array(
'coupon_1' => __( 'Coupon 1'),
'coupon_2' => __( 'Coupon 2'),
)
..but coupon_list()
is just returning a string of coupon names. How can I fix this?