我在我的网站上使用woocommerce。我想在SKU,常规价格,销售价格之后添加产品页面中添加一些额外的字段。额外字段包含默认值,如2%或5%。当用户输入产品价格时,应使用默认字段值&结果应显示在另一个字段中..
例如:
添加了文字字段2:2%(100%的2%= 2)
答案字段:107(100 + 5 + 2)
注意:答案字段应根据常规价格/销售价格+添加的文本字段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;
答案 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 ...
}
如果您有任何疑问,请告诉我。
截图: