php - 基于 Woocommerce 3 中 2 个自定义字段的产品常规价格计算

标签 php wordpress woocommerce product price

在 Woocommerce 中,我在 Woocommerce 默认价格字段下的定价部分的管理员中设置了 2 个自定义字段: margin 率和购买价格。

enter image description here

我想弄清楚如何根据以下计算自动更新产品价格(常规价格字段):

 $product_price = $rate_margin * $purchase_price;

感谢任何帮助。

最佳答案

You should add alway add the code that is used for your additional pricing fields in your question.

下面的代码是:

  1. 显示和保存 2 个自定义字段: margin 率和购买价格。
  2. 保 stub 据这 2 个自定义字段值计算的新产品价格。

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 );
}

代码位于您的事件子主题(事件主题)的 function.php 文件中。已测试并有效。

关于php - 基于 Woocommerce 3 中 2 个自定义字段的产品常规价格计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52718312/

相关文章:

php - 输入正确的 Google Recaptcha 挑战和激活码后无法重定向到所需页面

php - Codeigniter 2 的自定义表单验证错误消息

mysql - WordPress导入数据库错误

php - Woocommerce 订阅显示下一个付款日期

php - 撇号的问题

php - 使用 xpath 选择 css 类

javascript - 提交表单在 Firefox 中工作正常,但在 Safari 中不行

css - 水平显示元素

php - WooCommerce get_cart() 为 null

php - WooCommerce 的 WCFM 市场 - 忽略当前产品