我试图添加一个自定义字段到产品管理屏幕使用woocommerce插件,所以我可以有一个下拉菜单来选择新的或使用作为产品的条件。
我得到的下拉显示在管理屏幕上,但它不会显示新的或使用在产品的前端。
我将这段代码添加到functions.php中:
// 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 ) );
}我将其添加到了短描述符. the中:
<?php _e( 'Condition: ', ‘woocommerce’ ); ?>
<?php
echo get_post_meta( get_the_ID(), ‘_conditionselect’, true );
?>知道为什么这不管用吗?
好的,我不知道我做了什么,但是现在“2”这个词出现在前端的“条件:”旁边。但它只出现在其中一个产品上。我以为它是从这个密码来的:
‘two’ => __( ‘Used’, ‘woocommerce’ ), 因此,我将“2”改为“使用”,但它仍然在前端显示“2”。
发布于 2015-08-22 07:45:35
在看完你写的代码之后。我注意到“get_post_meta”的语法在当前上下文中是正确的。即使在你将“2”改为"used“之后,它仍然在前端显示”2“,原因是在进行这些更改之后,您没有更新该产品。结果显示了以前的自定义字段值。
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”则显示在前端。
https://stackoverflow.com/questions/23798110
复制相似问题