微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

在Woocommerce中,我在Woocommerce认价格字段下的定价部分中设置了2个自定义字段:保证金率和购买价格.

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.

以下代码是:

>显示和保存2个自定义字段:保证金率和购买价格.
>保存从该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文件中.经过测试和工作.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐