woocommerce中的修改添加产品页面?

时间:2015-02-25 06:06:12

标签: wordpress woocommerce

我在我的网站上使用woocommerce。我想在SKU,常规价格,销售价格之后添加产品页面中添加一些额外的字段。额外字段包含默认值,如2%或5%。当用户输入产品价格时,应使用默认字段值&结果应显示在另一个字段中..

例如:

  1. SKU:001
  2. 正常价格(卢比):100
  3. 添加了文字字段1:5%(100%的5%= 5)
  4. 添加了文字字段2:2%(100%的2%= 2)

  5. 答案字段:107(100 + 5 + 2)

  6. 注意:答案字段应根据常规价格/销售价格+添加的文本字段1 +添加的文本字段2中的值自动计算。

    怎么做???

    我使用以下功能创建了字段...

    
    
    // 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>';
    	
    }
    &#13;
    &#13;
    &#13;

1 个答案:

答案 0 :(得分:0)

将以下代码添加到主题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 ...
}

如果您有任何疑问,请告诉我。

截图:

enter image description here