为了账号安全,请及时绑定邮箱和手机立即绑定

在 WooCommerce 中选择产品重量后计算并显示新价格

在 WooCommerce 中选择产品重量后计算并显示新价格

PHP
MMTTMM 2022-11-12 13:47:59
在 WooCommerce 中,我使用一个代码来显示牛排重量选择表单,保存选择数据并在购物车、结帐页面、编辑订单时和电子邮件通知中显示这些数据。// Display Custom Checkbox Fieldadd_action('woocommerce_product_options_general_product_data', 'steak_custom_field_add');function steak_custom_field_add(){    global $post;    // Checkbox    woocommerce_wp_checkbox(        array(            'id' => '_steak_checkbox',            'label' => __('Steak Weight', 'woocommerce' ),            'description' => __( 'If necessary, enable steak weight selection', 'woocommerce' )        )    );}// Save Custom Checkbox Fieldadd_action('woocommerce_process_product_meta', 'steak_custom_field_save');function steak_custom_field_save($post_id){    // Custom Product Checkbox Field    $steak_checkbox = isset( $_POST['_steak_checkbox'] ) ? 'yes' : 'no';    update_post_meta($post_id, '_steak_checkbox', esc_attr( $steak_checkbox ));}// Display Custom Select Boxadd_action( 'woocommerce_before_add_to_cart_button', 'display_steak_custom_field', 0 );function display_steak_custom_field() {    global $product;    // If is single product page and have the "steak_checkbox" enabled we display the field    if ( $product->get_meta( '_steak_checkbox' ) === 'yes' ) {        echo '<div class="steak_select_box">';        $select = woocommerce_form_field( 'steak_custom_options', array(            'type'          => 'select',            'class'         => array('my-steak-select-box form-row-wide'),            'label'         => __('Steak Weight'),            'required'      => false,            'return'       => false,            'options'   => array(            )        ), '' );        echo $select;        echo '</div>';    }}但不幸的是,我不知道如何解决一个问题......本产品价格以每100克计算。最小起订量为 300 克。而我需要的是,在选择产品的重量时,应该据此计算该产品的最终成本。
查看完整描述

1 回答

?
HUX布斯

TA贡献1876条经验 获得超6个赞

要调整产品价格,在购物车页面等。根据从下拉列表中选择的重量,添加以下代码


function my_before_calculate_totals( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )

        return;


    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )

        return;


    // Loop through cart items

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {     

        if( isset( $cart_item['steak_option'] ) ) {


            // Remove the last 2 zeros (100g becomes 1, 300g becomes 3, 1000g becomes 10, etc...)

            // Remove 'g' from grams

            // convert string to integer

            $chosen_weight = (int) str_replace( '00', '', str_replace('g', '', $cart_item['steak_option']) );


            // Get current price

            $current_price = $cart_item['data']->get_price();


            // Set new price, price is already known per 100g

            $cart_item['data']->set_price( $current_price * $chosen_weight );

        }

    }

}

add_action( 'woocommerce_before_calculate_totals', 'my_before_calculate_totals', 10, 1 );

要更改单个产品页面上的价格,请根据下拉菜单添加此


function add_footer_steak_script() {

    global $woocommerce, $product;

    ?>

    <script type="text/javascript">

        jQuery(document).ready(function ($) {

            console.log('JS works!');


            var price = <?php echo $product->get_price(); ?>, currency = '<?php echo get_woocommerce_currency_symbol(); ?>';


            $( '[name=steak_custom_options]' ).change(function(){

                if (!(this.value < 1)) {

                    var dropdown_val = this.value;

                    var remove_g = dropdown_val.replace( 'g', '' );

                    var remove_double_zero = remove_g.replace( '00', '' );


                    var product_total = parseFloat( price * remove_double_zero );


                    $( '.woocommerce-Price-amount' ).html( currency + product_total.toFixed(2));


                }

            });

        });

    </script>

    <?php

}

add_action('wp_footer','add_footer_steak_script');


查看完整回答
反对 回复 2022-11-12
  • 1 回答
  • 0 关注
  • 73 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信