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

Modifications in woocommerce Add Product Page?

我正在为我的网站使用 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)

注意:答案字段应根据常规 Price/Sale 价格 + 添加的文本字段 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>';
 
}

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

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

屏幕截图: