创建可用的WooCommerce优惠券代码列表,并使用简码在任何地方显示

问题描述 投票:1回答:1

我正在尝试生成可用优惠券的列表,并使用简码显示它们。我希望使用SQL而不是“ -1”生成列表,因为根据我的理解,这在数据库上比较重。

我得到的错误是这个:Notice: Array to string conversion

add_shortcode('ac', 'coupon_list' );
function coupon_list() {

    // array for coupons, was hoping for a sql query instead but don't know how
    $args = array(
    'posts_per_page'   => -1,
    'orderby'          => 'title',
    'order'            => 'asc',
    'post_type'        => 'shop_coupon',
    'post_status'      => 'publish',
);

    $coupons = get_posts( $args );
        $coupon_names = array();
            foreach ( $coupons as $coupon ) {
        $coupon_name = $coupon->post_title;
        array_push( $coupon_names, $coupon_name );
    }

    // display all available coupons on product page
    echo $coupon_names;
}
php wordpress woocommerce shortcode coupon
1个回答
1
投票

您的代码中有2个错误:您试图使用echo显示一个数组,并且在使用简码功能时,要显示的数据需要返回

© www.soinside.com 2019 - 2024. All rights reserved.