基于 Woocommerce 3 中的 2 个自定义字段的产品常规价格计算 [英] Product regular price calculation based on 2 custom fields in Woocommerce 3
问题描述
在 Woocommerce 中,我在 Woocommerce 默认价格字段下的定价部分设置了 2 个自定义字段:保证金率和购买价格.
我想弄清楚如何根据以下计算自动更新产品价格(常规价格字段):
$product_price = $rate_margin * $purchase_price;
感谢任何帮助.
您应该始终添加用于问题中其他定价字段的代码.
下面的代码是:
- 显示和保存 2 个自定义字段:保证金率和购买价格.
- 保存根据这 2 个自定义字段值计算出的新产品价格.
<块引用>
之前,您需要删除生成那 2 个自定义字段的代码(因为它将被我的替换).
//添加和显示额外的产品定价自定义字段add_action('woocommerce_product_options_pricing', 'additional_product_pricing_option_fields', 50);函数 additional_product_pricing_option_fields() {$domain = "woocommerce";全球 $post;echo '</div><div class="options_group price show_if_simple show_if_external show_if_composite">';woocommerce_wp_text_input(数组('id' =>'_rate_margin','标签' =>__("利率保证金", $domain ),'占位符' =>'','说明' =>__("利率保证金解释在这里......", $domain ),'desc_tip' =>真的,) );woocommerce_wp_text_input(数组('id' =>'_购买价格','标签' =>__("购买价格", $domain ) .' ('.get_woocommerce_currency_symbol() .')','占位符' =>'','说明' =>__("利率保证金解释在这里......", $domain ),'desc_tip' =>真的,) );echo '<input type="hidden" name="_custom_price_nonce" value="' . wp_create_nonce() . '">';}//保存Rate margin"和Purchase_price"自定义字段值的实用函数函数 Saving_rate_margin_and_purchase_price( $product ) {//安全检查if ( isset($_POST['_custom_price_nonce']) && ! wp_verify_nonce($_POST['_custom_price_nonce']) ) {返回;}//保存Rate margin"和Purchase_price"自定义字段值if( isset($_POST['_rate_margin']) && isset($_POST['_purchase_price']) ) {$product->update_meta_data('_rate_margin', sanitize_text_field((float) $_POST['_rate_margin']));$product->update_meta_data('_purchase_price', sanitize_text_field((float) $_POST['_purchase_price']));}}//效用函数:从Rate margin"和Purchase price"自定义字段计算并保存产品价格函数calculate_and_save_new_product_price( $product ) {//当产品打折时禁用if( isset($_POST['_sale_price']) && $_POST['_sale_price'] > 0 ){返回;}//计算并保存新价格if( isset($_POST['_rate_margin']) && isset($_POST['_purchase_price'])&&$_POST['_rate_margin'] >0 &&$_POST['_purchase_price'] >0 ) {//获取定价数据$rate_margin = (float) $_POST['_rate_margin'];$purchase_price = (float) $_POST['_purchase_price'];$active_price = (float) $product->get_price();//计算新价格$new_price = $rate_margin * $purchase_price;//如果当前价格与计算出的新价格不同if( $new_price !== $active_price ) {//用新计算的价格更新常规价格和有效价格$product->set_price( $new_price );$product->set_regular_price( $new_price );}}}//保存和计算价格add_action( 'woocommerce_admin_process_product_object', 'update_product_meta_data', 100, 1 );函数 update_product_meta_data( $product ) {//保存Rate margin"和Purchase_price"自定义字段值Saving_rate_margin_and_purchase_price( $product );//<== 如果不与第一个函数一起使用,将被删除//从Rate margin"和Purchase price"自定义字段计算并保存产品价格calculate_and_save_new_product_price( $product );}
代码位于您的活动子主题(活动主题)的 function.php 文件中.经过测试并有效.
In Woocommerce I have set 2 custom fields in the admin in the pricing section under Woocommerce default prices fields: Margin rate and Purchase price.
I am trying to figure out how can I auto update the product price (regular price field) based on the calculation below:
$product_price = $rate_margin * $purchase_price;
Any help is appreciated.
You should add alway add the code that is used for your additional pricing fields in your question.
The code below is:
- displaying and saving 2 custom fields: Margin rate and Purchase price.
- saving a new product price calculated from that 2 custom fields values.
Before, you will need to remove your code that is generating that 2 custom fields (as it will be replaced by mine).
// Adding and displaying additional product pricing custom fields
add_action( 'woocommerce_product_options_pricing', 'additional_product_pricing_option_fields', 50 );
function additional_product_pricing_option_fields() {
$domain = "woocommerce";
global $post;
echo '</div><div class="options_group pricing show_if_simple show_if_external show_if_composite">';
woocommerce_wp_text_input( array(
'id' => '_rate_margin',
'label' => __("Rate margin", $domain ),
'placeholder' => '',
'description' => __("Rate margin explanation goes here…", $domain ),
'desc_tip' => true,
) );
woocommerce_wp_text_input( array(
'id' => '_purchase_price',
'label' => __("Purchase price", $domain ) . ' ('. get_woocommerce_currency_symbol() . ')',
'placeholder' => '',
'description' => __("Rate margin explanation goes here…", $domain ),
'desc_tip' => true,
) );
echo '<input type="hidden" name="_custom_price_nonce" value="' . wp_create_nonce() . '">';
}
// Utility function that save "Rate margin" and "Purchase_price" custom fields values
function saving_rate_margin_and_purchase_price( $product ) {
// Security check
if ( isset($_POST['_custom_price_nonce']) && ! wp_verify_nonce($_POST['_custom_price_nonce']) ) {
return;
}
// Save "Rate margin" and "Purchase_price" custom fields values
if( isset($_POST['_rate_margin']) && isset($_POST['_purchase_price']) ) {
$product->update_meta_data('_rate_margin', sanitize_text_field( (float) $_POST['_rate_margin'] ) );
$product->update_meta_data('_purchase_price', sanitize_text_field( (float) $_POST['_purchase_price'] ) );
}
}
// Utility function: Calculate and save product price from the "Rate margin" and the "Purchase price" custom fields
function calculate_and_save_new_product_price( $product ) {
// Disable when product is on sale
if( isset($_POST['_sale_price']) && $_POST['_sale_price'] > 0 ){
return;
}
// Calculate and save the new price
if( isset($_POST['_rate_margin']) && isset($_POST['_purchase_price'])
&& $_POST['_rate_margin'] > 0 && $_POST['_purchase_price'] > 0 ) {
// Catch the pricing data
$rate_margin = (float) $_POST['_rate_margin'];
$purchase_price = (float) $_POST['_purchase_price'];
$active_price = (float) $product->get_price();
// Calculating new price
$new_price = $rate_margin * $purchase_price;
// If the active price is different from the calculated new price
if( $new_price !== $active_price ) {
// Update regular price and active price with new calculated price
$product->set_price( $new_price );
$product->set_regular_price( $new_price );
}
}
}
// Saving and calculating prices
add_action( 'woocommerce_admin_process_product_object', 'update_product_meta_data', 100, 1 );
function update_product_meta_data( $product ) {
// Saving "Rate margin" and "Purchase_price" custom fields values
saving_rate_margin_and_purchase_price( $product ); // <== To be removed if not used with the first function
// Calculate and save product price from the "Rate margin" and the "Purchase price" custom fields
calculate_and_save_new_product_price( $product );
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
这篇关于基于 Woocommerce 3 中的 2 个自定义字段的产品常规价格计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!