woocommerce custom product field

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.

Read More

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.

Related posts

Leave a Reply

1 comment

  1. 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.

    // 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’ 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.