在 select 字段中获取可用的 WooCommerce 优惠券列表
Get a list of available WooCommerce coupons in a select field
作为我使用 WooCommerce API 创建的插件的一部分,我需要让优惠券显示在 select 字段中。这是相关的代码:
// 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 );
选项数组应该有这样的东西...
'options' => array(
'coupon_1' => __( 'Coupon 1'),
'coupon_2' => __( 'Coupon 2'),
)
..但是 coupon_list()
只是返回一串优惠券名称。我该如何解决这个问题?
你的函数 return 不是一个数组,而是一个字符串
通过使用 implode()
并且您应该使用 $coupon_post->post_name
作为数组键:
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 = []; // Initializing
// Push to array
foreach ( $coupon_posts as $coupon_post ) {
$coupon_codes[$coupon_post->post_name] = $coupon_post->post_title;
}
// Return coupon array
return $coupon_codes;
}
在 WooCommerce 中,您可以使用 woocommerce_form_field()
- ( type' => 'select'
) 创建一个 drop-down 列表(前端) - 对于后端,您可以使用 woocommerce_wp_select()
.
例如,要在 WooCommerce 单个产品页面(前端)上显示此内容,您可以使用:
function action_woocommerce_single_product_summary() {
// Add select field
woocommerce_form_field( 'the_coupon_type', array(
'type' => 'select',
'label' => __( 'Coupon to use', 'woocommerce' ),
'required' => false,
'options' => coupon_list(),
),'' );
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 9 );
作为我使用 WooCommerce API 创建的插件的一部分,我需要让优惠券显示在 select 字段中。这是相关的代码:
// 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 );
选项数组应该有这样的东西...
'options' => array(
'coupon_1' => __( 'Coupon 1'),
'coupon_2' => __( 'Coupon 2'),
)
..但是 coupon_list()
只是返回一串优惠券名称。我该如何解决这个问题?
你的函数 return 不是一个数组,而是一个字符串
通过使用 implode()
并且您应该使用 $coupon_post->post_name
作为数组键:
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 = []; // Initializing
// Push to array
foreach ( $coupon_posts as $coupon_post ) {
$coupon_codes[$coupon_post->post_name] = $coupon_post->post_title;
}
// Return coupon array
return $coupon_codes;
}
在 WooCommerce 中,您可以使用 woocommerce_form_field()
- ( type' => 'select'
) 创建一个 drop-down 列表(前端) - 对于后端,您可以使用 woocommerce_wp_select()
.
例如,要在 WooCommerce 单个产品页面(前端)上显示此内容,您可以使用:
function action_woocommerce_single_product_summary() {
// Add select field
woocommerce_form_field( 'the_coupon_type', array(
'type' => 'select',
'label' => __( 'Coupon to use', 'woocommerce' ),
'required' => false,
'options' => coupon_list(),
),'' );
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 9 );