Modifications in woocommerce Add Product Page?

I am using woocommerce for my site. I wanna to add some extra fields in add product page following to SKU, Regular Price, Sale Price.. Extra fields contains default values like 2% or 5%. when user enters Product price it should be calculated with default field values & result should be displayed in another field..

For Example:

Read More
  1. SKU : 001
  2. Regular Price(Rs) : 100
  3. Added Text Field1 : 5% (5% of 100 = 5)
  4. Added Text Field2 : 2% (2% of 100 = 2)

  5. Answer Field : 107 (100 + 5 + 2)

Note: Answer field should be automatically calculated from values present in Regular Price/Sale Price + added text field1 + added text field2.

How to do this???

I have created fields using following function…

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields() {
 
  global $woocommerce, $post;
  
  echo '<div class="options_group">';
  
  // Custom fields will be created here...
  // Text Field
woocommerce_wp_text_input( 
	array( 
		'id'          => '_text_field', 
		'label'       => __( 'Our Commision', 'woocommerce' ), 
		'placeholder' => '5%',
		'desc_tip'    => 'true',
		'description' => __( 'Commision will be added to Product Actual Price', 'woocommerce' ) 
	)
);
// Text Field
woocommerce_wp_text_input( 
	array( 
		'id'          => '_text_field', 
		'label'       => __( 'Payment Gateway Charges', 'woocommerce' ), 
		'placeholder' => '2%',
		'desc_tip'    => 'true',
		'description' => __( 'Payment Gateway Charges will be added to Product Actual Price', 'woocommerce' ) 
	)
);
  echo 'Selling Price = Your Price + Our Commision + Payment Gateway Charges.';
  echo '</div>';
	
}

Related posts

Leave a Reply

1 comment

  1. Add below code to your theme’s functions.php :

    add_action( 'woocommerce_product_options_general_product_data', 'so28712303_rohil_add_custom_general_fields' );
    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'so28712303_rohil_add_custom_general_fields_save' );
    
    function so28712303_rohil_add_custom_general_fields() {
    
        global $woocommerce, $post;
    
        echo '<div class="options_group">';
    
            woocommerce_wp_text_input( 
                array( 
                    'id'  => 'field_1', 
                    'label' => __( '<strong>Extra Field 1</strong>', 'woocommerce' ), 
                    'placeholder' => '', 
                    'description' => __( 'Please enter a number', 'woocommerce' ),
                    'type' => 'number',
                    'custom_attributes' => array(
                        'step'  => 'any',
                        'min'   => '0'
                    ) 
                )
            );
    
        echo '</div>';
    
        echo '<div class="options_group">';
    
            woocommerce_wp_text_input( 
                array( 
                    'id'  => 'field_2', 
                    'label' => __( '<strong>Extra Field 2</strong>', 'woocommerce' ), 
                    'placeholder' => '', 
                    'description' => __( 'Please enter a number', 'woocommerce' ),
                    'type' => 'number', 
                    'custom_attributes' => array(
                        'step'  => 'any',
                        'min'   => '0'
                    ) 
                )
            );
    
        echo '</div>';
    
        echo '<div class="options_group">';
    
            woocommerce_wp_text_input( 
                array( 
                    'id'  => 'result_field', 
                    'label' => __( '<strong style="color:#239804">Result</strong>', 'woocommerce' ), 
                    'placeholder' => '', 
                    'description' => __( 'Percentage of Price', 'woocommerce' ),
                    'type' => 'number',
                    'readonly' => 'readonly',
                    'custom_attributes' => array(
                        'step'  => 'any',
                        'min'   => '0',
                        'readonly' => 'readonly'
                    ) 
                )
            );
    
        echo '</div>';
    
    }//so28712303_rohil_add_custom_general_fields
    
    function so28712303_rohil_add_custom_general_fields_save( $post_id ){
        $woocommerce_field_1 = $_POST['field_1']; //Value of Extra field 1
        $woocommerce_field_2 = $_POST['field_2']; //Value of Extra field 2
        $woocommerce_result_field = $_POST['result_field']; //No use of this..you can delete
        $regular_price = $_POST['_regular_price']; //Value of regular price
    
        if( !empty( $woocommerce_field_1 ) || !empty( $woocommerce_field_2 ) ):
            update_post_meta( $post_id, 'field_1', esc_attr( $woocommerce_field_1 ) ); //Save value of Extra Field 1
            update_post_meta( $post_id, 'field_2', esc_attr( $woocommerce_field_2 ) ); //Save value of Extra Field 2
        endif;
        $result_field   =   ( $woocommerce_field_1 * $regular_price ) / 100 ; //Calculation goes here ...
            //if(empty($woocommerce_result_field))
        update_post_meta( $post_id, 'result_field', esc_attr( $result_field ) ); //Save result here ...
    }
    

    Let me know if you have any doubt.

    Screen shot :

    enter image description here