我在WooCommerce库存选项卡中添加了一个下拉列表,用户可以在其中选择要在前端显示的消息。
在函数中。php:
// Display Fields in Back End
add_action( \'woocommerce_product_options_inventory_product_data\', \'woo_add_custom_status_fields\' );
function woo_add_custom_status_fields() {
global $woocommerce, $post;
// Add Select to WooCommerce Inventory Tab in Back End
woocommerce_wp_select(
array(
\'id\' => \'productstatus_select\',
\'label\' => __( \'Product Status\', \'woocommerce\' ),
\'options\' => array(
\'Message one\' => __( \'The message\', \'woocommerce\' ),
\'Message two\' => __( \'The message\', \'woocommerce\' ),
\'Message three\' => __( \'The message\', \'woocommerce\' ),
)
)
);
}
// Save Fields
add_action( \'woocommerce_process_product_meta\', \'woo_add_custom_status_fields_save\' );
function woo_add_custom_status_fields_save( $post_id ){
$woocommerce_select = $_POST[\'productstatus_select\'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, \'productstatus_select\', esc_attr( $woocommerce_select ) );
}
在woocommerce/single product/meta中使用以下代码检索所选值。php <?php
// Display Custom Field Value in Front End
echo \'<p class="productstatus">\' . get_post_meta( $post->ID, \'productstatus_select\', true ) .\'</p>\';
?>
现在,我想知道是否有可能将不同的类分配给不同的选择,以便我可以对输出的代码进行样式化。例如,在前端将消息一设为红色,消息二设为绿色,等等。我不确定这些选项是否应该是数组,或者我是否需要一个额外的函数来实现这一点。非常感谢您的任何建议。