I am trying to add a custom field to the product admin screen using woocommerce plugin, so I can have a dropdown menu to select new or used as the condition of the product.
I got the drop down to show up in the admin screen, but it won’t display new or used on the frontend of the product.
I added this code to the 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 ) );
}
and I added this to the short-description.php:
<?php _e( 'Condition: ', âwoocommerceâ ); ?>
<?php
echo get_post_meta( get_the_ID(), â_conditionselectâ, true );
?>
Any Idea why this isn’t working?
Ok I’m not sure what I did but now the word “two” shows up next to “condition:” on the front end. BUT it only shows up on one of the products. I thought it was coming from this code:
âtwoâ => __( âUsedâ, âwoocommerceâ ),
So I changed “two” to “used” but it still displays “two” on the front end.
After going through the code which you have written. I observed that the syntax for ‘get_post_meta’ is correct in the current context. The reason why it still displayed ‘two’ on the front end even after you changes “two” to “used” is cause you didn’t updated that product after making these changes. Which Resulted in showing the previous custom field value.
The correct syntax of woocommerce_wp_select is.
‘instock’ and ‘outofstock’ are the values for the options which will be stored in db and ‘In stock’ and ‘Out of stock’ are displayed on the Front-end.