亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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 關注
  • 135 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號