Vishal Kumar
Vishal Kumar

Reputation: 1

how to save text values in wp_option of wordpress

I am trying to build a function for woocommerce and want to save these data to my wp_option.

function custom_before_price() {
                $args = array(
                'id' => 'custom_before_price_table1',
                'label' => __( 'Before Price', 'cbp1' ),
                'class' => 'cbp-custom-field',
                'desc_tip' => true,
                'description' => __( 'Enter the text before price.', 'cbpdes' ),
                );
                   woocommerce_wp_text_input( $args );
               }

               add_action( 'woocommerce_product_options_general_product_data', 'custom_before_price' );

Upvotes: 0

Views: 1274

Answers (3)

James Valeii
James Valeii

Reputation: 482

Saving the values to the wp_options table is not a normal WooCommerce behavior. WooCommerce saves product meta in wp_postmeta. There are a lot of possible downsides to storing data about specific products in wp_options. In particular, if you need to return data to the product admin editor from wp_options, you will need to labor on a method for that, as WooCommerce looks to wp_postmeta for custom meta.

The following set of functions will create your custom input and save the value when the product is updated via WP Admin.

    /**
    * Create custom meta
    **/
    function custom_before_price() {

        $args = array(
            'id' => 'custom_price',
            'label' => __( 'Custom Price', 'cprice' ),
            'data_type' => 'text', // Let WooCommerce formats our field as price field
            'description' => __( 'Enter the text before price.', 'cprice' ),    
        );

        woocommerce_wp_text_input( $args );
    }
    add_action( 'woocommerce_product_options_general_product_data', 'custom_before_price' );


    /**
    * Save custom meta
    **/
    add_action( 'woocommerce_process_product_meta', 'save_custom_field' );
    function save_custom_field( $post_id ) {
        global $wpdb;

        /*  Get Value */
        $custom_field_value = isset( $_POST['custom_price'] ) ? $_POST['custom_price'] : '';

        /*  Save to _options table (requested answer) */
        $option_name = $post_id.'-custom-price';
        $wpdb->replace(
            'wp_options',
            array(
                'option_name' => $option_name,
                'option_value' => $custom_field_value,
            ),
            array(
                '%s',
                '%s',
            )
        );
    

        /*  Save to postmeta table (normal for woocommerce) */
        $product = wc_get_product( $post_id );
        $product->update_meta_data( 'custom_price', $custom_field_value );
        $product->save();

    }

Upvotes: 0

Vishal Kumar
Vishal Kumar

Reputation: 1

function custom_before_price() {

    $args = array(
        'id' => 'custom_price',
        'label' => __( 'Custom Price', 'cprice' ),
        'data_type' => 'text', // Let WooCommerce formats our field as price field
        'description' => __( 'Enter the text before price.', 'cprice' ),    
    );

    woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_general_product_data', 'custom_before_price' );


/**
* Save custom meta
**/
add_action( 'woocommerce_process_product_meta', 'save_custom_field' );
function save_custom_field( $post_id ) {

    /*  Get Value */
    $custom_field_value = isset( $_POST['custom_price'] ) ? $_POST['custom_price'] : '';

    /*  Save to _options table (requested answer) */
    $option_name = $post_id.'-custom-price';
    add_option( $option_name, $custom_field_value );  
/*  its updates in wp_option also*/
    update_option( $option_name ,$custom_field_value );            

    /*  Save to postmeta table (normal for woocommerce) */
    $product = wc_get_product( $post_id );
    $product->update_meta_data( 'custom_price', $custom_field_value );
    $product->save();

}

Upvotes: 0

R.tbr
R.tbr

Reputation: 310

You should use add_option().something like this

add_option( string $option, mixed $value = '', string $deprecated = '', string|bool $autoload = 'yes' )

Upvotes: 1

Related Questions