Cordial
asked 2 months ago

I wanted to add a Custom Field to WooCommerce. The field is supposed to be plain Text Field.
Then it should be displayed on the Frontend. Unfortunately, I didn’t find such a Text Field Code Snippet. But the Code below worked:
==============================================================

/**
 * @snippet       Display RRP/MSRP @ WooCommerce Single Product Page
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
  
// -----------------------------------------
// 1. Add RRP field input @ product edit page
  
add_action( 'woocommerce_product_options_pricing', 'bbloomer_add_RRP_to_products' );     
  
function bbloomer_add_RRP_to_products() {         
    woocommerce_wp_text_input( array(
        'id' => 'rrp',
        'class' => 'short wc_input_price',
        'label' => __( 'RRP', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
        'data_type' => 'price',
    ));     
}
  
// -----------------------------------------
// 2. Save RRP field via custom field
  
add_action( 'save_post_product', 'bbloomer_save_RRP' );
  
function bbloomer_save_RRP( $product_id ) {
    global $typenow;
    if ( 'product' === $typenow ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
        if ( isset( $_POST['rrp'] ) ) {
            update_post_meta( $product_id, 'rrp', $_POST['rrp'] );
        }
    }
}
  
// -----------------------------------------
// 3. Display RRP field @ single product page
  
add_action( 'woocommerce_single_product_summary', 'bbloomer_display_RRP', 9 );
  
function bbloomer_display_RRP() {
    global $product;
    if ( $product->get_type() <> 'variable' && $rrp = get_post_meta( $product->get_id(), 'rrp', true ) ) {
        echo '<div class="woocommerce_rrp">';
        _e( 'RRP: ', 'woocommerce' );
        echo '<span>' . wc_price( $rrp ) . '</span>';
        echo '</div>';
    }
}
====================================================================

The problem is that this Code is for Price. I needed something that is just plain Text, plus a few adjustments.
I want:
Title: PRODUCT YEAR
Field Type: Text Field
And when it is displayed, Title should be in Bold Font.
How can I make these happen?
Regards.

Powered by DW Question & Answer Pro