Php woocommerce定制产品领域
我正在尝试使用woocommerce插件向产品管理屏幕添加一个自定义字段,这样我就可以有一个下拉菜单来选择new或used作为产品的条件 我得到了下拉列表,显示在管理屏幕上,但它不会显示新的或在产品前端使用 我在functions.php中添加了以下代码:Php woocommerce定制产品领域,php,wordpress,woocommerce,product,custom-fields,Php,Wordpress,Woocommerce,Product,Custom Fields,我正在尝试使用woocommerce插件向产品管理屏幕添加一个自定义字段,这样我就可以有一个下拉菜单来选择new或used作为产品的条件 我得到了下拉列表,显示在管理屏幕上,但它不会显示新的或在产品前端使用 我在functions.php中添加了以下代码: // Select woocommerce_wp_select( array( ‘id’ => ‘_conditionselect’, ‘label’ => __( ‘Condition’, ‘woocommerce’ ), ‘
// Select
woocommerce_wp_select( array(
‘id’ => ‘_conditionselect’,
‘label’ => __( ‘Condition’, ‘woocommerce’ ),
‘options’ => array(
‘one’ => __( ‘New’, ‘woocommerce’ ),
‘two’ => __( ‘Used’, ‘woocommerce’ ),
)
)
);
}
function woo_add_custom_general_fields_save( $post_id ){
// Select
$woocommerce_select = $_POST['_conditionselect'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, ‘_conditionselect’, esc_attr( $woocommerce_select ) );
}
我将此添加到short description.php:<?php _e( 'Condition: ', ‘woocommerce’ ); ?>
<?php
echo get_post_meta( get_the_ID(), ‘_conditionselect’, true );
?>
因此,我将“two”改为“used”,但它在前端仍然显示“two”。在阅读了您编写的代码之后。我注意到“get_post_meta”的语法在当前上下文中是正确的。即使在您将“二”更改为“已使用”后,它仍然在前端显示“二”的原因是您在进行这些更改后没有更新该产品。这导致显示以前的自定义字段值 woocommerce_wp_select的正确语法为
// Stock status
woocommerce_wp_select( array( 'id' => '_stock_status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
'instock' => __( 'In stock', 'woocommerce' ),
'outofstock' => __( 'Out of stock', 'woocommerce' )
), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );
“instock”和“outofstock”是存储在db中的选项值,“in stock”和“Out of stock”显示在前端
// Stock status
woocommerce_wp_select( array( 'id' => '_stock_status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
'instock' => __( 'In stock', 'woocommerce' ),
'outofstock' => __( 'Out of stock', 'woocommerce' )
), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );